如何使用Jedis捕获Redis中特定的添加和更新缓存事件?

huangapple go评论43阅读模式
英文:

How can I catch specific add and update cache events in Redis using 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      : 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:

    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/"

如果您需要任何进一步的帮助,请随时提出。

英文:

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/

答案1

得分: 1

我的问题是,当我捕获新的键事件时,总是伴随着一个设置值事件。

这是根据Redis的设计。您可以简单地选择任何一个选项来使用,并忽略另一个选项。例如,在您的代码中,您可以移除case "__keyevent@0__:new":块或case "__keyevent@0__:set":块。

另外,文档中提到:">= 7.0: 添加了事件类型new"。所以您也可以考虑使用低于7.0版本的Redis。

如何捕获特定的添加缓存事件和更新缓存事件?

您是否对所有可能的添加和更新事件感兴趣?或者只关心Strings数据结构的事件就足够了?SET命令在Strings中既执行添加操作又执行更新操作。因此,您可以选择仅使用该命令(在您的代码中即case "__keyevent@0__:set":块)。

英文:

> My problem is when i catch new key event always has a set value event go with this.

That's by Redis design. You can simply choose any one option to use and ignore the other one. For example, in your code you can remove either case "__keyevent@0__:new": block or case "__keyevent@0__:set": block.

Also, the doc says, ">= 7.0: Event type new added". So you may as well consider a Redis version lower than 7.0.

> How can i catch specific add cache event and update cache event in redis ?

Are you interested in all possible add and update events? Or events of only Strings data structure is enough for you? SET command does both add and update operations in Strings. So you can choose to use only that command (in your code that is the case "__keyevent@0__:set": block).

huangapple
  • 本文由 发表于 2023年5月29日 11:02:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定