python - 在 Django 1.3.1 中使 View 缓存过期

标签 python django caching django-views

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

我在 freenode 的#django 房间里四处询问,一致认为这可能是由于 the recent caching changes made in 1.3 造成的。

有没有人知道我可以如何清除以 get_absolute_url() 键控的模型的缓存条目?

最佳答案

我想通了!

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

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)

关于python - 在 Django 1.3.1 中使 View 缓存过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10455438/

相关文章:

.net - 在 Silverlight 中维护 DB 对象的内存缓存的最佳方法

c++ - 存储和检索对游戏世界中卸载对象的引用

caching - Coldfusion 9 上的 EHCache - 我可以创建多个缓存或禁用它吗?

python - 使用 pyvmomi 在 vmware 中获取实例的实际使用(分配)磁盘空间

python - 结束后有空格 =""

python - Django:用户配置文件和用户注册——使用 bitbucket 模块还是 django 当前的 1.3 版本?

html - Django 模板继承导致总线错误

python - 操作系统错误 : socket no longer exists (Python IDLE error)

java - 如何检查一个数字的每个数字是否大于或等于另一个数字?

python - 为什么在我离开内置运行服务器后 Django 找不到我的管理媒体文件?