You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(cache): add per-invocation TTL support and improve documentation
- Add per-invocation TTL feature for cached items
- Update README with detailed explanations of TTL and per-invocation TTL
- Refactor cache.py to support new TTL functionality
- Update CHANGELOG with new features and breaking changes
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,7 @@
6
6
7
7
- ✨ **New Features:**
8
8
- Added arguments excluding support for the `RedisFuncCache` class, which makes it possible to cache functions with arguments that cannot be serialized.
9
+
- Added support for per-invocation TTL.
9
10
10
11
- 💔 **Breaking Changes:**
11
12
- Rename `redis_func_cache.mixins.policies` to `redis_func_cache.mixins.scripts`.
Copy file name to clipboardExpand all lines: README.md
+36-4Lines changed: 36 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -368,20 +368,52 @@ Policies that support both clusters and store cache in multiple [Redis][] key pa
368
368
369
369
The [`RedisFuncCache`][] instance has two arguments to control the maximum size and expiration time of the cache:
370
370
371
-
- `maxsize`: the maximum number of items that the cache can hold.
371
+
- `maxsize`: The maximum number of items the cache can hold.
372
372
373
373
When the cache reaches its `maxsize`, adding a new item will cause an existing cached item to be removed according to the eviction policy.
374
374
375
375
> ℹ️ **Note:**\
376
376
> For "multiple" policies, each decorated function has its own standalone data structure, so the value represents the maximum size of each individual data structure.
377
377
378
+
This argument can be set when creating a cache instance:
- `ttl`: The expiration time (in seconds) for the cache data structure.
379
385
380
-
The cache's [Redis][] data structure will expire and be released after the specified time.
381
-
Each time the cache is accessed, the expiration time will be reset.
386
+
The entire [Redis][] data structure for the cache will expire and be removed after the specified time.
387
+
Each time the cache is accessed, its expiration time is refreshed. Thus, the cache will only be removed if it is not accessed within the specified period.
382
388
383
389
> ℹ️ **Note:**\
384
-
> For "multiple" policies, each decorated functionhas its own standalone data structure, so the `ttl` value represents the expiration time of each individual data structure. The expiration time will be reset each time the cache is accessed individually.
390
+
> For "multiple" policies, each decorated function has its own separate data structure, so the `ttl` value applies to each individual structure. The expiration time is refreshed independently whenever each cache is accessed.
391
+
392
+
You can set this argument when creating a cache instance:
- per-invocation TTL: The expiration time (in seconds) for each cached item, not the entire cache.
399
+
400
+
You can set this argument when decorating a function:
401
+
402
+
```python
403
+
@cache(ttl=300)
404
+
def my_func(x):
405
+
...
406
+
```
407
+
408
+
This means that each cached return value for a specific invocation of `my_func` will expire after 300 seconds.
409
+
The argument's default value is `None`, which means that the cache item will never expire.
410
+
411
+
> ⁉️ **Caution:**\
412
+
> This expiration relies on [Redis Hashes Field expiration](https://redis.io/docs/latest/develop/data-types/hashes/#field-expiration). Expiration only applies to the hash field (the cached return value), and does **not** reduce the total number of items in the cache when a field expires.
413
+
> Typically, the cached return value in the HASH portion is automatically released after expiration. However, the corresponding hash key in the ZSET portion is **not** removed automatically. Instead, it is only "lazily" cleaned up when accessed, or removed by the eviction policy when a new value is added. During this period, the ZSET portion continues to occupy memory, and the reported number of cache items does not decrease.
414
+
415
+
> ⚠️ **Warning:**\
416
+
> This feature requires [Redis][] Open Source version 7.4 or later.
0 commit comments