user14665310
user14665310

Reputation: 545

how can i invalidate page cache in django

How can I invalidate page cache in django. I am trying to cache a page displays a list that is unique for each user using django-rest-framework. I can not figure out how to invalidate the page cache when the another object is added, updated, deleted from the model.

I am open to using an external package or signals.

I don't know what the key is for @page_cache to invalidate the cache

I have tried to use django-fancy-cache but I just get an error

unsupported operand type(s) for +: 'float' and 'function'

my cache settings

CACHES = {
    'default': {
         'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
         'LOCATION': 'unique-snowflake',
     }
}

Upvotes: 0

Views: 1237

Answers (1)

Moein Babapour
Moein Babapour

Reputation: 104

You can move your invalidation logic to your models. For example, in order to invalidate a cache when an object is added/updated, you can override model's save method. e.g:

def save(self, *args, **kwargs):
    cache.delete(`YOUR_CACHE_KEY`)
    super().save(*args, **kwargs)

same for delete:

def delete(self, *args, **kwargs):
    cache.delete(`YOUR_CACHE_KEY`)
    super().delete(*args, **kwargs)

Upvotes: 0

Related Questions