gpt4 book ai didi

django - 明确清除 django memcached flush/cache() 或删除特定的 per-view-cache 键

转载 作者:行者123 更新时间:2023-12-01 15:29:21 29 4
gpt4 key购买 nike

我正在使用 django 的 Memcached 和 per-view 缓存机制与 @cache_page(timeout)装饰器。

在特定的 View 函数中,我希望清除整个缓存。
我试过 cache.clear()cache._cache.flush_all()但似乎都没有刷新缓存。

有没有其他方法可以做到这一点,还是我做错了?

编辑:
我意识到完全清除缓存并不是一个好主意,而不是仅仅删除所需的键。但是,我不知道我用 @cache_page(timeout) 装饰的 View 的键我如何知道这些?在我获得 key 后,我只做一个cache.delete(key)?

谢谢!

最佳答案

有比刷新整个缓存更好的方法。见 cache versioning :

When you change running code that uses cached values, you may need to purge any existing cached values. The easiest way to do this is to flush the entire cache, but this can lead to the loss of cache values that are still valid and useful.

Django provides a better way to target individual cache values. Django's cache framework has a system-wide version identifier, specified using the VERSION cache setting. The value of this setting is automatically combined with the cache prefix and the user-provided cache key to obtain the final cache key.

关于django - 明确清除 django memcached flush/cache() 或删除特定的 per-view-cache 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11247251/

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