最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot結(jié)合Redis配置工具類實(shí)現(xiàn)動(dòng)態(tài)切換庫(kù)

 更新時(shí)間:2022年08月10日 10:03:15   作者:Coder-CT  
本文主要介紹了SpringBoot結(jié)合Redis配置工具類實(shí)現(xiàn)動(dòng)態(tài)切換庫(kù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

我使用的版本是SpringBoot 2.6.4

可以實(shí)現(xiàn)注入不同的庫(kù)連接或是動(dòng)態(tài)切換庫(kù)

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
        
    </parent>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
spring:    
  redis:
    # open自定義的,使用aop切面控制
    open: false  # 是否開(kāi)啟redis緩存  true開(kāi)啟   false關(guān)閉
    database: 0
    host: 127.0.0.1
    password: 123456
    # history 自定義,切換庫(kù)索引
    history: 1 #歷史數(shù)據(jù)使用的庫(kù)
    port: 6379
    timeout: 6000ms  # 連接超時(shí)時(shí)長(zhǎng)(毫秒)
    jedis:
      pool:
        max-active: 1000  # 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)
        max-wait: -1ms      # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
        max-idle: 10      # 連接池中的最大空閑連接
        min-idle: 5       # 連接池中的最小空閑連接

配置類 , 默認(rèn)0號(hào)庫(kù)使用@Autowired注入,自定義庫(kù)使用@Resource(name = “history”)注入
動(dòng)態(tài)切庫(kù)有個(gè)問(wèn)題就是一旦切庫(kù) 后面的數(shù)據(jù)就會(huì)一直保存在切換的庫(kù)里面,比如實(shí)時(shí)數(shù)據(jù)需要保存在1號(hào)庫(kù),歷史數(shù)據(jù)需要保存在2號(hào)庫(kù),切庫(kù)后 實(shí)時(shí)的就會(huì)存歷史里面、下面這種配置,想用哪個(gè)庫(kù)就注入哪個(gè)庫(kù),不存在切庫(kù)問(wèn)題

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
?* Redis配置
?*/
@Configuration
public class RedisConfig {
? ? @Autowired
? ? private RedisConnectionFactory factory;
? ? @Value("${spring.redis.host}")
? ? private String host;
? ? @Value("${spring.redis.port}")
? ? private Integer port;
? ? @Value("${spring.redis.password}")
? ? private String password;
? ? @Value("${spring.redis.history}")
? ? private Integer history;

? ? /**
? ? ?* 實(shí)時(shí)數(shù)據(jù)庫(kù) 配置 默認(rèn)0號(hào)庫(kù)
? ? ?*/
? ? @Bean
? ? public RedisTemplate<String, Object> redisTemplate() {
? ? ? ? RedisTemplate<String, Object> template = new RedisTemplate<>();
? ? ? ? // 解決value不能轉(zhuǎn)成string chens 2022-06-08
? ? ? ? Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
? ? ? ? template.setKeySerializer(new StringRedisSerializer());
? ? ? ? template.setHashKeySerializer(new StringRedisSerializer());
? ? ? ? template.setHashValueSerializer(new StringRedisSerializer());
? ? ? ? template.setValueSerializer(serializer);
? ? ? ? template.setConnectionFactory(factory);
? ? ? ? return template;
? ? }

? ? /**
? ? ?* redis歷史數(shù)據(jù)庫(kù) 配置 ?根據(jù)yml配置庫(kù)
? ? ?*/
? ? @Bean("history")
? ? public RedisTemplate<String, Object> redisTemplatetwo() {
? ? ? ? RedisTemplate<String, Object> template = new RedisTemplate<>();
? ? ? ? // 解決value不能轉(zhuǎn)成string?
? ? ? ? Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
? ? ? ? template.setKeySerializer(new StringRedisSerializer());
? ? ? ? template.setHashKeySerializer(new StringRedisSerializer());
? ? ? ? template.setHashValueSerializer(new StringRedisSerializer());
? ? ? ? template.setValueSerializer(serializer);
? ? ? ? RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
? ? ? ? // 建立連接,設(shè)置庫(kù)索引
? ? ? ? redisStandaloneConfiguration.setDatabase(history);
? ? ? ? redisStandaloneConfiguration.setHostName(host);
? ? ? ? redisStandaloneConfiguration.setPort(port);
? ? ? ? redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
? ? ? ? LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration, LettuceClientConfiguration.builder()
? ? ? ? ? ? ? ? // 超時(shí)時(shí)間
? ? ? ? ? ? ? ? .commandTimeout(Duration.ofMinutes(30)).build());
? ? ? ? factory.afterPropertiesSet();
? ? ? ? template.setConnectionFactory(factory);
? ? ? ? return template;
? ? }

? ? @Bean
? ? public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
? ? ? ? return redisTemplate.opsForHash();
? ? }

