Merge pull request 'v4.3.0' (#220) from dev into main
All checks were successful
Analysis / SonarCloud (push) Successful in 1m0s
Tests / Build and Test (3.11) (push) Successful in 1m23s
Tests / Build and Test (3.12) (push) Successful in 1m20s
Tests / Build and Test (3.13) (push) Successful in 1m11s
Upload Python Package / release-build (release) Successful in 26s
Upload Python Package / gitea-publish (release) Successful in 14s
Upload Python Package / pypi-publish (release) Successful in 15s

Reviewed-on: #220
This commit is contained in:
2025-07-08 01:34:43 +03:00
8 changed files with 45 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
[build-system] [build-system]
requires = ["setuptools>=62.6", "wheel"] requires = ["setuptools>=77.0.3", "wheel"]
build-backend = "setuptools.build_meta" build-backend = "setuptools.build_meta"
[project] [project]
@@ -9,11 +9,11 @@ authors = [{ name = "Profitroll" }]
description = "Universal bot library with functions needed for basic Discord/Telegram bot development." description = "Universal bot library with functions needed for basic Discord/Telegram bot development."
readme = "README.md" readme = "README.md"
requires-python = ">=3.11" requires-python = ">=3.11"
license = { text = "GPLv3" } license = "GPL-3.0"
license-files = ["LICENSE"]
classifiers = [ classifiers = [
"Development Status :: 3 - Alpha", "Development Status :: 3 - Alpha",
"Intended Audience :: Developers", "Intended Audience :: Developers",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: OS Independent", "Operating System :: OS Independent",
"Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.12",

View File

@@ -1,2 +1,2 @@
aiofiles>=23.0.0 aiofiles>=23.0.0
typing-extensions~=4.13.0 typing-extensions~=4.14.0

View File

@@ -1,2 +1,2 @@
pymemcache~=4.0.0 pymemcache~=4.0.0
redis~=6.1.0 redis~=6.2.0

View File

@@ -1,12 +1,12 @@
black==25.1.0 black==25.1.0
build==1.2.2.post1 build==1.2.2.post1
isort==5.13.2 isort==5.13.2
mypy==1.15.0 mypy==1.16.1
pylint==3.3.7 pylint==3.3.7
pytest-asyncio==0.26.0 pytest-asyncio==1.0.0
pytest-cov==6.1.1 pytest-cov==6.2.1
pytest==8.3.5 pytest==8.4.1
tox==4.26.0 tox==4.27.0
twine==6.1.0 twine==6.1.0
types-aiofiles==24.1.0.20250516 types-aiofiles==24.1.0.20250606
types-ujson==5.10.0.20250326 types-ujson==5.10.0.20250326

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

@@ -13,9 +13,12 @@ logger: Logger = logging.getLogger(__name__)
class CacheMemcached(Cache): class CacheMemcached(Cache):
client: Client 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.client: Client = client
self.prefix: str | None = prefix self.prefix: str | None = prefix
self.default_ttl_seconds: int = default_ttl_seconds if default_ttl_seconds is not None else 0
logger.info("Initialized Memcached for caching") logger.info("Initialized Memcached for caching")
@@ -69,28 +72,34 @@ 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=self.default_ttl_seconds 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=self.default_ttl_seconds 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

@@ -5,7 +5,7 @@ from typing import Dict, Any, Optional
from redis import Redis from redis import Redis
from .cache import Cache from .cache import Cache
from ..utils._objects import _string_to_json, _json_to_string from ..utils._objects import _json_to_string, _string_to_json
logger: Logger = logging.getLogger(__name__) logger: Logger = logging.getLogger(__name__)
@@ -13,9 +13,12 @@ logger: Logger = logging.getLogger(__name__)
class CacheRedis(Cache): class CacheRedis(Cache):
client: Redis 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.client: Redis = client
self.prefix: str | None = prefix self.prefix: str | None = prefix
self.default_ttl_seconds: int | None = default_ttl_seconds
logger.info("Initialized Redis for caching") logger.info("Initialized Redis for caching")
@@ -69,28 +72,32 @@ 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=self.default_ttl_seconds 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, ex=self.default_ttl_seconds 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: