Skip to content

Commit 85e3636

Browse files
committed
docs: add compatibility note for serialization methods in caching policies
1 parent 126aa68 commit 85e3636

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,22 @@ def get_book(pool: ConnectionPool, book_id: int):
768768
769769
- Compatibility with other [decorator][]s is not guaranteed.
770770
771+
- The built-in policies in `redis_func_cache.policies` use [`pickle`][] to serialize function arguments, then calculate the cache key by hashing the serialized data with `md5`. [`pickle`][] is chosen because only the hash bytes are stored in Redis, not the serialized data itself, so this is safe. However, [`pickle`][] causes incompatibility between different Python versions. If your application needs to be compatible across Python versions, you should define your own hash policy using a version-compatible serialization method, for example:
772+
773+
```python
774+
from redis_func_cache import RedisFuncCache
775+
from redis_func_cache.policies.abstract import BaseSinglePolicy
776+
from redis_func_cache.mixins.hash import JsonMd5HashMixin
777+
from redis_func_cache.mixins.scripts import LfuScriptsMixin
778+
779+
class MyLfuPolicy(LfuScriptsMixin, JsonMd5HashMixin, BaseSinglePolicy):
780+
__key__ = "my-lfu"
781+
782+
my_cache = RedisFuncCache(__name__, policy=MyLfuPolicy, client=redis_client_factory)
783+
```
784+
785+
As shown above, unlike [`pickle`][], [json][] can be used across different Python versions.
786+
771787
- The cache eviction policies are mainly based on [Redis][] sorted set's score ordering. For most policies, the score is a positive integer. Its maximum value is `2^32-1` in [Redis][], which limits the number of times of eviction replacement. [Redis][] will return an `overflow` error when the score overflows.
772788
773789
- High concurrency or long-running decorated functions may result in unexpected cache misses and increased I/O operations. This can occur because the result value might not be saved quickly enough before the next call can hit the cache again.

0 commit comments

Comments
 (0)