English 中文(简体)
How can I catch specific add and update cache events in Redis using Jedis?
原标题:
  • 时间:2023-05-29 03:02:15
  •  标签:
  • redis
  • jedis

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/

问题回答

暂无回答




相关问题
How do you mix SQL DB vs. Key-Value store (i.e. Redis)

I m reviewing my code and realize I spend a tremendous amount of time taking rows from a database, formatting as XML, AJAX GET to browser, and then converting back into a hashed javascript object ...

Predis sharding (consistent hashing)

Predis claim to have Client-side sharding (support for consistent hashing of keys). http://github.com/nrk/predis I can do sharding using connect to an array of profiles (nodes) but it isn t ...

key value stores for extendable objects

http://www.infoq.com/presentations/newport-evolving-key-value-programming-model is a video about KV stores, and the whole premise is that redis promotes a column-based style for storing the attributes ...

nginx/redis and handling tracking params in url

I am using nginx and redis in my website. For several items on my site, I want to add tracking params to their urls so that when a user clicks on an item, I can collect statistics of user usage apart ...

热门标签