你可以在Redis中使hashKey的键值对失效吗?

时间:2015-04-30 04:59:58

标签: java redis key-value jedis

我想在redis中使hashKey中的特定键/值对设置到期。但redis会使整个hashKey失效,并且所有keyValue对都会丢失。例如,我只想从seqNu中删除Key:666。使用jedis.setex是另一种选择,但你不能在其中设置hashKey。

jedis.hset("seqNu","666",System.currentTimeMillis()+"");
jedis.hset("seqNu","777",System.currentTimeMillis()+"");
jedis.expire("seqNu", 20); // This expires the whole HashKey: seqNu after 20 seconds 

1 个答案:

答案 0 :(得分:4)

Redis只会过期"整个"密钥,而不是其数据结构中的单个元素(即,哈希的字段或集合的成员)。考虑将Hash拆分为多个字符串键(每个键都可以自行删除)或使用排序集,如下所述:Redis: To set timeout for a key value pair in Set