db.py 979 B

12345678910111213141516171819202122232425262728
  1. from redis import StrictRedis
  2. import logging
  3. import logging.handlers
  4. import os
  5. from .configure import conf
  6. class RedisDB(StrictRedis):
  7. def __init__(self, host, port, username, passwd, db):
  8. super().__init__(host=host, port=port, username=username, password=passwd, db=db, decode_responses=True)
  9. # redis是线程安全的
  10. self.logger = logging.getLogger("main.database")
  11. self.logger.setLevel(conf["LOG_LEVEL"])
  12. if len(conf["LOG_HOME"]) > 0:
  13. handle = logging.handlers.TimedRotatingFileHandler(
  14. os.path.join(conf["LOG_HOME"], f"redis-{username}@{host}.log"), backupCount=10)
  15. handle.setFormatter(logging.Formatter(conf["LOG_FORMAT"]))
  16. self.logger.addHandler(handle)
  17. redis = RedisDB(host=conf["REDIS_HOST"],
  18. port=conf["REDIS_PORT"],
  19. username=conf["REDIS_NAME"],
  20. passwd=conf["REDIS_PASSWD"],
  21. db=conf["REDIS_DATABASE"])