Specific NEW and UPDATE cache event on REDIS CACHE KEYSPACE NOTIFICATION.
Hi there, I am trying to catch 2 keyspace event "a New key event" and "update value of key event" in redis cache, im using Jedis, My problem is when i catch new key event always has a set value event go with this.
2023-05-29 09:43:06.938 INFO 22040 --- [ddedContainer-1] c.e.g.C.RedisCacheEventHandleConfig : Received task event: 1tuanAnh1 for channel: __keyevent@0__:new
2023-05-29 09:43:06.938 INFO 22040 --- [ddedContainer-1] c.e.g.C.RedisCacheEventHandleConfig : HANDLING NEW CACHE EVENT!!!
2023-05-29 09:43:06.938 INFO 22040 --- [ddedContainer-1] c.e.g.Service.CacheHandleServiceImpl : Key add: 1tuanAnh1
2023-05-29 09:43:06.938 INFO 22040 --- [dateContainer-1] c.e.g.C.RedisCacheEventHandleConfig : Received task event: 1tuanAnh1 for channel: __keyevent@0__:set
2023-05-29 09:43:06.941 INFO 22040 --- [dateContainer-1] c.e.g.C.RedisCacheEventHandleConfig : HANDLING UPDATE CACHE EVENT!!!
2023-05-29 09:43:06.941 INFO 22040 --- [dateContainer-1] c.e.g.Service.CacheHandleServiceImpl : Key update: 1tuanAnh1
this is my code:
public class RedisCacheEventHandleConfig implements MessageListener {
private final CacheHandleService cacheHandleService;
private final Gson gson;
@Override
public void onMessage(Message message, byte[] pattern) {
String key = new String(message.getBody());
String channel = new String(message.getChannel());
log.info("Received task event: {} for channel: {}", key, channel);
switch (channel) {
case "__keyevent@0__:new":
log.info("HANDLING NEW CACHE EVENT!!!");
cacheHandleService.handleAddCacheForKey(key);
break;
case "__keyevent@0__:set":
Jedis jedis = new Jedis("http://localhost:6379");
DataExample res = gson.fromJson(jedis.get(key), DataExample.class);
if (res != null){
log.info("HANDLING UPDATE CACHE EVENT!!!");
cacheHandleService.handleUpdateCacheForKey(key);
}
break;
case "__keyevent@0__:expired":
log.info("HANDLING EXPIRED CACHE EVENT!!!");
cacheHandleService.handleExpiredCacheForKey(key);
break;
}
}
}
public class CacheHandleServiceImpl implements CacheHandleService{
@Override
public void handleAddCacheForKey(String key) {
log.info("Key add: {}", key);
}
@Override
public void handleUpdateCacheForKey(String key) {
log.info("Key update: {}", key);
}
@Override
public void handleExpiredCacheForKey(String key) {
log.info("Key expired: {}", key);
}
}
How can i catch specific add cache event and update cache event in redis ? Thanks for reading.
Iam following this doc: https://redis.io/docs/manual/keyspace-notifications/