Skip to content

Commit 455230e

Browse files
committed
docs(cache): update parameter names for excluding arguments from cache key
- Rename `exclude_positional_args` to `exclude_args_indices` - Rename `exclude_keyword_args` to `exclude_args_names` - Update README.md to reflect new parameter names - Update cache.py to use new parameter names
1 parent da0f615 commit 455230e

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
-**New Features:**
88
- Added arguments excluding support for the `RedisFuncCache` class, which makes it possible to cache functions with arguments that cannot be serialized.
99

10+
- 💔 **Breaking Changes:**
11+
- Rename `redis_func_cache.mixins.policies` to `redis_func_cache.mixins.scripts`.
12+
1013
## v0.4
1114

1215
> 📅 2025-06-24

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ However, you can work around this issue by:
728728
729729
- Splitting the function into two parts: one with fully serializable arguments (apply the cache decorator to this part), and another that may contain un-serializable arguments (this part calls the first one).
730730
731-
- Using `exclude_keyword_args` or `exclude_positional_args` to exclude un-serializable arguments from key and hash calculations.
731+
- Using `exclude_args_names` or `exclude_args_indices` to exclude un-serializable arguments from key and hash calculations.
732732
733733
This approach is particularly useful for functions such as a database query.
734734
@@ -737,20 +737,21 @@ For example, the `pool` argument in the following function is not serializable:
737737
```python
738738
def get_book(pool: ConnectionPool, book_id: int):
739739
connection = pool.get_connection()
740-
return connection.execute("SELECT * FROM books WHERE book_id = %s", book_id)
740+
book = connection.execute("SELECT * FROM books WHERE book_id = %s", book_id).fetchone()
741+
return json.dumps(book.to_dict())
741742
```
742743
743744
However, we can exclude the `pool` argument from the key and hash calculations, so the function can be cached:
744745
745746
```python
746-
@cache(exclude_keyword_args=["pool"])
747+
@cache(exclude_args_names=["pool"])
747748
def get_book(pool: ConnectionPool, book_id: int):
748749
...
749750
```
750751
751752
Important:
752-
- When using the `exclude_keyword_args` parameter, ensure that the argument is passed using keyword way.
753-
- When using the `exclude_positional_args` parameter, ensure that the argument is passed using positional way.
753+
- When using the `exclude_args_names` parameter, ensure that the argument is passed in a keyword way.
754+
- When using the `exclude_args_indices` parameter, ensure that the argument is passed in a positional way.
754755
755756
## Known Issues
756757

