Login

very simplistic url cache flushing

Author:
jpt
Posted:
August 4, 2008
Language:
Python
Version:
.96
Score:
1 (after 3 ratings)

This is an (overly simple) example of how to manually delete something from the cache.

Usage is simply delete_url_cache('/some_view/')

This most likely doesn't work when using Vary headers but does seem to work fine for the most general case of simply needing to programmatically flush the cache after certain actions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from django.conf import settings
from django.core.cache import cache
from django.utils.encoding import iri_to_uri

def delete_url_cache(url):
    """ simple method to delete the cache for a particular URL """
    key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
    cache_key = 'views.decorators.cache.cache_header.%s.%s' % (key_prefix,
                                                               iri_to_uri(url))
    cache.delete(cache_key)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

jpt (on August 5, 2008):

oops, it certainly is.. my mistake not seeing if anything had been submitted since last time i checked

#

Please login first before commenting.