CouchDB 是否有与 Redis 相当的工具?到期?

发布于 2024-11-14 19:04:21 字数 284 浏览 7 评论 0原文

CouchDB 是否有像 Redis 中那样的过期功能?

Redis 过期示例:

#!/usr/bin/env python
import redis
redis_server = redis.Redis(host='localhost',port=5477,db=0)
r.set('cat','meow')
r.expire('cat',10)
# do some work and ten seconds later...
r.get('cat') # returns None

Does CouchDB have an equivalent to expire like in Redis?

Example for Redis expire:

#!/usr/bin/env python
import redis
redis_server = redis.Redis(host='localhost',port=5477,db=0)
r.set('cat','meow')
r.expire('cat',10)
# do some work and ten seconds later...
r.get('cat') # returns None

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

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

发布评论

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

评论(3

揪着可爱 2024-11-21 19:04:21

不,CouchDB 没有这个。

Redis 使用惰性方法并在检查密钥时删除它们,即使它们可能已经过期得更早。此外,正如 @antirez 指出的那样,Redis 会每秒左右删除一组随机的过期密钥,以控制数据库大小。

如果 CouchDB 本身不支持此功能,您可以在对象之上添加一个小层来完成此工作。添加过期字段,并在尝试检索对象时,确保过期时间是在将来。如果没有,请删除过期的对象。此外,由于已删除的对象必须持久存在(因此可以复制删除操作),因此您还需要定期查找已删除的文档和 清除它们

No. CouchDB does not have this.

Redis uses a lazy approach and deletes keys when they are inspected even though they may have expired much earlier. Also, as @antirez pointed out Redis will remove a random set of expired keys every second or so to keep the database size under control.

If CouchDB does not natively support this, you could add a tiny layer on top of your objects to do this work. Add an expiry field and when trying to retrieve objects, make sure expiry is in the future. If not, delete the expired objects. Furthermore, since deleted objects must persist (so the delete action can be replicated), you will also need to periodically find deleted documents and purge them.

烟织青萝梦 2024-11-21 19:04:21

好问题!简单的答案是“否”,但另一个答案是mu

惯用的 CouchDB 方法是在记录(文档)中包含 expires_at 时间戳。接下来有一个视图,按过期时间戳进行索引。客户端将查询以时间戳为键的视图,时间戳值大于或等于现在。结果将是所有有效文档的列表。

这需要客户端的时钟同步。如果您有一个中央权威服务器(这是一种非常常见的情况),一种简单的同步方法是让客户端 ping couch 并检查其 HTTP Date 标头。

Good question! The simple answer is "no" but another answer is mu.

The idiomatic CouchDB approach would be to have expires_at timestamps in the records (documents). Next have a view, indexed by expiration timestamp. Clients would query the view keyed on timestamp, with the timestamp value greater or equal to now. The result will be a list of all valid documents.

This requires clients' clocks to be synchronized. If you have one central, authoritative server (a very common situation), an easy way to synchronize is for clients to ping couch and check its HTTP Date header.

海的爱人是光 2024-11-21 19:04:21

不,这是 memcache/redis 功能。 CouchDB 是数据持久数据库。

No. This is memcache/redis feature. CouchDB is data-persistent db.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文