Added a default TTL of 300 seconds for cache entries (#219)
All checks were successful
Analysis / SonarCloud (push) Successful in 50s
Analysis / SonarCloud (pull_request) Successful in 1m4s
Tests / Build and Test (3.11) (pull_request) Successful in 1m14s
Tests / Build and Test (3.12) (pull_request) Successful in 1m15s
Tests / Build and Test (3.13) (pull_request) Successful in 1m19s

This commit is contained in:
2025-07-07 23:47:55 +02:00
parent 3110bb64b1
commit 32a9e14d0c
2 changed files with 31 additions and 7 deletions

View File

@@ -7,15 +7,22 @@ from pymemcache import Client
from .cache import Cache
from ..utils._objects import _json_to_string, _string_to_json
DEFAULT_TTS_SECONDS: int = 300
logger: Logger = logging.getLogger(__name__)
class CacheMemcached(Cache):
client: Client
def __init__(self, client: Client, prefix: Optional[str] = None):
def __init__(
self, client: Client, prefix: Optional[str] = None, default_ttl_seconds: Optional[int] = None
) -> None:
self.client: Client = client
self.prefix: str | None = prefix
self.default_ttl_seconds: int = (
default_ttl_seconds if default_ttl_seconds is not None else DEFAULT_TTS_SECONDS
)
logger.info("Initialized Memcached for caching")
@@ -73,7 +80,11 @@ class CacheMemcached(Cache):
key = self._get_prefixed_key(key)
try:
self.client.set(key, _json_to_string(value), expire=0 if ttl_seconds is None else ttl_seconds)
self.client.set(
key,
_json_to_string(value),
expire=self.default_ttl_seconds if ttl_seconds is None else ttl_seconds,
)
logger.debug("Set json cache key '%s'", key)
except Exception as exc:
logger.error("Could not set json cache key '%s' due to: %s", key, exc)
@@ -83,7 +94,9 @@ class CacheMemcached(Cache):
key = self._get_prefixed_key(key)
try:
self.client.set(key, value, expire=0 if ttl_seconds is None else ttl_seconds)
self.client.set(
key, value, expire=self.default_ttl_seconds if ttl_seconds is None else ttl_seconds
)
logger.debug("Set string cache key '%s'", key)
except Exception as exc:
logger.error("Could not set string cache key '%s' due to: %s", key, exc)

View File

@@ -5,7 +5,9 @@ from typing import Dict, Any, Optional
from redis import Redis
from .cache import Cache
from ..utils._objects import _string_to_json, _json_to_string
from ..utils._objects import _json_to_string, _string_to_json
DEFAULT_TTS_SECONDS: int = 300
logger: Logger = logging.getLogger(__name__)
@@ -13,9 +15,14 @@ logger: Logger = logging.getLogger(__name__)
class CacheRedis(Cache):
client: Redis
def __init__(self, client: Redis, prefix: Optional[str] = None):
def __init__(
self, client: Redis, prefix: Optional[str] = None, default_ttl_seconds: Optional[int] = None
) -> None:
self.client: Redis = client
self.prefix: str | None = prefix
self.default_ttl_seconds: int = (
default_ttl_seconds if default_ttl_seconds is not None else DEFAULT_TTS_SECONDS
)
logger.info("Initialized Redis for caching")
@@ -73,7 +80,11 @@ class CacheRedis(Cache):
key = self._get_prefixed_key(key)
try:
self.client.set(key, _json_to_string(value), ex=ttl_seconds)
self.client.set(
key,
_json_to_string(value),
ex=self.default_ttl_seconds if ttl_seconds is None else ttl_seconds,
)
logger.debug("Set json cache key '%s'", key)
except Exception as exc:
logger.error("Could not set json cache key '%s' due to: %s", key, exc)
@@ -83,7 +94,7 @@ class CacheRedis(Cache):
key = self._get_prefixed_key(key)
try:
self.client.set(key, value, ex=ttl_seconds)
self.client.set(key, value, ex=self.default_ttl_seconds if ttl_seconds is None else ttl_seconds)
logger.debug("Set string cache key '%s'", key)
except Exception as exc:
logger.error("Could not set string cache key '%s' due to: %s", key, exc)