creditrating-wcq/Utils/DataBase/RedisUtils.py

34 lines
1.2 KiB
Python
Raw Permalink Normal View History

2023-02-20 09:44:05 +08:00
import redis as redis
REDIS_HOST = "192.168.18.128"
REDIS_PORT = 6379
class RedisPool:
def __init__(self, host: str, port: int, password: str = None):
self.host = host
self.port = port
self.password = password if password else None
self.redis_pool: redis.ConnectionPool = None
self.conn: redis.Redis = None
def connect(self, max_connections=10):
if self.password:
self.redis_pool = redis.ConnectionPool(host=self.host, port=self.port,
decode_responses=True,
max_connections=max_connections)
else:
self.redis_pool = redis.ConnectionPool(host=self.host, port=self.port,
password=self.password,
decode_responses=True,
max_connections=max_connections)
self.conn = self.get_redis_client()
def get_redis_client(self):
conn = redis.Redis(connection_pool=self.redis_pool, decode_responses=True)
return conn
redis_pool = RedisPool(host=REDIS_HOST, port=REDIS_PORT)
redis_pool.connect()