Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Version 0.15.0
Unreleased

- Make ``SimpleCache`` thread-safe using a ``threading.RLock``. :issue:`446`
- Allow passing a custom ``serializer`` on cache creation and add ``JSONSerializer``. :pr:`462`


Version 0.14.0
--------------
Expand Down
12 changes: 9 additions & 3 deletions src/cachelib/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import typing as _t

from cachelib.base import BaseCache
from cachelib.serializers import BaseSerializer
from cachelib.serializers import DynamoDbSerializer

CREATED_AT_FIELD = "created_at"
Expand Down Expand Up @@ -33,10 +34,12 @@ class DynamoDbCache(BaseCache):
this as the TTL field, then DynamoDB will
automatically delete expired entries.
:param key_prefix: A prefix that should be added to all keys.

:param serializer: An optional serializer to use instead of the default
BaseSerializer. The serializer must implement the
dumps and loads methods.
"""

serializer = DynamoDbSerializer()
serializer: BaseSerializer = DynamoDbSerializer()

def __init__(
self,
Expand All @@ -45,6 +48,7 @@ def __init__(
key_field: _t.Optional[str] = "cache_key",
expiration_time_field: _t.Optional[str] = "expiration_time",
key_prefix: _t.Optional[str] = None,
serializer: _t.Optional[BaseSerializer] = None,
**kwargs: _t.Any,
):
super().__init__(default_timeout)
Expand All @@ -60,6 +64,8 @@ def __init__(
self.key_prefix = key_prefix or ""
self._dynamo = boto3.resource("dynamodb", **kwargs)
self._attr = boto3.dynamodb.conditions.Attr
if serializer is not None:
self.serializer = serializer

try:
self._table = self._dynamo.Table(table_name)
Expand Down Expand Up @@ -134,7 +140,7 @@ def get(self, key: str) -> _t.Any:
cache_item = self._get_item(self.key_prefix + key)
if cache_item:
response = cache_item[RESPONSE_FIELD]
value = self.serializer.loads(response)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the JSON serializer will be broken for dynamodb since this unpacking was happening inside the serializer .loads function I moved it here since it makes more sense and will allow other serializers to be used without special handling.

This change doesn't affect end user in anything while allowing for using other serializers.

value = self.serializer.loads(response.value)
return value
return None

Expand Down
10 changes: 9 additions & 1 deletion src/cachelib/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from time import time

from cachelib.base import BaseCache
from cachelib.serializers import BaseSerializer
from cachelib.serializers import FileSystemSerializer


Expand Down Expand Up @@ -42,6 +43,9 @@ class FileSystemCache(BaseCache):
generate the filename for cached results.
Default is lazy loaded and can be overridden by
setting `_default_hash_method`
:param serializer: An optional serializer to use instead of the default
BaseSerializer. The serializer must implement the
dump and load methods.
"""

#: used for temporary files by the FileSystemCache
Expand All @@ -51,7 +55,7 @@ class FileSystemCache(BaseCache):
#: default file name hashing method
_default_hash_method = staticmethod(_lazy_md5)

serializer = FileSystemSerializer()
serializer: BaseSerializer = FileSystemSerializer()

