caching - Redis缓存更新

标签 caching redis

EDIT2:澄清:代码已经在未命中逻辑上刷新缓存。我正在尝试做的是减少错过缓存命中的次数。

我正在使用 Redis 作为 API 的缓存。这个想法是,当 API 收到调用时,它首先检查缓存,如果数据不在缓存中,API 将获取它并缓存它以备下次使用。

目前配置如下:

maxmemory 50mb
maxmemory-policy allkeys-lru

也就是说,最多使用 50mb 内存,不断尝试在其中尝试 key ,当内存已满时通过删除最近最少使用的 key (lru) 开始。

现在我想介绍第二类键。对于第二个类别,我将设置一个特定的到期时间。现在我想建立一种机制,当这些 key 到期时,该机制会启动并刷新它们(并设置新的到期时间)。

我该怎么做?

编辑: 一些进展。事实证明,Redis 有一个 pub/sub 消息系统,特别是可以在事件上发送消息。其中之一是过期 key ,可以这样启用:

notify-keyspace-events Ex

我发现这段代码可以描述一个阻塞 python process subscribing to Redis' messaging system .它可以很容易地更改为检测 key 过期并在 key 过期时调用 API,然后 API 将刷新 key 。

def work(self, item):
    requests.get('http://apiurl/?q={param}'.format(param=item['data']))

所以这正是我要问的。

通常,这感觉太危险且无法控制。我可以想象很多不同的情况,在这些情况下这会很快失败。

那么,什么是更好的解决方案?

最佳答案

http://redis.io/topics/notifications

Keyspace notifications allows clients to subscribe to Pub/Sub channels in order to receive events affecting the Redis data set in some way. Examples of the events that is possible to receive are the following:

All the keys expiring in the database 0. (e.g)

...

EXPIRE generates an expire event when an expire is set to the key, or a expired event every time setting an expire results into the key being deleted (see EXPIRE documentation for more info).

关于caching - Redis缓存更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31394739/

相关文章:

ruby - 如何在 Ruby/Rails 中缓存/序列化 Net::IMAP 对象?

hash - 在 LRU 上删除 Redis Hash Key 的字段

typescript - 使用 NestJS 基于模式获取多个 Redis 缓存键

java - 是否可以将 EVAL SHA 与 spring-data redis 一起使用?

caching - 如何在 Grails 1.2.5 上清除 Ivy 缓存

java - Java 声明式(基于注释)缓存解决方案

安卓缓存管理器

javascript - 服务 worker : Return cached response first and fetch response async

ruby-on-rails - 将 Paperclip 与 rails-redis 一起使用时为 "IOError - closed stream"

redis - 在 Redis 中,如何通过从第二个排序集中排除记录来将记录与第一个集分开。?