Clearing a specific cache in Django

I am using view caching for a django project.

It says that the cache uses the URL as a key, so I wonder how to clear the cache of one of the keys if the user updates / deletes the object.

Example: a user posts a blog post to domain.com/post/1234/. If the user edits this, I would like to delete the cached version of this URL by adding some cache delete command at the end of the view which saves the edited post.

I use:

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

If post.id is 1234, it looks like this might work, but it doesn't:

def edit_post(....):
    # stuff that saves the edits
    cache.delete('/post/%s/' % post.id)
    return Http.....
+5
source share
1 answer

django cache docs , cache.delete('key') . , :

  • , , cache django.core.cache:

    from django.core.cache import cache
    
    # ...
    cache.delete('my_url')
    
  • , , (, URL-, "domain.com" ). , URL :

    $ ./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 / ?
    
+14

All Articles