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<String, String> connect = redisClient.connect().sync();
        return  connect.ttl(name + key);
    }

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

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章