django-cache-memoize icon indicating copy to clipboard operation
django-cache-memoize copied to clipboard

Automatically (with help) invalidate cache when code changes

Open jdavid opened this issue 6 years ago • 5 comments

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.

jdavid avatar Aug 28 '19 11:08 jdavid

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) 

peterbe avatar Aug 29 '19 14:08 peterbe

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.

peterbe avatar Aug 29 '19 14:08 peterbe

Yes. It may be as well a more generic option (e.g. suffix), just something that is added to the key.

jdavid avatar Aug 30 '19 10:08 jdavid

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.

pySilver avatar Mar 30 '21 02:03 pySilver

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.

utapyngo avatar Aug 30 '21 03:08 utapyngo