12345678910111213141516171819202122232425262728293031 |
- from redis import asyncio as aioredis
- class RedisHelper(object):
- _pool: aioredis.connection.ConnectionPool = None
- _instance = None
- def __new__(cls, *args, **kwargs):
- if cls._instance is None:
- cls._instance = super().__new__(cls, *args, **kwargs)
- return cls._instance
- def _get_pool(self) -> aioredis.connection.ConnectionPool:
- if self._pool is None:
- self._pool = aioredis.ConnectionPool(
- host="r-bp1mb0v08fqi4hjffupd.redis.rds.aliyuncs.com", # 外网地址
- port=6379,
- db=0,
- password="Wqsd@2019",
- max_connections=100)
- return self._pool
- def get_client(self) -> aioredis.client.Redis:
- pool = self._get_pool()
- client = aioredis.Redis(connection_pool=pool)
- return client
- async def close(self):
- if self._pool:
- await self._pool.disconnect(inuse_connections=True)
|