Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

redis_conn.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import json
  2. import redis
  3. import logging
  4. from rag import settings
  5. from rag.utils import singleton
  6. @singleton
  7. class RedisDB:
  8. def __init__(self):
  9. self.REDIS = None
  10. self.config = settings.REDIS
  11. self.__open__()
  12. def __open__(self):
  13. try:
  14. self.REDIS = redis.Redis(host=self.config.get("host", "redis").split(":")[0],
  15. port=int(self.config.get("host", ":6379").split(":")[1]),
  16. db=int(self.config.get("db", 1)),
  17. password=self.config.get("password"))
  18. except Exception as e:
  19. logging.warning("Redis can't be connected.")
  20. return self.REDIS
  21. def is_alive(self):
  22. return self.REDIS is not None
  23. def get(self, k):
  24. if not self.REDIS: return
  25. try:
  26. return self.REDIS.get(k)
  27. except Exception as e:
  28. logging.warning("[EXCEPTION]get" + str(k) + "||" + str(e))
  29. self.__open__()
  30. def set_obj(self, k, obj, exp=3600):
  31. try:
  32. self.REDIS.set(k, json.dumps(obj, ensure_ascii=False), exp)
  33. return True
  34. except Exception as e:
  35. logging.warning("[EXCEPTION]set_obj" + str(k) + "||" + str(e))
  36. self.__open__()
  37. return False
  38. def set(self, k, v, exp=3600):
  39. try:
  40. self.REDIS.set(k, v, exp)
  41. return True
  42. except Exception as e:
  43. logging.warning("[EXCEPTION]set" + str(k) + "||" + str(e))
  44. self.__open__()
  45. return False
  46. REDIS_CONN = RedisDB()