beaker icon indicating copy to clipboard operation
beaker copied to clipboard

Cache decorator: option to explicitely ignore some parameters by name

Open sapristi opened this issue 4 years ago • 6 comments

Hello, we are in the need of a feature where we could explicitly ignore some arguments of a cached function (by argument name). Would you be interested in integrating such a feature into beaker ?

What are the current requirements for a pull request ?

  • supported python versions
  • anything else ?

Thank you/

sapristi avatar Jan 14 '21 09:01 sapristi

I could see this feature as including 2 keyword arguments, each taking a list: include_kwargs exclude_kwargs Where only one of these should be supplied. That makes sense to me. Python versions supported should be the same as beaker currently supports, unless there's some reason it can only work with a newer Python.

bbangert avatar Jan 14 '21 17:01 bbangert

That's nice to hear !

How would you integrate the current skip_self behaviour ? I can think of two ways:

  • only activated when neither include_kwargs nor exclude_kwargs are specified
  • only activated when specifying exclude_kwargs

I think it could be good to provide a dont_skip_self switch as well.

Also as you can see in the current PR: https://github.com/bbangert/beaker/pull/202 it is possible to exclude args as well as kwargs, so maybe include_kwargs is not exactly the right name for the parameter.

sapristi avatar Jan 15 '21 08:01 sapristi

Hello, any news on this issue on your side ?

After thinking a bit more about my pull request, I think that changing the way cache keys are computed might not be such a good idea. Maybe a better approach would be to only use the new method if include_args or exclude_args are provided ?

sapristi avatar Jan 21 '21 14:01 sapristi

@bbangert @sapristi Any chance to see #202 being merged? This looks exactly like the feature I'm looking for, and seemed to work according to the last tests on the PR.

fmigneault avatar Dec 07 '21 18:12 fmigneault

Haha I've completely given up on this (was even for a previous job). Turned out beaker was not the best tool for what we were doing, we ended using http://www.grantjenks.com/docs/diskcache/ and a custom wrapper.

sapristi avatar Dec 07 '21 19:12 sapristi

I added some additional packaging to him, achieving my goal


redis_config = commonConfig["redis"]
cache_opts = {
    'cache.type': 'ext:redis',
    'cache.url': f"redis://default:{redis_config['password']}@{redis_config['host']}:{redis_config['port']}",
    'cache.prefix': "file"
}

_cache = CacheManager(**cache_opts)


def cache(*cache_args, **cache_kwargs):
    def decorator(f):
        @wraps(f)
        def decorated(*args, **kwargs):
            __args = []
            for arg in args:
                if not isinstance(arg, Session):
                    __args.append(arg)

            @_cache.cache(*cache_args, **cache_kwargs)
            def __cache(*_args, **_kwargs):
                return f(*args, **kwargs)

            return __cache(*__args, **kwargs)
        return decorated
    return decorator

werct avatar Oct 09 '23 05:10 werct