keithxm23
keithxm23

Reputation: 1280

Explicitly clear django memcached flush/cache() OR delete specific per-view-cache key

I'm using django's Memcached and the per-view caching mechanism with the @cache_page(timeout) decorator.

Inside a particular view function I wish to clear the entire cache. I tried cache.clear() and cache._cache.flush_all() but neither seem to flush the cache.

Is there some other way to do this or am I doing it wrong?

EDIT: I realize that completely clearing the cache is not such a good idea as opposed to just deleting the required keys. However, I do not know the keys for the views I've decorated with @cache_page(timeout) How do I get to know these? And after I acquire the key, I just do a cache.delete(key)?

Thanks!

Upvotes: 4

Views: 3680

Answers (1)

Mariusz Jamro
Mariusz Jamro

Reputation: 31653

There is be a better way than flushing the entire cache. See cache versioning:

When you change running code that uses cached values, you may need to purge any existing cached values. The easiest way to do this is to flush the entire cache, but this can lead to the loss of cache values that are still valid and useful.

Django provides a better way to target individual cache values. Django's cache framework has a system-wide version identifier, specified using the VERSION cache setting. The value of this setting is automatically combined with the cache prefix and the user-provided cache key to obtain the final cache key.

Upvotes: 1

Related Questions