src/redis_func_cache/cache.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,11 @@ def _before_get(
446446
user_function,
447447
user_args,
448448
user_kwds,
449-
exclude_positional_args: Optional[Sequence[int]] = None,
450-
exclude_keyword_args: Optional[Sequence[str]] = None,
449+
exclude_args_indices: Optional[Sequence[int]] = None,
450+
exclude_args_names: Optional[Sequence[str]] = None,
451451
):
452-
args = [x for i, x in enumerate(user_args) if i not in (exclude_positional_args or [])]
453-
kwds = {k: v for k, v in user_kwds.items() if k not in (exclude_keyword_args or {})}
452+
args = [x for i, x in enumerate(user_args) if i not in (exclude_args_indices or [])]
453+
kwds = {k: v for k, v in user_kwds.items() if k not in (exclude_args_names or {})}
454454
keys = self.policy.calc_keys(user_function, args, kwds)
455455
hash_value = self.policy.calc_hash(user_function, args, kwds)
456456
ext_args = self.policy.calc_ext_args(user_function, args, kwds) or ()
@@ -463,8 +463,8 @@ def exec(
463463
user_kwds: Mapping[str, Any],
464464
serialize_func: Optional[SerializerT] = None,
465465
deserialize_func: Optional[DeserializerT] = None,
466-
exclude_positional_args: Optional[Sequence[int]] = None,
467-
exclude_keyword_args: Optional[Sequence[str]] = None,
466+
exclude_args_indices: Optional[Sequence[int]] = None,
467+
exclude_args_names: Optional[Sequence[str]] = None,
468468
**options,
469469
):
470470
"""Execute the given user function with the provided arguments.
@@ -488,7 +488,7 @@ def exec(
488488
if not (isinstance(script_0, redis.commands.core.Script) and isinstance(script_1, redis.commands.core.Script)):
489489
raise RuntimeError("Can not eval redis lua script in asynchronous mode on a synchronous redis client")
490490
keys, hash_value, ext_args = self._before_get(
491-
user_function, user_args, user_kwds, exclude_positional_args, exclude_keyword_args
491+
user_function, user_args, user_kwds, exclude_args_indices, exclude_args_names
492492
)
493493
cached_return_value = self.get(script_0, keys, hash_value, self.ttl, options, ext_args)
494494
if cached_return_value is not None:
@@ -505,8 +505,8 @@ async def aexec(
505505
user_kwds: Mapping[str, Any],
506506
serialize_func: Optional[SerializerT] = None,
507507
deserialize_func: Optional[DeserializerT] = None,
508-
exclude_positional_args: Optional[Sequence[int]] = None,
509-
exclude_keyword_args: Optional[Sequence[str]] = None,
508+
exclude_args_indices: Optional[Sequence[int]] = None,
509+
exclude_args_names: Optional[Sequence[str]] = None,
510510
**options,
511511
):
512512
"""Asynchronous version of :meth:`.exec`"""
@@ -517,7 +517,7 @@ async def aexec(
517517
):
518518
raise RuntimeError("Can not eval redis lua script in synchronous mode on an asynchronous redis client")
519519
keys, hash_value, ext_args = self._before_get(
520-
user_function, user_args, user_kwds, exclude_positional_args, exclude_keyword_args
520+
user_function, user_args, user_kwds, exclude_args_indices, exclude_args_names
521521
)
522522
cached = await self.aget(script_0, keys, hash_value, self.ttl, options, ext_args)
523523
if cached is not None:
@@ -532,8 +532,8 @@ def decorate(
532532
user_function: Optional[CallableTV] = None,
533533
/,
534534
serializer: Optional[SerializerSetterValueT] = None,
535-
exclude_positional_args: Optional[Sequence[int]] = None,
536-
exclude_keyword_args: Optional[Sequence[str]] = None,
535+
exclude_args_indices: Optional[Sequence[int]] = None,
536+
exclude_args_names: Optional[Sequence[str]] = None,
537537
**options,
538538
) -> CallableTV:
539539
"""Decorate the given function with caching.
@@ -549,11 +549,11 @@ def decorate(
549549
550550
If assigned, it overwrite the :attr:`serializer` property of the cache instance on the decorated function.
551551
552-
exclude_positional_args: A list of positional argument indices to exclude from cache key generation.
552+
exclude_args_indices: A list of positional argument indices to exclude from cache key generation.
553553
554554
These arguments will be filtered out before cache operations.
555555
556-
exclude_keyword_args: A list of keyword argument names to exclude from cache key generation.
556+
exclude_args_names: A list of keyword argument names to exclude from cache key generation.
557557
558558
These parameters will be filtered out before cache operations.
559559
@@ -625,8 +625,8 @@ def wrapper(*user_args, **user_kwargs):
625625
user_kwargs,
626626
serialize_func,
627627
deserialize_func,
628-
exclude_positional_args,
629-
exclude_keyword_args,
628+
exclude_args_indices,
629+
exclude_args_names,
630630
**options,
631631
)
632632

@@ -638,8 +638,8 @@ async def awrapper(*user_args, **user_kwargs):
638638
user_kwargs,
639639
serialize_func,
640640
deserialize_func,
641-
exclude_positional_args,
642-
exclude_keyword_args,
641+
exclude_args_indices,
642+
exclude_args_names,
643643
**options,
644644
)
645645

0 commit comments

Comments
 (0)