gpt4 book ai didi

python - 从 Django 的缓存中删除特定项目?

转载 作者:太空狗 更新时间:2023-10-29 20:22:15 26 4
gpt4 key购买 nike

我正在通过 memcached 使用站点范围的缓存作为后端。我想在基础数据库对象更改时使缓存中的页面无效。

如果页面名称更改,那么我将使整个缓存失效(因为它会影响每个页面上的导航。笨拙但足以满足我的需求。

如果只有页面内容发生变化,那么我想使该页面的缓存无效。

有没有简单的方法来做到这一点?

最佳答案

我没有用 Django 做很多缓存,但我想你想要的是 signals .

您可以在底层对象上设置一个post_save 信号,并让回调函数使缓存中的该页面无效。

from django.core.signals import post_save
from django.core.cache import cache

def invalidate_cache(sender, **kwargs):
# invalidate cache
cache.delete(sender.get_absolute_url()) # or any other pertinent keys

post_save.connect(invalidate_cache, sender=UnderlyingModel)

这应该在项目更新时从缓存中正确删除它。

关于python - 从 Django 的缓存中删除特定项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/720800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com