Redis Key使用Jedis过期通知 [英] Redis Key expire notification with Jedis

查看:661
本文介绍了Redis Key使用Jedis过期通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的密钥在redis数据存储中到期时,我正在尝试使用redis实现到期密钥通知。 redis网站提供了有关如何 http://redis.io/topics/notifications 的一些描述,但我无法找到任何示例如何使用像Jedis这样的redis java客户端吗?

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的新手。

Any possible code with illustration will be very helpful as im new to redis.

推荐答案

您只能使用 pub-sub 模型执行此操作
启动Redis服务器

You can do it with the pub-sub model only Start Redis Server

将redis.conf中的notify-keyspace-events更改为KEA(这取决于您的要求)。在redis文档中给出的详细信息 http://redis.io/topics/notifications

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 Java客户端(Jedis),请尝试以下操作:

Redis Java Client (Jedis) ,Try the following:

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


}



订阅者:



****注意** jedis。 psubscribe (新的KeyExpiredListener() ,__ key * __:*); - 这种方法支持基于正则表达式模式的频道
而jedis。订阅(新的KeyExpiredListener(),__ keyyspace @ 0 __:notify); - 此方法采用完整/精确的频道名称

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

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);

    }
}

现在首先开始订阅然后运行TestJedis.You将看到以下输出:

Now first start your Subscriber and then Run the TestJedis.You wil see the following output:

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 value of the expired key as well.

注意: Redis仅提供密钥通过密钥空间事件的通知到期密钥,一旦密钥到期,值就会丢失。为了使你的密钥价值到期,你可以使用阴影键的棘手概念进行下面的工作:

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:

当你创建通知密钥时,还会创建一个特殊的过期影子键(不要使实际通知失效)。例如:

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 

//在频道 keyevent @ 0 中获取过期消息:过期
//在:(或您决定使用的任何分隔符)上拆分键,取第二部分获取你的原始密钥

// 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

// Then get the value and do whatever with it
GET notify
// Then delete the key
DEL notify

请注意,不使用shadowkey的值所以你想使用尽可能小的值,可能是一个空字符串。设置工作要多一些,但上述系统完全符合您的需求。开销是一些额外的命令,用于实际检索和删除密钥以及空密钥的存储成本。

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.

否则,您必须以这样的方式准备密钥:它包含附加的值。

Otherwise you have to prepare your key in such a way that it includes the value appended with it.

希望它可以帮到你!

这篇关于Redis Key使用Jedis过期通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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