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,
How can I capture the generated EXPIRE event and also get for which key it got generated in Redisson java library?
Is this better approach(redis inbuilt autoexpiration), or running some thread which checks expired keys is better?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With