我可以在 redis 中设置全局 TTL 吗? [英] Can I set global TTL in redis?

查看:98
本文介绍了我可以在 redis 中设置全局 TTL 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 redis 中设置全局 TTL 吗?而不是每次设置密钥时都设置TTL.

Can I set global TTL in redis? Instead of setting TTL every time I set a key.

我用谷歌搜索,但找不到任何线索.所以好像做不到?

I googled, but cannot found any clue. So it seems cannot be done?

谢谢.

推荐答案

不,Redis 没有全局/默认 TTL 的概念,是的,您必须为每个键单独设置它.但是,根据您的要求和您尝试执行的操作,可能还有其他方法可以实现您的目标.换句话说,你为什么需要它?

No, Redis doesn't have a notion of a global/default TTL and yes, you do have to set it for each key independently. However, depending on your requirements and on what you're trying to do, there may be other ways to achieve your goal. Put differently, why do you need it?

例如,如果您想使用 Redis 作为缓存而不用担心必须删除旧"项,则只需将 maxmemory_policy 设置为 allkey-lru.每当 Redis 的内存耗尽时,这将驱逐最近最少使用的键.

For example, if you want to use Redis as a cache and not worry about having to remove "old" items, you can get simply by setting the maxmemory_policy to allkey-lru. This will evict the least recently used keys whenever Redis' memory is exhausted.

有关更多信息,请参阅@arganzheng 和@Kristján 下方评论中的有用链接,以及redis.conf 配置文件.

for more information, see the helpful links in the comments below from @arganzheng and @Kristján, as well as the inline documentation in the redis.conf configuration file.

这篇关于我可以在 redis 中设置全局 TTL 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