Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
1daed542a3
|
|||
e4ce5976f2
|
|||
32a9e14d0c
|
|||
3110bb64b1
|
|||
ad38dbdca1
|
|||
edc3e0717d
|
|||
d70fd4f491
|
|||
c4fb1dd5dd
|
|||
38bf43a5e7
|
|||
5dff5fa71d
|
|||
e596658c68
|
|||
1e1b04a8ff
|
|||
69b034c007
|
|||
|
54de950899 | ||
c17a206c44
|
|||
dc05eb0ccb
|
|||
3a7f748d96
|
@@ -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",
|
||||
|
@@ -1,2 +1,2 @@
|
||||
aiofiles>=23.0.0
|
||||
typing-extensions~=4.13.0
|
||||
typing-extensions~=4.14.0
|
@@ -1,2 +1,2 @@
|
||||
pymemcache~=4.0.0
|
||||
redis~=6.1.0
|
||||
redis~=6.2.0
|
@@ -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
|
@@ -1,4 +1,4 @@
|
||||
__version__ = "4.2.0"
|
||||
__version__ = "4.3.0"
|
||||
__license__ = "GPL3"
|
||||
__author__ = "Profitroll"
|
||||
|
||||
|
8
src/libbot/cache/classes/cache.py
vendored
8
src/libbot/cache/classes/cache.py
vendored
@@ -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
|
||||
|
21
src/libbot/cache/classes/cache_memcached.py
vendored
21
src/libbot/cache/classes/cache_memcached.py
vendored
@@ -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:
|
||||
|
21
src/libbot/cache/classes/cache_redis.py
vendored
21
src/libbot/cache/classes/cache_redis.py
vendored
@@ -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:
|
||||
|
Reference in New Issue
Block a user