Skip to content

Commit

Permalink
fix: Attempt at making cache_this chainable
Browse files Browse the repository at this point in the history
  • Loading branch information
thorwhalen committed Dec 14, 2024
1 parent 2d30b32 commit 7198ffb
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions dol/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ def __get__(self, instance, owner=None):
# If cache is False, always compute the value
return self.func(instance)

cache = self._get_cache(instance)

return self._get_or_compute(instance, cache)

def _get_cache(self, instance):
try:
cache = self.__get_cache(instance)
except (
Expand All @@ -159,7 +164,9 @@ def __get__(self, instance, owner=None):
f"instance to cache {self.attrname!r} property."
)
raise TypeError(msg) from None
return cache

def _get_or_compute(self, instance, cache):
val = cache.get(self.cache_key, _NOT_FOUND)
if val is _NOT_FOUND:
with self.lock:
Expand All @@ -180,6 +187,21 @@ def __get__(self, instance, owner=None):

__class_getitem__ = classmethod(GenericAlias)

# TODO: Time-boxed attempt to get a __call__ method to work with the class
# (so that you can chain two cache_this decorators, (problem is that the outer
# expects the inner to be a function, not an instance of CachedProperty, so
# tried to make CachedProperty callable).
# def __call__(self, instance):
# """
# Call the cached property.

# :param func: The function to be called.
# :return: The cached property.
# """
# cache = self._get_cache(instance)

# return self._get_or_compute(instance, cache)


def cache_this(
func: PropertyFunc = None,
Expand Down

0 comments on commit 7198ffb

Please sign in to comment.