Skip to content

Commit

Permalink
feat: cache=False option in cache_this
Browse files Browse the repository at this point in the history
  • Loading branch information
thorwhalen committed Jul 22, 2024
1 parent 9de6653 commit 022d8a4
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions dol/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def __get__(self, instance, owner=None):
raise TypeError(
'Cannot use _cache_this instance without calling __set_name__ on it.'
)
if self.cache is False:
# If cache is False, always compute the value
return self.func(instance)

try:
cache = self.__get_cache(instance)
except (
Expand Down Expand Up @@ -161,6 +165,22 @@ def cache_this(func=None, *, cache=None, key=None):
>>> obj.foo # so that the next time we access foo, it's returned from the cache.
42
Not that if you specify `cache=False`, you get a property that is computed
every time it's accessed:
>>> class NoCache:
... @cache_this(cache=False)
... def foo(self):
... print("In NoCache.foo...")
... return 42
...
>>> obj = NoCache()
>>> obj.foo
In NoCache.foo...
42
>>> obj.foo
In NoCache.foo...
42
Specify the cache as a dictionary that lives outside the instance:
Expand Down Expand Up @@ -262,12 +282,26 @@ def cache_this(func=None, *, cache=None, key=None):
b'\x80\x04K*.'
"""
# the cache is False case, where we just want a property, computed by func
if cache is False:
if func is None:

def wrapper(f):
return property(f)

return wrapper
else:
return property(func)

# The general case
# If func is not given, we want a decorator
if func is None:

def wrapper(f):
return _cache_this(f, cache=cache, key=key)

return wrapper
# If func is given, we want to return the _cache_this instance
else:
return _cache_this(func, cache=cache, key=key)

Expand Down

0 comments on commit 022d8a4

Please sign in to comment.