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
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
@@ -8,6 +8,7 @@
8
8
- Added arguments excluding support for the `RedisFuncCache` class, which makes it possible to cache functions with arguments that cannot be serialized.
9
9
- Added support for per-invocation TTL.
10
10
-`RdisFuncCache.disabled()` provides a scope in which the cache is disabled temporarily.
11
+
- Added support for controlling cache TTL update behavior with `update_ttl` parameter.
11
12
12
13
- 💔 **Breaking Changes:**
13
14
- Rename `redis_func_cache.mixins.policies` to `redis_func_cache.mixins.scripts`.
Copy file name to clipboardExpand all lines: README.md
+48-7Lines changed: 48 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -464,6 +464,47 @@ Other serialization libraries such as [bson][], [simplejson](https://pypi.org/pr
464
464
> The [`pickle`][] module is highly powerful but poses a significant security risk because it can execute arbitrary code during deserialization. Use it with extreme caution, especially when handling data from untrusted sources.
465
465
> For best practices, it is recommended to cache functions that return simple, [JSON][]-serializable data. If you need to serialize more complex data structures than those supported by [JSON][], consider using safer alternatives such as [bson][], [msgpack][], or [yaml][].
466
466
467
+
### TTL Update Behavior
468
+
469
+
By default, accessing cached data updates the expiration time (TTL) of the cache data structures. This behavior is referred to as "sliding TTL". However, you can control this behavior using the `update_ttl` parameter.
470
+
471
+
There are two TTL update modes:
472
+
473
+
- **Sliding TTL** (`update_ttl=True`, default): Each cache access (both read and write) extends the life of the cache data structures.
474
+
- **Fixed TTL** (`update_ttl=False`): The expiration time is set only when the cache data structures are first created, and subsequent accesses do not extend their life.
475
+
476
+
You can set the `update_ttl` parameter at the cache instance level:
# Override to use fixed TTL for this specific function
492
+
@cache(update_ttl=False)
493
+
def my_func(x):
494
+
...
495
+
496
+
# Override to use sliding TTL for this specific function
497
+
@cache(update_ttl=True)
498
+
def another_func(x):
499
+
...
500
+
```
501
+
502
+
The `update_ttl` parameter controls the behavior of the cache data structures (sorted set and hash map) as a whole, not individual cached items. It is independent of the per-item TTL (set via the `ttl` parameter in the decorator), which controls the expiration of individual cached functionresults.
503
+
504
+
> 💡 **Tip:** \
505
+
> Use fixed TTL when you want predictable cache expiration behavior, regardless of how often cached functions are accessed.
506
+
> Use sliding TTL when you want frequently accessed cached data to remain in cache longer.
507
+
467
508
## Advanced Usage
468
509
469
510
### Custom result serializer
@@ -657,7 +698,7 @@ The serializer and decoder are defined in the `__hash_config__` attribute of the
0 commit comments