Added support for cache prefix
All checks were successful
Analysis / SonarCloud (push) Successful in 54s
Analysis / SonarCloud (pull_request) Successful in 46s
Tests / Build and Test (3.11) (pull_request) Successful in 1m12s
Tests / Build and Test (3.12) (pull_request) Successful in 1m9s
Tests / Build and Test (3.13) (pull_request) Successful in 1m6s

This commit is contained in:
2025-05-18 17:29:27 +02:00
parent 84e1cf7ce9
commit 95abf4265c
5 changed files with 44 additions and 14 deletions

View File

@@ -1,6 +1,6 @@
import logging
from logging import Logger
from typing import Dict, Any
from typing import Dict, Any, Optional
from pymemcache import Client
@@ -13,21 +13,27 @@ logger: Logger = logging.getLogger(__name__)
class CacheMemcached(Cache):
client: Client
def __init__(self, client: Client):
self.client = client
def __init__(self, client: Client, prefix: Optional[str] = None):
self.client: Client = client
self.prefix: str | None = prefix
logger.info("Initialized Memcached for caching")
@classmethod
def from_config(cls, engine_config: Dict[str, Any]) -> "CacheMemcached":
def from_config(cls, engine_config: Dict[str, Any], prefix: Optional[str] = None) -> "CacheMemcached":
if "uri" not in engine_config:
raise KeyError(
"Cache configuration is invalid. Please check if all keys are set (engine: memcached)"
)
return cls(Client(engine_config["uri"], default_noreply=True))
return cls(Client(engine_config["uri"], default_noreply=True), prefix=prefix)
def _get_prefixed_key(self, key: str) -> str:
return key if self.prefix is None else f"{self.prefix}_{key}"
def get_json(self, key: str) -> Any | None:
key = self._get_prefixed_key(key)
try:
result: Any | None = self.client.get(key, None)
@@ -43,6 +49,8 @@ class CacheMemcached(Cache):
return None if result is None else _string_to_json(result)
def get_string(self, key: str) -> str | None:
key = self._get_prefixed_key(key)
try:
result: str | None = self.client.get(key, None)
@@ -62,6 +70,8 @@ class CacheMemcached(Cache):
raise NotImplementedError()
def set_json(self, key: str, value: Any) -> None:
key = self._get_prefixed_key(key)
try:
self.client.set(key, _json_to_string(value))
logger.debug("Set json cache key '%s'", key)
@@ -70,6 +80,8 @@ class CacheMemcached(Cache):
return None
def set_string(self, key: str, value: str) -> None:
key = self._get_prefixed_key(key)
try:
self.client.set(key, value)
logger.debug("Set string cache key '%s'", key)
@@ -82,6 +94,8 @@ class CacheMemcached(Cache):
raise NotImplementedError()
def delete(self, key: str) -> None:
key = self._get_prefixed_key(key)
try:
self.client.delete(key)
logger.debug("Deleted cache key '%s'", key)