redis_config.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # !/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. @Project : lq-agent-api
  5. @File :redis_config.py
  6. @IDE :PyCharm
  7. @Author :
  8. @Date :2025/7/21 13:44
  9. '''
  10. from dataclasses import dataclass
  11. from foundation.base.config import config_handler
  12. @dataclass
  13. class RedisConfig:
  14. """Redis 连接配置"""
  15. url: str = "redis://127.0.0.1:6379"
  16. host: str = "127.0.0.1"
  17. port: int = 6379
  18. password: str = None
  19. db: int = 0
  20. max_connections: int = 50
  21. session_prefix: str = "session:"
  22. lock_prefix: str = "lock:"
  23. session_ttl: int = 3600 # 会话过期时间(秒)
  24. def load_config_from_env() -> tuple[RedisConfig]:
  25. """从环境变量加载配置"""
  26. redis_config = RedisConfig(
  27. url=config_handler.get("redis", "REDIS_URL", "redis://127.0.0.1:6379"),
  28. password=config_handler.get("redis", "REDIS_PASSWORD"),
  29. db=int(config_handler.get("redis", "REDIS_DB", "0")),
  30. max_connections=int(config_handler.get("redis", "REDIS_MAX_CONNECTIONS", "50"))
  31. )
  32. return redis_config