简体   繁体   中英

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',
     }
}

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. eg:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM