Closes #219
All checks were successful
Analysis / SonarCloud (push) Successful in 1m35s

This commit is contained in:
2025-07-07 23:35:21 +02:00
parent ad38dbdca1
commit 3110bb64b1
4 changed files with 15 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
__version__ = "4.2.0" __version__ = "4.3.0"
__license__ = "GPL3" __license__ = "GPL3"
__author__ = "Profitroll" __author__ = "Profitroll"

View File

@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Any, Dict from typing import Any, Dict, Optional
import pymemcache import pymemcache
import redis import redis
@@ -27,16 +27,16 @@ class Cache(ABC):
pass pass
@abstractmethod @abstractmethod
def set_json(self, key: str, value: Any) -> None: def set_json(self, key: str, value: Any, ttl_seconds: Optional[int] = None) -> None:
# TODO This method must also carry out ObjectId conversion! # TODO This method must also carry out ObjectId conversion!
pass pass
@abstractmethod @abstractmethod
def set_string(self, key: str, value: str) -> None: def set_string(self, key: str, value: str, ttl_seconds: Optional[int] = None) -> None:
pass pass
@abstractmethod @abstractmethod
def set_object(self, key: str, value: Any) -> None: def set_object(self, key: str, value: Any, ttl_seconds: Optional[int] = None) -> None:
pass pass
@abstractmethod @abstractmethod

View File

@@ -69,28 +69,28 @@ class CacheMemcached(Cache):
def get_object(self, key: str) -> Any | None: def get_object(self, key: str) -> Any | None:
raise NotImplementedError() raise NotImplementedError()
def set_json(self, key: str, value: Any) -> None: def set_json(self, key: str, value: Any, ttl_seconds: Optional[int] = None) -> None:
key = self._get_prefixed_key(key) key = self._get_prefixed_key(key)
try: try:
self.client.set(key, _json_to_string(value)) self.client.set(key, _json_to_string(value), expire=0 if ttl_seconds is None else ttl_seconds)
logger.debug("Set json cache key '%s'", key) logger.debug("Set json cache key '%s'", key)
except Exception as exc: except Exception as exc:
logger.error("Could not set json cache key '%s' due to: %s", key, exc) logger.error("Could not set json cache key '%s' due to: %s", key, exc)
return None return None
def set_string(self, key: str, value: str) -> None: def set_string(self, key: str, value: str, ttl_seconds: Optional[int] = None) -> None:
key = self._get_prefixed_key(key) key = self._get_prefixed_key(key)
try: try:
self.client.set(key, value) self.client.set(key, value, expire=0 if ttl_seconds is None else ttl_seconds)
logger.debug("Set string cache key '%s'", key) logger.debug("Set string cache key '%s'", key)
except Exception as exc: except Exception as exc:
logger.error("Could not set string cache key '%s' due to: %s", key, exc) logger.error("Could not set string cache key '%s' due to: %s", key, exc)
return None return None
# TODO Implement binary serialization # TODO Implement binary serialization
def set_object(self, key: str, value: Any) -> None: def set_object(self, key: str, value: Any, ttl_seconds: Optional[int] = None) -> None:
raise NotImplementedError() raise NotImplementedError()
def delete(self, key: str) -> None: def delete(self, key: str) -> None:

View File

@@ -69,28 +69,28 @@ class CacheRedis(Cache):
def get_object(self, key: str) -> Any | None: def get_object(self, key: str) -> Any | None:
raise NotImplementedError() raise NotImplementedError()
def set_json(self, key: str, value: Any) -> None: def set_json(self, key: str, value: Any, ttl_seconds: Optional[int] = None) -> None:
key = self._get_prefixed_key(key) key = self._get_prefixed_key(key)
try: try:
self.client.set(key, _json_to_string(value)) self.client.set(key, _json_to_string(value), ex=ttl_seconds)
logger.debug("Set json cache key '%s'", key) logger.debug("Set json cache key '%s'", key)
except Exception as exc: except Exception as exc:
logger.error("Could not set json cache key '%s' due to: %s", key, exc) logger.error("Could not set json cache key '%s' due to: %s", key, exc)
return None return None
def set_string(self, key: str, value: str) -> None: def set_string(self, key: str, value: str, ttl_seconds: Optional[int] = None) -> None:
key = self._get_prefixed_key(key) key = self._get_prefixed_key(key)
try: try:
self.client.set(key, value) self.client.set(key, value, ex=ttl_seconds)
logger.debug("Set string cache key '%s'", key) logger.debug("Set string cache key '%s'", key)
except Exception as exc: except Exception as exc:
logger.error("Could not set string cache key '%s' due to: %s", key, exc) logger.error("Could not set string cache key '%s' due to: %s", key, exc)
return None return None
# TODO Implement binary serialization # TODO Implement binary serialization
def set_object(self, key: str, value: Any) -> None: def set_object(self, key: str, value: Any, ttl_seconds: Optional[int] = None) -> None:
raise NotImplementedError() raise NotImplementedError()
def delete(self, key: str) -> None: def delete(self, key: str) -> None: