def _invalidate_cache(self, instance): """ Explicitly set a None value instead of just deleting so we don't have any race conditions where: Thread 1 -> Cache miss, get object from DB Thread 2 -> Object saved, deleted from cache Thread 1 -> Store (stale) object fetched from DB in cache Five second should be more than enough time to prevent this from happening for a web app. """ cache.set(instance.cache_key, None, 5)
def invalidate_cache(obj, field): cache.set(obj._get_cache_key(field=field), None, 5)