Clearing specific cache in Django

前端 未结 3 1032
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 05:33

I am using view caching for a django project.

It says the cache uses the URL as the key, so I\'m wondering how to clear the cache of one of the keys if a user update

3条回答
  •  情歌与酒
    2020-12-30 05:46

    From django cache docs, it says that cache.delete('key') should be enough. So, it comes to my mind two problems you might have:

    1. Your imports are not correct, remember that you have to import cache from the django.core.cache module:

      from django.core.cache import cache
      
      # ...
      cache.delete('my_url')
      
    2. The key you're using is not correct (maybe it uses the full url, including "domain.com"). To check which is the exact url you can go into your shell:

      $ ./manage.py shell
      >>> from django.core.cache import cache
      >>> cache.has_key('/post/1234/')
      # this will return True or False, whether the key was found or not
      # if False, keep trying until you find the correct key ...
      >>> cache.has_key('domain.com/post/1234/') # including domain.com ?
      >>> cache.has_key('www.domain.com/post/1234/') # including www.domain.com ?
      >>> cache.has_key('/post/1234') # without the trailing / ?
      

提交回复
热议问题