Invalidating Memcached Keys on save() in Django -


i've got view in django uses memcached cache data more highly trafficked views rely on relatively static set of data. key word relatively: need invalidate memcached key particular url's data when it's changed in database. clear possible, here's meat an' potatoes of view (person model, cache django.core.cache.cache):

def person_detail(request, slug):      if request.is_ajax():         cache_key = "%s_about_%s" % settings.site_prefix, slug          # check cache see if we've got result made.         json_dict = cache.get(cache_key)          # cache hit?         if json_dict none:             # that's negative ghost rider             person = get_object_or_404(person, display = true, slug = slug)              json_dict = {                 'name' : person.name,                 'bio' : person.bio_html,                 'image' : person.image.extra_thumbnails['large'].absolute_url,             }              cache.set(cache_key)          # json_dict exist, whether it's cache or not         response = httpresponse()         response['content-type'] = 'text/javascript'         response.write(simpljson.dumps(json_dict)) # make sure it's formatted js using simplejson         return response     else:         # templated response generated 

what want @ cache_key variable in it's "unformatted" form, i'm not sure how this--if can done @ all.

just in case there's this, here's want (this person model's hypothetical save method)

def save(self):         # if update, key cached, otherwise won't, let's see if can't find me     try:         old_self = person.objects.get(pk=self.id)         cache_key = # voodoo magic variable         old_key = cache_key.format(settings.site_prefix, old_self.slug) # generate key cached         cache.delete(old_key) # hit both barrels of rock salt      # turns out  doesn't exist, let's make first request faster making cache right     except doesnotexist:         # haven't gotten yet.      super(person, self).save() 

i'm thinking making view class sorta stuff, , having functions in remove_cache or generate_cache since sorta stuff lot. better idea? if so, how call views in urlconf if they're in class?

urlconf should point callable. there's no strict requirement make point function exactly. implement base class cache methods extend it:

class realview(baseviewwithcachemethods):     def __call__(self, request):         if request.is_ajax():             return self.ajax_view()         return self.html_view() 

urlconf definition that:

from django.conf.urls.defaults import * views import realview  urlpattrens = patterns('',     (r'^$', realview()), ) 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -