Django:通过管理面板更改数据时自动使缓存无效?

时间:2021-01-12 00:21:13

On a roll with Django questions today.

今天就Django提问。

The caching framework looks pretty awesome and I'd like to use it sitewide. Rather than set an explicit expiry time for my views, I'd prefer to cache them indefinitely and only invalidate/delete the cache when the content changes. Dream scenario, right?

缓存框架看起来非常棒,我想在整个网站上使用它。我不想为我的视图设置明确的到期时间,而是希望无限期地缓存它们,并且只在内容更改时使缓存无效/删除。梦想的场景吧?

Is there some way to hook into Django's automatic admin so that when a CRUD operation happens, the relevant cache gets deleted? I expect I'd have to somehow tell the admin panel which model should invalidate which class, but in principle, is this possible? Some kind of callback I can add? Any alternatives?

有没有办法挂钩Django的自动管理员,以便在CRUD操作发生时,相关的缓存被删除?我希望我必须以某种方式告诉管理面板哪个模型应该使哪个类无效,但原则上,这可能吗?我可以添加某种回调吗?任何替代品?

thanks! Matt

谢谢!马特

1 个解决方案

#1


6  

Two part answer:

两部分答案:

  1. Clear cache on a CRUD event? Easy as pie — use Django signals.

    清除CRUD事件的缓存?像饼一样容易 - 使用Django信号。

  2. Clear only the relevant parts of the cache? This is a genuinely hard problem. On the surface it may look straightforward, but the dependencies can be very difficult to discern for all but the most trivial cases.

    只清除缓存的相关部分?这是一个真正难以解决的问题。从表面上看,它可能看起来很简单,但除了最微不足道的情况之外,除了最简单的情况之外,依赖性很难辨别。

We sort of solved part 2 by extending the django caching code to embed object class/id info into the name, and then caching at a sub-page level. On a CRUD event we could do a simple regexp through the cached item names and prune as needed.

我们通过扩展django缓存代码以将对象类/ id信息嵌入到名称中,然后在子页面级别进行缓存来解决第2部分。在CRUD事件中,我们可以通过缓存的项目名称进行简单的正则表达式,并根据需要进行修剪。

All in all, I think it was yet another case of Premature Optimization and it's not at all clear that it made any difference. Next time I'll wait until there is a proven, measurable performance problem before doing something like this.

总而言之,我认为这是另一个早熟优化的案例,并不是很明显它没有任何区别。下次我会等到有一个经过验证的,可测量的性能问题之后再做这样的事情。

#1


6  

Two part answer:

两部分答案:

  1. Clear cache on a CRUD event? Easy as pie — use Django signals.

    清除CRUD事件的缓存?像饼一样容易 - 使用Django信号。

  2. Clear only the relevant parts of the cache? This is a genuinely hard problem. On the surface it may look straightforward, but the dependencies can be very difficult to discern for all but the most trivial cases.

    只清除缓存的相关部分?这是一个真正难以解决的问题。从表面上看,它可能看起来很简单,但除了最微不足道的情况之外,除了最简单的情况之外,依赖性很难辨别。

We sort of solved part 2 by extending the django caching code to embed object class/id info into the name, and then caching at a sub-page level. On a CRUD event we could do a simple regexp through the cached item names and prune as needed.

我们通过扩展django缓存代码以将对象类/ id信息嵌入到名称中,然后在子页面级别进行缓存来解决第2部分。在CRUD事件中,我们可以通过缓存的项目名称进行简单的正则表达式,并根据需要进行修剪。

All in all, I think it was yet another case of Premature Optimization and it's not at all clear that it made any difference. Next time I'll wait until there is a proven, measurable performance problem before doing something like this.

总而言之,我认为这是另一个早熟优化的案例,并不是很明显它没有任何区别。下次我会等到有一个经过验证的,可测量的性能问题之后再做这样的事情。