在 Django 1.3.1 中过期视图缓存 [英] Expiring a view-cache in Django 1.3.1

查看:17
本文介绍了在 Django 1.3.1 中过期视图缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使模型的 post_save(通过 https://docs.djangoproject.com/en/1.3/topics/cache/?from=olddocs#the-per-视图缓存).我做了一些谷歌搜索,并在 SO 上找到了这个答案:在 Django 中使视图缓存过期? 但它对我不起作用.

I'm trying to expire a view-level cache on a model's post_save (that was set via https://docs.djangoproject.com/en/1.3/topics/cache/?from=olddocs#the-per-view-cache). I did some googling and found this answer here on SO: Expire a view-cache in Django? but it's not working for me.

我在 freenode 的 #django 房间四处询问,一致认为这可能是由于 最近在 1.3 中所做的缓存更改

I asked around in the #django room on freenode and the consensus was that this was probably due to the recent caching changes made in 1.3

有没有人知道我如何清除模型的缓存条目,该模型的键为 get_absolute_url()?

Does anyone have any idea on how I can wipe out the cache entry for a model keyed off it's get_absolute_url()?

推荐答案

我想通了!

干杯 ilvar 为我指明了正确的方向.我的实现如下.我创建了一个名为 cache_key 的属性,并将 post_save 接收器添加到模型的子类中,这些模型的视图级缓存在更新后需要清除.随时欢迎提出改进建议!

I figured it out!

Cheers to ilvar for pointing me in the right direction. My implementation is below. I created a property named cache_key and added a post_save receiver onto the sub class of the models whose view-level caches I needed to clear after they had been updated. Suggestions for improvement are always welcome!

from django.conf import settings
from django.core.cache import cache
from django.db.models.signals import post_save
from django.http import HttpRequest
from django.utils.cache import _generate_cache_header_key

from someapp.models import BaseModelofThisClass

class SomeModel(BaseModelofThisClass):
    ...
    @property
    def cache_key(self):
        # Create a fake request object
        request = HttpRequest()
        # Set the request method
        request.method = "GET"
        # Set the request path to be this object's permalink.
        request.path = self.get_absolute_url()
        # Grab the key prefix (if it exists) from settings
        key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
        # Generate this object's cache key using django's key generator
        key = _generate_cache_header_key(key_prefix, request)
        # This is a bit hacky but necessary as I don't know how to do it
        # properly otherwise. While generating a cache header key, django
        # uses the language of the HttpRequest() object as part of the
        # string. The fake request object, by default, uses
        # settings.LANGUAGE_CODE as it's language (in my case, 'en-us')
        # while the true request objects that are used in building views
        # use settings.LANGUAGES ('en'). Instead of replacing the segment
        # of the string after the fact it would be far better create a more
        # closely mirrored HttpRequest() object prior to passing it to
        # _generate_cache_header_key().
        key = key.replace(settings.LANGUAGE_CODE, settings.LANGUAGES[settings.DEFAULT_LANGUAGE][0])

        return key

    @receiver(post_save)
    def clear_cache_for_this_item(sender, instance, **kwargs):
        # If this is a sub-class of another model
        if sender not in BaseModelofThisClass.__subclasses__():
            return
        else:
            cache.delete(instance.cache_key)

这篇关于在 Django 1.3.1 中过期视图缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