Clearing specific cache in Django
Asked Answered
P

5

29

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 updates/deletes the object.

An example: A user posts a blog post to domain.com/post/1234/ .. If the user edits that, i'd like to delete the cached version of that URL by adding some kind of delete cache command at the end of the view that saves the edited post.

I'm using:

@cache_page(60 * 60)
def post_page(....):

If the post.id is 1234, it seems like this might work, but it's not:

def edit_post(....):
    # stuff that saves the edits
    cache.delete('/post/%s/' % post.id)
    return Http.....
Parabolize answered 9/1, 2012 at 5:48 Comment(2)
My guess is that the keys you are using are incorrect. You can try using this script on your memcached server to list the keys. Once you have the appropriate key try the cache.delete(key) method again.Harwood
Here is the link to the updated django cache documents: Django CachesJunkojunkyard
P
35

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 / ?
    
Proficient answered 9/1, 2012 at 6:12 Comment(2)
This seems to be the accepted answer. What was the correct key? I do have the same issue and Icannot find the correct key.Penner
@Penner I had this issue as well. It turns out that django's cache_page decorator creates a key based off of the request object, which is a better way to explain it than the docs. So for example you'll end up with a key that looks like this when all is said and done: ":1:views.decorators.cache.cache_page..GET.3e144467194c80669ac0d860e0368097.ec0b8f79f7413e4479a39eb2bb0104f0.en-us.America/New_York". There is a more concise explanation hereFerreous
U
2

I had same problem and I found this solution.

if you are using this decorator:

django.views.decorators.cache.cache_page

you can use this function:

import hashlib
from typing import List
from django.core.cache import cache


def get_cache_keys_from_url(absolute_uri) -> List[str]:
    url = hashlib.md5(f"absolute_uri".encode('ascii'))
    return cache.keys(f'*{url.hexdigest()}*')

which you need to get cache keys for absolute_uri

and than use cache.delete_many(get_cache_keys_from_url(http://exemple.com/post/1234/)) for clear page with url - http://exemple.com/post/1234/

Uticas answered 14/9, 2022 at 13:56 Comment(0)
J
1

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
Jonson answered 18/9, 2018 at 19:43 Comment(0)
A
0

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.

Arnold answered 16/12, 2020 at 0:31 Comment(0)
A
0

With delete() and delete_many(), you can delete the specific cache values in LocMemCache as shown below. *delele() and delete_many() can delete the specific version of single cache value and multiple cache values respectively and my answer explains how to set and get cache values with LocMemCache and the answer of my question explains the default version of a cache value with LocMemCache:

from django.http import HttpResponse
from django.core.cache import cache

def test(request):
    cache.set("first_name", "John")
    cache.set("first_name", "David", version=2)
    cache.set("last_name", "Smith")
    cache.set("last_name", "Miller", version=2)
    cache.set("age", 36)
    cache.set("age", 42, version=2)

    cache.delete("first_name") # Delete "John"
    cache.delete("first_name", 2) # Delete "David"
    cache.delete_many(["last_name", "age"]) # Detele "Smith" and 36
    cache.delete_many(["last_name", "age"], 2) # Detele "Miller" and 42
    return HttpResponse("Test")

In addition, clear() can delete all cache values as shown below:

from django.http import HttpResponse
from django.core.cache import cache

def test(request):
    cache.set("first_name", "John")
    cache.set("first_name", "David", version=2)
    cache.set("last_name", "Smith")
    cache.set("last_name", "Miller", version=2)
    cache.set("age", 36)
    cache.set("age", 42, version=2)

    cache.clear() # Delete all

    return HttpResponse("Test")
Automatism answered 21/8, 2023 at 0:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.