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

Wrap functions in class with decorator #625

Open
Dreamsorcerer opened this issue Jan 2, 2023 · 1 comment
Open

Wrap functions in class with decorator #625

Dreamsorcerer opened this issue Jan 2, 2023 · 1 comment
Milestone

Comments

@Dreamsorcerer
Copy link
Member

Dreamsorcerer commented Jan 2, 2023

The decorator should be simplified to a simple function, which should then return an object which wraps the passed function. Something along the lines of:

class Wrapper:
    def __init__(self, func):
        self.func = func

    def __call__(self, *args, **kwargs):
        # Get from cache...
        # Or call self.func(*args, **kwargs)

def cached(func):
    return Wrapper(func)

This simplifies the logic, and makes it easy to add methods on to our function (related: #538). For example, to call the function while forcing the cache to update could look something like:

@cached
def foo(...):
    ...

# Force update
foo.refresh(...)
@slingshotvfx
Copy link

+1 for this feature, it would be great (especially for unit testing) to be able to call a function without the cache interfering, just like async-lru's cache_invalidate() method

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

2 participants