django-cache-memoize
django-cache-memoize copied to clipboard
Automatically (with help) invalidate cache when code changes
Sometimes a change in the code should trigger the invalidation of the cache. The idea is to add the version optional parameter to cache_memoize, defaulting to None. Then the version, if not None, would be added to the cache key.
We could use it like this:
@cache_memoize(60)
def f(...):
Then we fix a bug in f and write:
@cache_memoize(60, version=1)
Another bug fixed (so many :bug: :beetle: ) :
@cache_memoize(60, version=2)
So, when the fixes are deployed the cache will be invalidated, instead of returning a wrong cached value.
I like that. One could perhaps use it like this:
from gitutils import get_current_git_sha
current_git_sha = get_current_git_sha()
@cache_memoize(60, version=current_git_sha)
That would, if you chose to use it like that, mean that a fresh new deployment triggers a different cache key for all memoized functions.
Yes. It may be as well a more generic option (e.g. suffix), just something that is added to the key.
version / suffix would be nice, it can be set somewhere in project settings globally so if one really wants - all cache will be cleared when such option changes.
I vote for version.
Django itself does support the version argument: https://docs.djangoproject.com/en/3.2/topics/cache/#cache-versioning
It should be easy to add just one more pass-through argument to cache_memoize.