redis.py 967 B

12345678910111213141516171819202122232425262728293031
  1. from redis import asyncio as aioredis
  2. class RedisHelper(object):
  3. _pool: aioredis.connection.ConnectionPool = None
  4. _instance = None
  5. def __new__(cls, *args, **kwargs):
  6. if cls._instance is None:
  7. cls._instance = super().__new__(cls, *args, **kwargs)
  8. return cls._instance
  9. def _get_pool(self) -> aioredis.connection.ConnectionPool:
  10. if self._pool is None:
  11. self._pool = aioredis.ConnectionPool(
  12. host="r-bp1mb0v08fqi4hjffupd.redis.rds.aliyuncs.com", # 外网地址
  13. port=6379,
  14. db=0,
  15. password="Wqsd@2019",
  16. max_connections=100)
  17. return self._pool
  18. def get_client(self) -> aioredis.client.Redis:
  19. pool = self._get_pool()
  20. client = aioredis.Redis(connection_pool=pool)
  21. return client
  22. async def close(self):
  23. if self._pool:
  24. await self._pool.disconnect(inuse_connections=True)