Skip to content

Commit

Permalink
doc: doctest for mk_memoizer
Browse files Browse the repository at this point in the history
  • Loading branch information
thorwhalen committed Apr 16, 2024
1 parent 926293b commit cb7861e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions dol/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ def get_cache(cache):

# The following is a "Cache-Aside" read-cache with NO builtin cache update or refresh mechanism.
def mk_memoizer(cache):
"""
Make a memoizer that caches the output of a getter function in a cache.
Note: This is a specialized memoizer for getter functions/methods, i.e.
functions/methods that have the signature (instance, key) and return a value.
:param cache: The cache to use. Must have __getitem__ and __setitem__ methods.
:return: A memoizer that caches the output of the function in the cache.
>>> cache = dict()
>>> @mk_memoizer(cache)
... def getter(self, k):
... print(f"getting value for {k}...")
... return k * 10
...
>>> getter(None, 2)
getting value for 2...
20
>>> getter(None, 2)
20
"""
def memoize(method):
@wraps(method)
def memoizer(self, k):
Expand Down

0 comments on commit cb7861e

Please sign in to comment.