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"
__author__ = "Profitroll"

View File

@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import Any, Dict
from typing import Any, Dict, Optional
import pymemcache
import redis
@@ -27,16 +27,16 @@ class Cache(ABC):
pass
@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!
pass
@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
@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
@abstractmethod

View File

@@ -69,28 +69,28 @@ class CacheMemcached(Cache):
def get_object(self, key: str) -> Any | None:
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)
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)
except Exception as exc:
logger.error("Could not set json cache key '%s' due to: %s", key, exc)
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)
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)
except Exception as exc:
logger.error("Could not set string cache key '%s' due to: %s", key, exc)
return None
# 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()
def delete(self, key: str) -> None:

View File

@@ -69,28 +69,28 @@ class CacheRedis(Cache):
def get_object(self, key: str) -> Any | None:
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)
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)
except Exception as exc:
logger.error("Could not set json cache key '%s' due to: %s", key, exc)
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)
try:
self.client.set(key, value)
self.client.set(key, value, ex=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)
return None
# 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()
def delete(self, key: str) -> None: