python - cached_property 何时重置?

标签 python django caching

目前我正在使用 @cached_property 来避免重复访问数据库。

class MyModel(models.Model):
    ...

    @cached_property
    def my_opts(self):
        options = Option.objects.filter(...)
        return options
    ...

我使用了这个属性前端模板。

它在 shell 和测试中运行良好。但是当我在浏览器中测试时,我不知道缓存的属性何时重置。

每当我刷新浏览器时,该属性都会重置。那么在这种情况下使用缓存属性有用吗?客户端方面的 cached_property 值何时重置?

提前致谢!

最佳答案

documentation很清楚:

The cached result will persist as long as the instance does, so if the instance is passed around and the function subsequently invoked, the cached result will be returned.

每当您刷新浏览器时,都会重新创建实例,从而使缓存失效。如果您希望缓存能够持续多个请求,则应考虑使用 cache framework .

关于python - cached_property 何时重置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54680910/

相关文章:

c - 提高缓存命中率

python - python中的嵌套if条件

Python Gekko 参数估计 : too long computational time because of "Dynamic Init C"

python - 如何在 python 中生成音符或和弦?

python - Python 或 Django 中的 CURL

python - 设置 pycharm 运行 django unittest

Python类装饰器扩展类导致递归

javascript - 确保在客户端更新 CSS/JavaScript

mysql - Django 1.8 迁移 "Table already exists"

javascript - 浏览器的 HTTP 缓存曾经用于存储 XMLHttpRequest 响应吗?