feat: inmemory and async cache expire configurable (#2496)

inmemory and async cache expire configurable
This commit is contained in:
ming 2024-07-08 03:19:42 -04:00 • committed by GitHub
commit f6636551bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 4 deletions

View file

@ -28,10 +28,11 @@ class CacheServiceFactory(ServiceFactory):
if redis_cache.is_connected(): if redis_cache.is_connected():
logger.debug("Redis cache is connected") logger.debug("Redis cache is connected")
return redis_cache return redis_cache
logger.warning("Redis cache is not connected, falling back to in-memory cache") else:
return AsyncInMemoryCache() # do not attempt to fallback to another cache type
raise ConnectionError("Failed to connect to Redis cache")
elif settings_service.settings.cache_type == "memory": elif settings_service.settings.cache_type == "memory":
return ThreadingInMemoryCache() return ThreadingInMemoryCache(expiration_time=settings_service.settings.cache_expire)
elif settings_service.settings.cache_type == "async": elif settings_service.settings.cache_type == "async":
return AsyncInMemoryCache() return AsyncInMemoryCache(expiration_time=settings_service.settings.cache_expire)

View file

@ -74,6 +74,8 @@ class Settings(BaseSettings):
"""The number of connections to allow that can be opened beyond the pool size. If not provided, the default is 10.""" """The number of connections to allow that can be opened beyond the pool size. If not provided, the default is 10."""
cache_type: str = "async" cache_type: str = "async"
"""The cache type can be 'async' or 'redis'.""" """The cache type can be 'async' or 'redis'."""
cache_expire: int = 3600
"""The cache expire in seconds."""
variable_store: str = "db" variable_store: str = "db"
"""The store can be 'db' or 'kubernetes'.""" """The store can be 'db' or 'kubernetes'."""