Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sapristi opened this issue Jan 14, 2021 · 6 comments
Open

Comments

@sapristi
Copy link

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/

@bbangert
Copy link
Owner

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
Copy link
Author

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: #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
Copy link
Author

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
Copy link

@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
Copy link
Author

sapristi commented Dec 7, 2021

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
Copy link

werct commented Oct 9, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants