Skip to content

Commit

Permalink
Improve redis_cache() documentation (#401)
Browse files Browse the repository at this point in the history
`redis_cache()` is more similar to `functools.cache()` than
`functools.lru_cache()`.
  • Loading branch information
brainix authored May 11, 2021
1 parent 6bd6393 commit ce60c8c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,21 +439,21 @@ Two caveats:

## redis_cache()

`redis_cache()` is a simple function return value cache, sometimes called
`redis_cache()` is a simple lightweight unbounded function return value cache,
sometimes called
[“memoize”](https://en.wikipedia.org/wiki/Memoization).
`redis_cache()` implements Python’s excellent
[`functools.lru_cache()`](https://docs.python.org/3/library/functools.html#functools.lru_cache)
[`functools.cache()`](https://docs.python.org/3/library/functools.html#functools.cache)
API as closely as is feasible. In other words, you can use `redis_cache()` the
same way that you use `functools.lru_cache()`.
same way that you use `functools.cache()`.

_Limitations:_

1. Arguments to the function must be hashable.
2. Return values from the function must be JSON serializable.
3. `functools.lru_cache()` allows for a maximum size and has an eviction
policy; `redis_cache()` has neither. This means that your function’s
return value cache can grow unbounded. Only use `redis_cache()` in any of
these cases:
3. Just like `functools.cache()`, `redis_cache()` does not allow for a maximum
size, and does not evict old values, and grows unbounded. Only use
`redis_cache()` in one of these cases:
1. Your function’s argument space has a known small cardinality.
2. You specify a `timeout` when calling `redis_cache()` to decorate your
function, to dump your _entire_ return value cache `timeout` seconds
Expand Down
4 changes: 2 additions & 2 deletions pottery/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@

def _raise_on_error(func: F) -> Callable[[F], F]:
@functools.wraps(func)
def wrap(*args: Any, **kwargs: Any) -> Any:
def wrapper(*args: Any, **kwargs: Any) -> Any:
try:
return func(*args, **kwargs)
except ResponseError as error:
raise IndexError('list assignment index out of range') from error
return wrap
return wrapper


class RedisList(Base, collections.abc.MutableSequence):
Expand Down
1 change: 1 addition & 0 deletions tests/test_doctests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def _modules(self):
'our doctests run too slowly',
)
def test_doctests(self):
'Run doctests and confirm that they work and are not science fiction'
for module in self._modules():
with self.subTest(module=module):
results = doctest.testmod(m=module)
Expand Down

0 comments on commit ce60c8c

Please sign in to comment.