redis.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Time : 2024/10/17 15:46
  5. @Author : cao
  6. @File : redis.py.py
  7. @Desc :
  8. """
  9. from config import get_logger
  10. import redis
  11. import os
  12. from typing import (Text,
  13. )
  14. import json
  15. logger = get_logger()
  16. def get_redis_config(env='prod'):
  17. if env == "prod":
  18. redis_host = os.getenv("RADIS_HOST", "192.168.100.159")
  19. redis_password = os.getenv("redis_password", "^YHN&UJM")
  20. redis_port = 6379
  21. redis_db = 0
  22. else:
  23. redis_host = os.getenv("RADIS_HOST", "10.0.0.24")
  24. redis_password = os.getenv("redis_password", "^YHN&UJM")
  25. redis_port = 6379
  26. redis_db = 0
  27. return redis_host, redis_password, redis_port, redis_db
  28. redis_host, redis_password, redis_port, redis_db = get_redis_config()
  29. pool = redis.ConnectionPool(host=redis_host,
  30. port=redis_port,
  31. password=redis_password,
  32. db=redis_db,
  33. socket_timeout=1000,
  34. decode_responses=True
  35. )
  36. r = redis.Redis(connection_pool=pool)
  37. def get_status_cache(uid: Text, bot_id: Text, label: Text, session_id: Text):
  38. # 获取用户当前状态
  39. from scene import Dialog, RobotService
  40. scene = None
  41. try:
  42. name = f"{bot_id}_{label}"
  43. key = f"{uid}_{session_id}"
  44. records = r.hget(name, key)
  45. if records:
  46. logger.info(f"获取缓存信息成功:uid:{uid},session:{session_id}")
  47. res = json.loads(records)
  48. logger.info(f"load cache: {res}")
  49. case = RobotService(**res)
  50. scene = Dialog(case)
  51. return scene
  52. else:
  53. logger.info(f"没有缓存信息:uid:{uid},session:{session_id}")
  54. return
  55. except Exception as e:
  56. logger.error("获取缓存信息失败%s", e)
  57. return
  58. def del_cache(uid: Text, bot_id: Text, label: Text, session_id: Text):
  59. try:
  60. name = f"{bot_id}_{label}"
  61. key = f"{uid}_{session_id}"
  62. r.hdel(name, key)
  63. logger.info("删除缓存状态信息success")
  64. except Exception as e:
  65. logger.error("删除缓存状态信息%s", e)
  66. def with_status_cache(uid: Text, bot_id: Text, label: Text, session_id: Text, scene):
  67. try:
  68. name = f"{bot_id}_{label}"
  69. key = f"{uid}_{session_id}"
  70. value = json.dumps(scene.case, ensure_ascii=False, default=lambda obj: obj.__dict__)
  71. logger.info(f"save cache:uid:{uid};sessionid:{session_id}:code:{scene.case.code}")
  72. r.hset(name, key, value)
  73. r.expire(name, 3600)
  74. except Exception as e:
  75. logger.error("redis缓存状态%s", e)
  76. def get_tracking(uid: Text):
  77. try:
  78. name = f"{uid}_tracking"
  79. key = f"{uid}"
  80. content = r.hget(name, key)
  81. if content:
  82. return content
  83. except Exception as e:
  84. logger.error(e)
  85. return None
  86. if __name__ == "__main__":
  87. set_cache("yxxxx2", "10001","status", "yxx", "2.10")