如何使烧杯/塔式缓存中具有公共前缀的多个键失效?

发布于 2024-10-15 01:15:02 字数 479 浏览 11 评论 0原文

假设我有以下片段,它使用 search_term 缓存函数加载,大概是 limit 作为键。(在烧杯中,它被转换为我想的所有参数元组的字符串表示形式)

# Assuming a cache object is available like:
cache = CacheManager(dict_of_config_options)


def populate_things():

    @cache.cache('mycache', expire=15)
    def load(search_term, limit, offset):
        return load_the_data(search_term, limit, offset)

    return load('rabbits', 20, 0)

现在,如果我在数据库中进行一些插入并希望使所有内容无效与新更新的 search_term 关联的缓存数据,如何枚举所有缓存数据以便手动使它们失效?

suppose I have the following snippet which caches the function load using search_term, limit as key, presumably.(In beaker it's converted to string representation of tuple of all arguments I suppose)

# Assuming a cache object is available like:
cache = CacheManager(dict_of_config_options)


def populate_things():

    @cache.cache('mycache', expire=15)
    def load(search_term, limit, offset):
        return load_the_data(search_term, limit, offset)

    return load('rabbits', 20, 0)

Now if I do some insertion into the database and want to invalidate all cached data associated with the newly updated search_term, how can I enumerate all cached data so I can manually invalidate them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空宴 2024-10-22 01:15:02

使单个密钥无效是直接的:

cache.get_cache("mycache").remove_value(key="rabbits")

如果正如您的问题标题所述,需要使整堆密钥无效,我建议将它们放在单独的缓存中,然后清除整个缓存:

cache.get_cache("all_my_rabbit_stuff").clear()

您将需要一些仔细规划哪个缓存中的内容,这样

  • 在清除缓存时就不会出现数百万个小缓存
  • ,也不会丢弃一些仍然新鲜的数据

Invalidating a single key is straight-forward:

cache.get_cache("mycache").remove_value(key="rabbits")

If there's, as your question's title says, whole bunch of keys that need to be invalidated, I'd recommend to put them in separate cache and then clearing the whole cache:

cache.get_cache("all_my_rabbit_stuff").clear()

You'll need some careful planning what goes in which cache so that

  • you don't end up with millions of small caches
  • when wiping out a cache, you don't throw away some still-fresh data too
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文