Cache invalidation on code fail? #40
-
I would be glad to see an example of how I can reset the cache if another method of the class gets an error. |
Beta Was this translation helpful? Give feedback.
Answered by
simon-liebehenschel
Feb 17, 2022
Replies: 1 comment 1 reply
-
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):
... |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
FyZzyss
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: