diff --git a/pyproject.toml b/pyproject.toml index 9fc3893..5420649 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["setuptools>=62.6", "wheel"] +requires = ["setuptools>=77.0.3", "wheel"] build-backend = "setuptools.build_meta" [project] @@ -9,11 +9,11 @@ authors = [{ name = "Profitroll" }] description = "Universal bot library with functions needed for basic Discord/Telegram bot development." readme = "README.md" requires-python = ">=3.11" -license = { text = "GPLv3" } +license = "GPL-3.0" +license-files = ["LICENSE"] classifiers = [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", - "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", "Operating System :: OS Independent", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", diff --git a/requirements/_.txt b/requirements/_.txt index aa412d5..da0b621 100644 --- a/requirements/_.txt +++ b/requirements/_.txt @@ -1,2 +1,2 @@ aiofiles>=23.0.0 -typing-extensions~=4.13.0 \ No newline at end of file +typing-extensions~=4.14.0 \ No newline at end of file diff --git a/requirements/cache.txt b/requirements/cache.txt index 6c3d33f..31034da 100644 --- a/requirements/cache.txt +++ b/requirements/cache.txt @@ -1,2 +1,2 @@ pymemcache~=4.0.0 -redis~=6.1.0 \ No newline at end of file +redis~=6.2.0 \ No newline at end of file diff --git a/requirements/dev.txt b/requirements/dev.txt index 54f12e3..7d23cf9 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -1,12 +1,12 @@ black==25.1.0 build==1.2.2.post1 isort==5.13.2 -mypy==1.15.0 +mypy==1.16.1 pylint==3.3.7 -pytest-asyncio==0.26.0 -pytest-cov==6.1.1 -pytest==8.3.5 -tox==4.26.0 +pytest-asyncio==1.0.0 +pytest-cov==6.2.1 +pytest==8.4.1 +tox==4.27.0 twine==6.1.0 -types-aiofiles==24.1.0.20250516 +types-aiofiles==24.1.0.20250606 types-ujson==5.10.0.20250326 \ No newline at end of file diff --git a/src/libbot/__init__.py b/src/libbot/__init__.py index fcb6220..6604090 100644 --- a/src/libbot/__init__.py +++ b/src/libbot/__init__.py @@ -1,4 +1,4 @@ -__version__ = "4.2.0" +__version__ = "4.3.0" __license__ = "GPL3" __author__ = "Profitroll" diff --git a/src/libbot/cache/classes/cache.py b/src/libbot/cache/classes/cache.py index 8b0f617..5f9846c 100644 --- a/src/libbot/cache/classes/cache.py +++ b/src/libbot/cache/classes/cache.py @@ -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 diff --git a/src/libbot/cache/classes/cache_memcached.py b/src/libbot/cache/classes/cache_memcached.py index f592b1d..4c74453 100644 --- a/src/libbot/cache/classes/cache_memcached.py +++ b/src/libbot/cache/classes/cache_memcached.py @@ -13,9 +13,12 @@ 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 0 logger.info("Initialized Memcached for caching") @@ -69,28 +72,34 @@ 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=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) 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=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) 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: diff --git a/src/libbot/cache/classes/cache_redis.py b/src/libbot/cache/classes/cache_redis.py index 797a866..c5d0ad9 100644 --- a/src/libbot/cache/classes/cache_redis.py +++ b/src/libbot/cache/classes/cache_redis.py @@ -5,7 +5,7 @@ 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 logger: Logger = logging.getLogger(__name__) @@ -13,9 +13,12 @@ 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 | None = default_ttl_seconds logger.info("Initialized Redis for caching") @@ -69,28 +72,32 @@ 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=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) 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=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) 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: