| 125 | |
| 126 | |
| 127 | def init_sqlbot_cache(): |
| 128 | cache_type: str = settings.CACHE_TYPE |
| 129 | if cache_type == "memory": |
| 130 | FastAPICache.init(InMemoryBackend()) |
| 131 | SQLBotLogUtil.info("SQLBot 使用内存缓存, 仅支持单进程模式") |
| 132 | elif cache_type == "redis": |
| 133 | from fastapi_cache.backends.redis import RedisBackend |
| 134 | import redis.asyncio as redis |
| 135 | from redis.asyncio.connection import ConnectionPool |
| 136 | redis_url = settings.CACHE_REDIS_URL or "redis://localhost:6379/0" |
| 137 | pool = ConnectionPool.from_url(url=redis_url) |
| 138 | redis_client = redis.Redis(connection_pool=pool) |
| 139 | FastAPICache.init(RedisBackend(redis_client), prefix="sqlbot-cache") |
| 140 | SQLBotLogUtil.info(f"SQLBot 使用Redis缓存, 可使用多进程模式") |
| 141 | else: |
| 142 | SQLBotLogUtil.warning("SQLBot 未启用缓存, 可使用多进程模式") |
| 143 | |
| 144 | |
| 145 | def is_cache_initialized() -> bool: |