[Answer]-Compare and set using cache in django

1👍

If you are using the generic Django caching framework, this is not possible. If you are only using MemCache, this question on Django Memcache: Compare and Set contains all you need.

However, be advised that while CAS solves the problem of multiple users setting the same cache value, this is only a problem in high traffic environments. If simultaneous cache-miss-and-updates occur once or twice per month, you’d be better off ignoring CAS.

If the cached value is really expensive to calculate, you could also think of a separate background task that calculates the result, stores it in the cache, and runs just before the cache expiry. In this case, your users would never experience a cache miss and the entire CAS-issue becomes irrelevant.

Leave a comment