Redisson Capturing Key Expire Event

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

We use RMap to store key and value pairs, an example of which is as follows,

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

      

I want to assign a TTL for each key that I add and I know I can do it with RMap.expire()

. What I am not getting is how can I listen when a specific key expires. Since each key will have a different TTL and, as mentioned in the Redis documentation, it takes care of the automatic expiration of keys and also generates events.

My question is:

  • How can I capture the generated EXPIRE event and also get what key it was generated for in the Java Redisson library?

  • Is this the best approach (redis built-in auto-expiration) or is there some thread working that better checks for expired keys?

+3


source to share


1 answer


Starting with versions 2.9.3 and 3.4.3 Redisson offers the ability to register a listener for card expiration.

Here's a usage example:



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

map.put("key", "value", 10, TimeUnit.SECONDS);

      

+3


source







All Articles