Clearing specific cache in Django

前端 未结 3 932
没有蜡笔的小新
没有蜡笔的小新 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:42

    There's a trick that might work for this. The cache_page decorator takes an optional argument for key_prefix. It's supposed to be used when you have, say, multiple sites on a single cache. Here's what the docs say:

    CACHE_MIDDLEWARE_KEY_PREFIX – If the cache is shared across multiple sites using the same Django installation, set this to the name of the site, or some other string that is unique to this Django instance, to prevent key collisions. Use an empty string if you don’t care.

    But if you don't have multiple sites, you can abuse it thus:

    cache_page(cache_length, key_prefx="20201215")
    

    I used the date as the prefix so it's easy to rotate through new invalidation keys if you want. This should work nicely.

    However! Please note that if you are using this trick, some caches (e.g., the DB cache, filesystem cache, and probably others) do not clean up expired entries except when they're accessed. If you use the trick above, you won't ever access the cache entry again and it'll linger until you clear it out. Probably doesn't matter, but worth considering.

    0 讨论(0)
  • 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 / ?
      
    0 讨论(0)
  • 2020-12-30 06:03

    I make a function to delete key starting with some text. This help me to delete dynamic keys.

    list posts cached

    def get_posts(tag, page=1):
        cached_data = cache.get('list_posts_home_tag%s_page%s' % (tag, page))
        if not cached_data:
            cached_data = mycontroller.get_posts(tag, page)
            cache.set('list_posts_home_tag%s_page%s' % (tag, page), cached_data, 60)
        return cached_data
    

    when update any post, call flush_cache

    def update(data):
        response = mycontroller.update(data)
        flush_cache('list_posts_home')
        return response
    

    flush_cache to delete any dynamic cache

    def flush_cache(text):
        for key in list(cache._cache.keys()):
            if text in key:
                cache.delete(key.replace(':1:', ''))
    

    Do not forget to import cache from django

    from django.core.cache import cache
    
    0 讨论(0)
提交回复
热议问题