Skip to content

Cache invalidation on code fail? #40

Discussion options

You must be logged in to vote

I never think about such cache use cases. The first thing that comes to my mind is using a decorator that catches exceptions and performs any callbacks you need when an error happens. Simplified example:

cache_backend = AnyCacheBackendYouWant()


def clear_cache_on_error(function): 
      # I do not write full decorator's body for readability
        
        try:
            return await function(*args, **kwargs)
        except Exception as error:
               # do anything you need...
               await cache_backend.clear()
               raise



class YourClass:
    
    @cached(cache=cache_backend)
    @clear_cache_on_error
    def func(self):
        ...

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@FyZzyss
Comment options

Answer selected by FyZzyss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants