陪玩平台搭建,利用Redis实现多级缓存

陪玩平台搭建,利用Redis实现多级缓存

如何使用redis6客户端缓存

前置条件: redis服务端版本必须是>=6。lettuce版本>=6 目前java的redis客户端找了一圈,貌似只有lettuce 6支持,其他客户端估计后期会支持

1、项目中pom引入lettuce GAV

  <dependency>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
            <version>6.1.8.RELEASE</version>
        </dependency>

 

2、利用lettuce6提供的ClientSideCaching进行实现

 /**
     * 客户端缓存同步
     *
     */
    public String getClientCacheValue(Map<String,String> clientCache,String key){
        StatefulRedisConnection<String, String> connect = redisClient.connect();
      //  Map<String,String> clientCache = new ConcurrentHashMap<>();
        CacheFrontend<String,String> frontend = ClientSideCaching.enable(CacheAccessor.forMap(clientCache),
                connect, TrackingArgs.Builder.enabled().noloop());
        return frontend.get(key);

    }

 

3、测试

@Override
public void run(ApplicationArguments args) throws Exception {
    while(true){
        System.out.println(lettuceRedisTemplate.getClientCacheValue("zhangsan"));
        TimeUnit.SECONDS.sleep(1);
    }


}

 

redis里面的zhangsan数据未发生变更时,

控制台输出的数据为

我们将redis zhangsan的密码改成9999,

看本地缓存能否立马捕捉到

控制台发现密码已经改成9999

以上就是陪玩平台搭建,利用Redis实现多级缓存, 更多内容欢迎关注之后的文章

posted @ 2025-02-22 09:04  云豹科技-苏凌霄  阅读(12)  评论(0)    收藏  举报