bbangert / beaker

WSGI middleware for sessions and caching
https://beaker.readthedocs.org/
Other
517 stars 147 forks source link

Cache decorator: option to explicitely ignore some parameters by name #201

Open sapristi opened 3 years ago

sapristi commented 3 years ago

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 ?

Thank you/

bbangert commented 3 years ago

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.

sapristi commented 3 years ago

That's nice to hear !

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

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 commented 3 years ago

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 ?

fmigneault commented 2 years ago

@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.

sapristi commented 2 years ago

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.

werct commented 10 months ago

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