JetCache获取lettuce客户端

JetCache目前支持两种redis客户端,分别是jedis和lettuce,由于自身一些需求,原生的jetCacheAPI满足不了需求,故需要在此基础上使用redis客户端作为支持,目前公司在Apollo上配置了Lettuce客户端,故本文以lettuce客户端为例。

 

新增一个redis配置类

注意:@bean只能在@Configuration注解下

import com.alicp.jetcache.autoconfigure.LettuceFactory;
import com.alicp.jetcache.autoconfigure.RedisLettuceAutoConfiguration;
import io.lettuce.core.RedisClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

/**
 * Description:
 *
 * @author :Marco.Gu([email protected])
 * date:2020/6/10
 */
@Configuration
public class RedisClientConfig {
    @Bean(name = "redisClient")
    @DependsOn(RedisLettuceAutoConfiguration.AUTO_INIT_BEAN_NAME)
    public LettuceFactory defaultClient() {
        return new LettuceFactory("remote.default", RedisClient.class);
    }
}

 

在被扫描的类中直接注入

@Autowired
private RedisClient redisClient;

接下来可以通过redis客户端做一些原生处理了,如查询缓存过期时间、设置缓存过期时间、重命名key等


    @Override
    public Long getExpireTime(String name, String key) throws Exception {
        RedisCommands connect = redisClient.connect().sync();
        return  connect.ttl(name + key);
    }

    @Override
    public Boolean setExpireTime(String name, String key, long time) throws Exception {
        RedisCommands connect = redisClient.connect().sync();
        return  connect.expire(name + key, time);
    }

    @Override
    public Boolean renamenx(String oldName, String oldKey, String name, String key) {
        RedisCommands connect = redisClient.connect().sync();
        return connect.renamenx(oldName+oldKey,name+key);
    }

 

你可能感兴趣的:(Redis,lettuce,JetCache,redisClient,redis)