def __init__(
self,
Expand All @@ -60,6 +64,7 @@ def __init__(
default_timeout: int = 300,
mode: _t.Optional[int] = None,
hash_method: _t.Any = None,
serializer: _t.Optional[BaseSerializer] = None,
):
BaseCache.__init__(self, default_timeout)
self._path = cache_dir
Expand All @@ -76,6 +81,9 @@ def __init__(
if self._mode is None:
self._mode = self._get_compatible_platform_mode()

if serializer is not None:
self.serializer = serializer

try:
os.makedirs(self._path)
except OSError as ex:
Expand Down
2 changes: 2 additions & 0 deletions src/cachelib/memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class MemcachedCache(BaseCache):
the keys in the same format as passed. Furthermore all get methods
silently ignore key errors to not cause problems when untrusted user data
is passed to the get methods which is often the case in web applications.
This cache doesn't have a serializer since the underlying memcached client
libraries handle serialization internally."
Comment on lines +32 to +33
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could implement this but it's not simple so I left it as is.


:param servers: a list or tuple of server addresses or alternatively
a :class:`memcache.Client` or a compatible client.
Expand Down
18 changes: 12 additions & 6 deletions src/cachelib/mongodb.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import datetime
import logging
import typing as _t

from cachelib.base import BaseCache
from cachelib.serializers import BaseSerializer
from cachelib.serializers import MongoDbSerializer


class MongoDbCache(BaseCache):
Expand All @@ -19,10 +19,12 @@ class MongoDbCache(BaseCache):
:param default_timeout: Set the timeout in seconds after which cache entries
expire
:param key_prefix: A prefix that should be added to all keys.

:param serializer: An optional serializer to use instead of the default
BaseSerializer. The serializer must implement the
dumps and loads methods.
"""

serializer = BaseSerializer()
serializer: BaseSerializer = MongoDbSerializer()

def __init__(
self,
Expand All @@ -31,25 +33,29 @@ def __init__(
collection: _t.Optional[str] = "cache-collection",
default_timeout: int = 300,
key_prefix: _t.Optional[str] = None,
serializer: _t.Optional[BaseSerializer] = None,
**kwargs: _t.Any,
):
super().__init__(default_timeout)
try:
import pymongo
except ImportError:
logging.warning("no pymongo module found")
except ImportError as err:
raise RuntimeError("no pymongo module found") from err
Comment on lines +42 to +43
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the only cache backend that doesn't raise the error like this.

Also pymongo was unbound when used after these lines since on error we log and continue instead of raising.


if client is None or isinstance(client, str):
client = pymongo.MongoClient(host=client)
client = pymongo.MongoClient(host=client, **kwargs)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

**kwargs was there but not used for some reason.

self.client = client[db][collection]
index_info = self.client.index_information()
all_keys = {
subkey[0] for value in index_info.values() for subkey in value["key"]
}
if "id" not in all_keys:
self.client.create_index("id", unique=True)

self.key_prefix = key_prefix or ""
self.collection = collection
if serializer is not None:
self.serializer = serializer

def _utcnow(self) -> _t.Any:
"""Return a tz-aware UTC datetime representing the current time"""
Expand Down
7 changes: 6 additions & 1 deletion src/cachelib/redis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import typing as _t

from cachelib.redis_base import BaseRedisCache
from cachelib.serializers import BaseSerializer
from cachelib.serializers import RedisSerializer


Expand All @@ -22,6 +23,9 @@ class RedisCache(BaseRedisCache):
specified on :meth:`~BaseCache.set`. A timeout of
0 indicates that the cache never expires.
:param key_prefix: A prefix that should be added to all keys.
:param serializer: An optional serializer to use instead of the default
BaseSerializer. The serializer must implement the
dumps and loads methods.

Any additional keyword arguments will be passed to ``redis.Redis``.
"""
Expand All @@ -36,6 +40,7 @@ def __init__(
db: int = 0,
default_timeout: int = 300,
key_prefix: _t.Optional[_t.Union[str, _t.Callable[[], str]]] = None,
serializer: _t.Optional[BaseSerializer] = None,
**kwargs: _t.Any,
):
if host is None:
Expand All @@ -52,4 +57,4 @@ def __init__(
)
else:
client = host
super().__init__(client, default_timeout, key_prefix)
super().__init__(client, default_timeout, key_prefix, serializer)
9 changes: 8 additions & 1 deletion src/cachelib/redis_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from cachelib.base import BaseCache
from cachelib.serializers import BaseRedisSerializer
from cachelib.serializers import BaseSerializer


class BaseRedisCache(BaseCache):
Expand All @@ -15,21 +16,27 @@ class BaseRedisCache(BaseCache):
specified on :meth:`~BaseCache.set`. A timeout of
0 indicates that the cache never expires.
:param key_prefix: A prefix that should be added to all keys.
:param serializer: An optional serializer to use instead of the default
BaseSerializer. The serializer must implement the
dumps and loads methods.
"""

_read_client: _t.Any = None
_write_client: _t.Any = None
serializer = BaseRedisSerializer()
serializer: BaseSerializer = BaseRedisSerializer()

def __init__(
self,
client: _t.Any,
default_timeout: int = 300,
key_prefix: _t.Optional[_t.Union[str, _t.Callable[[], str]]] = None,
serializer: _t.Optional[BaseSerializer] = None,
):
BaseCache.__init__(self, default_timeout)
self._read_client = self._write_client = client
self.key_prefix = key_prefix or ""
if serializer is not None:
self.serializer = serializer

def _get_prefix(self) -> str:
return (
Expand Down
68 changes: 58 additions & 10 deletions src/cachelib/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import pickle
import typing as _t
Expand All @@ -11,10 +12,8 @@ class BaseSerializer:
used only by FileSystemCache which dumps/loads to/from a file stream.
"""

def _warn(self, e: pickle.PickleError) -> None:
logging.warning(
f"An exception has been raised during a pickling operation: {e}"
)
def _warn(self, e: Exception) -> None:
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

broaden exception to allow for pickle and json errors handling without getting type errors

logging.warning(f"An exception has been raised during an operation: {e}")

def dump(
self, value: int, f: _t.IO[bytes], protocol: int = pickle.HIGHEST_PROTOCOL
Expand Down Expand Up @@ -119,9 +118,58 @@ class ValkeySerializer(BaseRedisSerializer):
class DynamoDbSerializer(RedisSerializer):
"""Default serializer for DynamoDbCache."""

def loads(self, value: _t.Any) -> _t.Any:
"""The reversal of :meth:`dump_object`. This might be called with
None.
"""
value = value.value
return super().loads(value)
pass


class MongoDbSerializer(BaseSerializer):
"""Default serializer for MongoDbCache."""

pass
Comment on lines +124 to +127
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was not implemented for some reason



class JSONSerializer(BaseSerializer):
"""Generic JSON serializer for caches.

Use this serializer if you want to serialize to JSON instead of pickle.
Note that JSON does not support serialization of Python bytes objects.
If you need to cache bytes, use the default pickle-based serializer.
This serializer is not used by default.
"""

def dump(
self, value: _t.Any, f: _t.IO[bytes], *args: _t.Any, **kwargs: _t.Any
) -> None:
try:
f.write(json.dumps(value, *args, **kwargs).encode())
except (TypeError, ValueError) as e:
self._warn(e)

def load(self, f: _t.IO[bytes], *args: _t.Any, **kwargs: _t.Any) -> _t.Any:
try:
data = json.loads(f.read(), *args, **kwargs)
except (TypeError, ValueError) as e:
self._warn(e)
return None
else:
return data

def dumps(
self, value: _t.Any, *args: _t.Any, **kwargs: _t.Any
) -> _t.Optional[bytes]:
try:
serialized = json.dumps(value, *args, **kwargs).encode()
except (TypeError, ValueError) as e:
self._warn(e)
return None
return serialized

def loads(
self, bvalue: _t.Union[str, bytes, bytearray], *args: _t.Any, **kwargs: _t.Any
) -> _t.Any:
try:
data = json.loads(bvalue, *args, **kwargs)
except (TypeError, ValueError) as e:
self._warn(e)
return None
else:
return data
9 changes: 8 additions & 1 deletion src/cachelib/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from time import time

from cachelib.base import BaseCache
from cachelib.serializers import BaseSerializer
from cachelib.serializers import SimpleSerializer


Expand All @@ -16,19 +17,25 @@ class SimpleCache(BaseCache):
:param default_timeout: the default timeout that is used if no timeout is
specified on :meth:`~BaseCache.set`. A timeout of
0 indicates that the cache never expires.
:param serializer: An optional serializer to use instead of the default
BaseSerializer. The serializer must implement the
dumps and loads methods.
"""

serializer = SimpleSerializer()
serializer: BaseSerializer = SimpleSerializer()

def __init__(
self,
threshold: int = 500,
default_timeout: int = 300,
serializer: _t.Optional[BaseSerializer] = None,
):
BaseCache.__init__(self, default_timeout)
self._cache: _t.Dict[str, _t.Any] = {}
self._threshold = threshold or 500 # threshold = 0
self._lock = threading.RLock()
if serializer is not None:
self.serializer = serializer

def _over_threshold(self) -> bool:
return len(self._cache) > self._threshold
Expand Down
Loading