Search code examples
pythonpython-3.xpython-decoratorsfunctools

Caching for special calls


How can you change the behavior of the decorator lru_cache from functools so that the decorated function has a flag that indicates whether to cache this call or not.
for example

@new_lru_cache
def f(args, kwargs):
    ...body function...

f(1) # not saved
f(2, cache=True) # saved

Solution

  • If you are gonna write a whole function for it, why not copy-paste the original source and implement your logic there?

    def lru_cache(maxsize=128, typed=False, cache=False):
        if isinstance(maxsize, int):
            if maxsize < 0:
                maxsize = 0
        elif callable(maxsize) and isinstance(typed, bool):
            user_function, maxsize = maxsize, 128
            wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
            return update_wrapper(wrapper, user_function)
        elif maxsize is not None:
            raise TypeError('Expected first argument to be an integer, a callable, or None')
    
        def decorating_function(user_function):
            if not cache:
                return user_function # We add here
            wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
            return update_wrapper(wrapper, user_function)
    
        return decorating_function
    

    This way you have even more control over the function, and you can see everything easily, and you can play around with the inners of the functools this way.