Java Jedis 的 Redis 密钥过期通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26406303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redis Key expire notification with Jedis
提问by Rocky Ray
I am trying to implement a expiry key notification with redis ,when my key expires in the redis data store. The redis website provides some description of how http://redis.io/topics/notifications, but Im unable to find any example how to do it using redis java client like Jedis?
当我的密钥在 redis 数据存储中过期时,我正在尝试使用 redis 实现过期密钥通知。redis 网站提供了一些关于http://redis.io/topics/notifications 的描述,但我找不到任何示例如何使用像 Jedis 这样的 redis java 客户端来做到这一点?
Any possible code with illustration will be very helpful as im new to redis.
任何可能的带有插图的代码都会非常有帮助,因为我是 redis 的新手。
采纳答案by Kuntal-G
You can do it with the pub-submodel only Start Redis Server
你可以只用pub-sub模型来做Start Redis Server
Change the notify-keyspace-events in redis.conf to KEA (this depends on your requirement).Details given in redis documentation http://redis.io/topics/notifications.
将 redis.conf 中的 notify-keyspace-events 更改为 KEA(这取决于您的要求)。redis 文档http://redis.io/topics/notifications 中给出了详细信息。
Redis Java Client (Jedis) ,Try the following:
Redis Java Client (Jedis) ,请尝试以下操作:
Notification Listener:
通知监听器:
public class KeyExpiredListener extends JedisPubSub {
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
System.out.println("onPSubscribe "
+ pattern + " " + subscribedChannels);
}
@Override
public void onPMessage(String pattern, String channel, String message) {
System.out
.println("onPMessage pattern "
+ pattern + " " + channel + " " + message);
}
//add other Unimplemented methods
}
Subscriber:
订户:
****Note** jedis.psubscribe(new KeyExpiredListener(), "__key*__:*"); -- This methods support regex pattern based channel whereas jedis.subscribe(new KeyExpiredListener(), ""__keyspace@0__:notify"); --This method takes full/exact channel name
****注意**绝地武士。psubscribe(new KeyExpiredListener(), "__key*__:*"); -- 此方法支持基于正则表达式模式的通道,而 jedis。subscribe(new KeyExpiredListener(), ""__keyspace@0__:notify"); --这个方法需要完整/准确的频道名称
public class Subscriber {
public static void main(String[] args) {
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
Jedis jedis = pool.getResource();
jedis.psubscribe(new KeyExpiredListener(), "__key*__:*");
}
}
Test Class:
测试类:
public class TestJedis {
public static void main(String[] args) {
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
Jedis jedis = pool.getResource();
jedis.set("notify", "umq");
jedis.expire("notify", 10);
}
}
Now first start your Subscriber and then Run the TestJedis.You wil see the following output:
现在首先启动您的订阅服务器,然后运行 TestJedis。您将看到以下输出:
onPSubscribe __key*__:* 1
onPMessage pattern __key*__:* __keyspace@0__:notify set
onPMessage pattern __key*__:* __keyevent@0__:set notify
onPMessage pattern __key*__:* __keyspace@0__:notify expire
onPMessage pattern __key*__:* __keyevent@0__:expire notify
onPMessage pattern __key*__:* __keyspace@0__:notify expired
onPMessage pattern __key*__:* __keyevent@0__:expired notify
Now one use-case where you are interested in the valueof the expired key as well.
现在有一个用例,您也对过期密钥的值感兴趣。
Note:Redis only provide the key on expiration of key through notification of keyspace events, value is lost once the key expire. In-order to get the value on your key expire you can do the following work around shown below with the tricky concept of shadow key:
注意:Redis 只通过 keyspace 事件的通知来提供 key 过期时的 key,一旦 key 过期,value 就会丢失。为了获得您的密钥过期的值,您可以使用阴影密钥的棘手概念执行如下所示的以下工作:
When you create your notify key, also create a special expiring "shadow" key (don't expire the actual notify). For example:
创建通知密钥时,还要创建一个特殊的过期“影子”密钥(不要使实际通知过期)。例如:
// set your key value
SET notify umq
//set your "shadow" key, note the value here is irrelevant
SET shadowkey:notify "" EX 10
// Get an expiration message in the channel keyevent@0:expired // Split the key on ":"(or whatever separator you decide to use), take the second part to get your original key
// 在通道中获取过期消息keyevent@0:expired // 在“:”(或您决定使用的任何分隔符)上拆分密钥,获取第二部分以获取原始密钥
// Then get the value and do whatever with it
GET notify
// Then delete the key
DEL notify
Note that the value of the shadowkey isn't used so you want to use the smallest possible value, could be an empty string "". It's a little more work to setup but the above system does exactly what you need. The overhead is a few extra commands to actually retrieve and delete your key plus the storage cost of an empty key.
请注意,未使用 shadowkey 的值,因此您希望使用尽可能小的值,可能是空字符串“”。设置需要更多的工作,但上述系统正是您所需要的。开销是一些额外的命令来实际检索和删除您的密钥以及空密钥的存储成本。
Otherwise you have to prepare your key in such a way that it includes the value appended with it.
否则,您必须以包含附加值的方式准备您的密钥。
Hope it helps you!
希望对你有帮助!
回答by Vishvesh Phadnis
This may help you.
这可能对你有帮助。
JedisPool jedisPool=null;
JedisPoolConfig poolConfig = null;
try {
poolConfig=new JedisPoolConfig();
jedisPool = new JedisPool(poolConfig,"127.0.0.1" /*Host IP*/,1234 /*Port*/, 0);
Jedis jedis=jedisPool.getResource();
jedis.expire("KeyName", 10 /*Key Expires in 10 seconds*/);
} catch (Exception e) {
}