你可以在Redis中使hashKey的键值对失效吗? [英] Can you expire a key value pair for a hashKey in Redis

查看:2742
本文介绍了你可以在Redis中使hashKey的键值对失效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to expire a particular key/value pair set in a hashKey in redis. But redis expires the whole hashKey with all keyValue pairs inside lost. For example i just want to remove Key:666 from seqNu. Using jedis.setex is other option but u cant set a hashKey in it.

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 


推荐答案

Redis只会使整个键到期,而不是其数据结构中的单个元素(即Hash字段或Set的成员)。考虑将Hash拆分为多个字符串键(每个键都可以自行删除)或使用排序集,如下所述: Redis:设置设置中键值对的超时时间

Redis only expires "whole" keys, not individual elements inside its data structures (i.e.g. a Hash's field or a Set's member). Consider splitting your Hash into multiple string keys (each expirable by itself) or using Sorted Sets as explained here: Redis: To set timeout for a key value pair in Set

这篇关于你可以在Redis中使hashKey的键值对失效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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