Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redisson Capturing Key Expire Event

I am using Redis as a cache service in my Big Data application. The main purpose of Redis is to validate key which we receive from every request.

We use RMap for storing key and value pairs, example of which is as follows,

key = 1212sads23sads341212saas23asds45
value = Regular java object with some complex data.

I want to assign TTL for every key I insert and I know I can do that using RMap.expire(). What I am not getting is, how can I listen to when particular key expires. As every key is going to have a different TTL and as mentioned in Redis documentation, it takes care of auto expiration of keys and also generates events.

My question is,

  1. How can I capture the generated EXPIRE event and also get for which key it got generated in Redisson java library?

  2. Is this better approach(redis inbuilt autoexpiration), or running some thread which checks expired keys is better?

like image 335
Rahul Borkar Avatar asked Jul 18 '17 08:07

Rahul Borkar


1 Answers

Since 3.4.3 version Redisson offers ability to register listener for map entry expiration.

Here is the usage example:

RMapCache<String, String> mapCache = redisson.getMapCache("myMap");
int expireListener = map.addListener(new EntryExpiredListener<String, String>() {
    @Override
    public void onExpired(EntryEvent<String, String> event) {
      event.getKey(); // expired key
      event.getValue() // expired value
      // ...
    }
});

map.put("key", "value", 10, TimeUnit.SECONDS);
like image 141
Nikita Koksharov Avatar answered Oct 20 '22 06:10

Nikita Koksharov