? ? @Bean
? ? public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
? ? ? ? return redisTemplate.opsForValue();
? ? }

? ? @Bean
? ? public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
? ? ? ? return redisTemplate.opsForList();
? ? }

? ? @Bean
? ? public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
? ? ? ? return redisTemplate.opsForSet();
? ? }

? ? @Bean
? ? public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
? ? ? ? return redisTemplate.opsForZSet();
? ? }
}

工具類,setDbIndex()動(dòng)態(tài)切換庫(kù),方法調(diào)用完成應(yīng)切回默認(rèn)庫(kù)

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
?* Redis工具類
?*
?* @author chens
?*/
@Component
public class RedisUtils {
? ? @Autowired
? ? private RedisTemplate<String, Object> redisTemplate;
? ? @Resource(name = "history")
? ? private RedisTemplate<String, Object> historyTemplate;
? ? @Autowired
? ? private ValueOperations<String, String> valueOperations;
? ? @Autowired
? ? private HashOperations<String, String, Object> hashOperations;
? ? @Autowired
? ? private ListOperations<String, Object> listOperations;
? ? @Autowired
? ? private SetOperations<String, Object> setOperations;
? ? @Autowired
? ? private ZSetOperations<String, Object> zSetOperations;
? ? // 默認(rèn)數(shù)據(jù)庫(kù)
? ? private static Integer database = 0;
? ? /**
? ? ?* 默認(rèn)過(guò)期時(shí)長(zhǎng),單位:秒
? ? ?*/
? ? public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
? ? /**
? ? ?* 不設(shè)置過(guò)期時(shí)長(zhǎng)
? ? ?*/
? ? public final static long NOT_EXPIRE = -1;
? ? private final static Gson gson = new Gson();

? ? public void set(String key, Object value, long expire) {
? ? ? ? valueOperations.set(key, toJson(value));
? ? ? ? if (expire != NOT_EXPIRE) {
? ? ? ? ? ? redisTemplate.expire(key, expire, TimeUnit.SECONDS);
? ? ? ? }
? ? }

? ? public void set(Integer index, String key, Object value, long expire) {
? ? ? ? setDbIndex(index);
? ? ? ? valueOperations.set(key, toJson(value));
? ? ? ? if (expire != NOT_EXPIRE) {
? ? ? ? ? ? redisTemplate.expire(key, expire, TimeUnit.SECONDS);
? ? ? ? }
? ? }

? ? public void set(String key, Object value) {
? ? ? ? set(key, value, DEFAULT_EXPIRE);
? ? }

? ? public void set(Integer index, String key, Object value) {
? ? ? ? setDbIndex(index);
? ? ? ? set(key, value, DEFAULT_EXPIRE);
? ? }

? ? public <T> T get(String key, Class<T> clazz, long expire) {
? ? ? ? String value = valueOperations.get(key);
? ? ? ? if (expire != NOT_EXPIRE) {
? ? ? ? ? ? redisTemplate.expire(key, expire, TimeUnit.SECONDS);
? ? ? ? }
? ? ? ? return value == null ? null : fromJson(value, clazz);
? ? }

? ? public <T> T get(Integer index, String key, Class<T> clazz, long expire) {
? ? ? ? setDbIndex(index);
? ? ? ? String value = valueOperations.get(key);
? ? ? ? if (expire != NOT_EXPIRE) {
? ? ? ? ? ? redisTemplate.expire(key, expire, TimeUnit.SECONDS);
? ? ? ? }
? ? ? ? return value == null ? null : fromJson(value, clazz);
? ? }

? ? // 獲取實(shí)時(shí)數(shù)據(jù)集合 chens
? ? public <T> List<T> getList(List<String> keys, Class<T> clazz) {
? ? ? ? List<T> list = new LinkedList<>();
? ? ? ? if (keys.size() < 1) return list;
? ? ? ? for (String key : keys) {
? ? ? ? ? ? T t = get(key, clazz, NOT_EXPIRE);
? ? ? ? ? ? if (t != null) {
? ? ? ? ? ? ? ? list.add(t);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //keys.forEach(i -> list.add(get(i, clazz, NOT_EXPIRE)));
? ? ? ? return list;
? ? }

? ? public <T> T get(String key, Class<T> clazz) {
? ? ? ? return get(key, clazz, NOT_EXPIRE);
? ? }

? ? public <T> T get(Integer index, String key, Class<T> clazz) {
? ? ? ? setDbIndex(index);
? ? ? ? return get(key, clazz, NOT_EXPIRE);
? ? }

? ? public String get(String key, long expire) {
? ? ? ? String value = valueOperations.get(key);
? ? ? ? if (expire != NOT_EXPIRE) {
? ? ? ? ? ? redisTemplate.expire(key, expire, TimeUnit.SECONDS);
? ? ? ? }
? ? ? ? return value;
? ? }

? ? public String get(Integer index, String key, long expire) {
? ? ? ? setDbIndex(index);
? ? ? ? String value = valueOperations.get(key);
? ? ? ? if (expire != NOT_EXPIRE) {
? ? ? ? ? ? redisTemplate.expire(key, expire, TimeUnit.SECONDS);
? ? ? ? }
? ? ? ? return value;
? ? }

? ? public String get(String key) {
? ? ? ? return get(key, NOT_EXPIRE);
? ? }

? ? public String get(Integer index, String key) {
? ? ? ? setDbIndex(index);
? ? ? ? return get(key, NOT_EXPIRE);
? ? }

? ? public void delete(String key) {
? ? ? ? redisTemplate.delete(key);
? ? }

? ? public void delete(Integer index, String key) {
? ? ? ? setDbIndex(index);
? ? ? ? redisTemplate.delete(key);
? ? }

? ? /**
? ? ?* Object轉(zhuǎn)成JSON數(shù)據(jù)
? ? ?*/
? ? private String toJson(Object object) {
? ? ? ? if (object instanceof Integer || object instanceof Long || object instanceof Float ||
? ? ? ? ? ? ? ? object instanceof Double || object instanceof Boolean || object instanceof String) {
? ? ? ? ? ? return String.valueOf(object);
? ? ? ? }
? ? ? ? return gson.toJson(object);
? ? }

? ? /**
? ? ?* JSON數(shù)據(jù),轉(zhuǎn)成Object
? ? ?*/
? ? private <T> T fromJson(String json, Class<T> clazz) {
? ? ? ? //T t = JSONObject.parseObject(json, clazz);

? ? ? ? return gson.fromJson(json, clazz);
? ? }

? ? /**
? ? ?* 設(shè)置數(shù)據(jù)庫(kù)索引
? ? ?* chens
? ? ?*
? ? ?* @param dbIndex 數(shù)據(jù)庫(kù)索引
? ? ?*/
? ? private void setDbIndex(Integer dbIndex) {
? ? ? ? // 邊界判斷
? ? ? ? if (dbIndex == null || dbIndex > 15 || dbIndex < 0) {
? ? ? ? ? ? dbIndex = database;
? ? ? ? }
? ? ? ? LettuceConnectionFactory redisConnectionFactory = (LettuceConnectionFactory) redisTemplate
? ? ? ? ? ? ? ? .getConnectionFactory();
? ? ? ? if (redisConnectionFactory == null) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? redisConnectionFactory.setDatabase(dbIndex);
? ? ? ? redisTemplate.setConnectionFactory(redisConnectionFactory);
? ? ? ? // 屬性設(shè)置后
? ? ? ? redisConnectionFactory.afterPropertiesSet();
? ? ? ? // 重置連接
? ? ? ? redisConnectionFactory.resetConnection();
? ? }

}

到此這篇關(guān)于SpringBoot結(jié)合Redis配置工具類實(shí)現(xiàn)動(dòng)態(tài)切換庫(kù)的文章就介紹到這了,更多相關(guān)SpringBoot Redis動(dòng)態(tài)切換庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Springboot+Redis執(zhí)行l(wèi)ua腳本的項(xiàng)目實(shí)踐

    Springboot+Redis執(zhí)行l(wèi)ua腳本的項(xiàng)目實(shí)踐

    本文主要介紹了Springboot+Redis執(zhí)行l(wèi)ua腳本的項(xiàng)目實(shí)踐,詳細(xì)的介紹Redis與Lua腳本的結(jié)合應(yīng)用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • 關(guān)于spring5的那些事:@Indexed 解密

    關(guān)于spring5的那些事:@Indexed 解密

    這篇文章主要介紹了關(guān)于spring5的那些事:@Indexed 解密,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java FTPClient連接池的實(shí)現(xiàn)

    Java FTPClient連接池的實(shí)現(xiàn)

    這篇文章主要介紹了Java FTPClient連接池的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • Java在創(chuàng)建文件時(shí)指定編碼的實(shí)現(xiàn)方法

    Java在創(chuàng)建文件時(shí)指定編碼的實(shí)現(xiàn)方法

    本文主要介紹了Java在創(chuàng)建文件時(shí)指定編碼的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • java使用Cookie判斷用戶登錄情況的方法

    java使用Cookie判斷用戶登錄情況的方法

    這篇文章主要為大家詳細(xì)介紹了java使用Cookie判斷用戶登錄情況,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Java實(shí)現(xiàn)網(wǎng)絡(luò)資源的單線程下載

    Java實(shí)現(xiàn)網(wǎng)絡(luò)資源的單線程下載

    這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)在一個(gè)線程中完成網(wǎng)絡(luò)資源的下載,文中的實(shí)現(xiàn)步驟講解詳細(xì),感興趣的可以嘗試下
    2022-10-10
  • Java分頁(yè)查詢的幾種實(shí)現(xiàn)方法舉例

