开发者

How do I invalidate several keys with common prefix in beaker / pylons cache?

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 searc开发者_如何学JAVAh_term, how can I enumerate all cached data so I can manually invalidate them?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