    Java分頁(yè)查詢的幾種實(shí)現(xiàn)方法舉例

    這篇文章主要給大家介紹了關(guān)于Java分頁(yè)查詢的幾種實(shí)現(xiàn)方法,分頁(yè)是系統(tǒng)中常用到的功能,只要涉及到查詢必定伴隨而來(lái)的就是分頁(yè),文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • IDEA創(chuàng)建SpringBoot父子Module項(xiàng)目的實(shí)現(xiàn)

    IDEA創(chuàng)建SpringBoot父子Module項(xiàng)目的實(shí)現(xiàn)

    本文主要介紹了IDEA創(chuàng)建SpringBoot父子Module項(xiàng)目的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • java web過(guò)濾器處理亂碼

    java web過(guò)濾器處理亂碼

    本文主要介紹了java web過(guò)濾器處理亂碼的方法解析。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-04-04
  • SpringBoot模擬實(shí)現(xiàn)流式輸出效果

    SpringBoot模擬實(shí)現(xiàn)流式輸出效果

    這篇文章主要為大家詳細(xì)介紹了如何使用SpringBoot模擬實(shí)現(xiàn)流式輸出效果,并在前端使用流式接收數(shù)據(jù)并打印,感興趣的小伙伴可以參考一下
    2025-03-03

最新評(píng)論

建昌县| 东港市| 苍梧县| 灵宝市| 竹北市| 威海市| 巩留县| 镇沅| 涟水县| 山东省| 格尔木市| 汾阳市| 诏安县| 榕江县| 临洮县| 五家渠市| 宿迁市| 仲巴县| 黔西县| 德安县| 随州市| 仪征市| 龙陵县| 望都县| 汽车| 鄢陵县| 新兴县| 昌黎县| 义马市| 精河县| 漳平市| 田林县| 互助| 兴宁市| 镇平县| 饶平县| 长顺县| 修文县| 罗平县| 宁都县| 连江县|