欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot結合Redis配置工具類實現動態(tài)切換庫

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

我使用的版本是SpringBoot 2.6.4

可以實現注入不同的庫連接或是動態(tài)切換庫

    <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  # 是否開啟redis緩存  true開啟   false關閉
    database: 0
    host: 127.0.0.1
    password: 123456
    # history 自定義,切換庫索引
    history: 1 #歷史數據使用的庫
    port: 6379
    timeout: 6000ms  # 連接超時時長(毫秒)
    jedis:
      pool:
        max-active: 1000  # 連接池最大連接數(使用負值表示沒有限制)
        max-wait: -1ms      # 連接池最大阻塞等待時間(使用負值表示沒有限制)
        max-idle: 10      # 連接池中的最大空閑連接
        min-idle: 5       # 連接池中的最小空閑連接

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

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;

? ? /**
? ? ?* 實時數據庫 配置 默認0號庫
? ? ?*/
? ? @Bean
? ? public RedisTemplate<String, Object> redisTemplate() {
? ? ? ? RedisTemplate<String, Object> template = new RedisTemplate<>();
? ? ? ? // 解決value不能轉成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歷史數據庫 配置 ?根據yml配置庫
? ? ?*/
? ? @Bean("history")
? ? public RedisTemplate<String, Object> redisTemplatetwo() {
? ? ? ? RedisTemplate<String, Object> template = new RedisTemplate<>();
? ? ? ? // 解決value不能轉成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();
? ? ? ? // 建立連接,設置庫索引
? ? ? ? redisStandaloneConfiguration.setDatabase(history);
? ? ? ? redisStandaloneConfiguration.setHostName(host);
? ? ? ? redisStandaloneConfiguration.setPort(port);
? ? ? ? redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
? ? ? ? LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration, LettuceClientConfiguration.builder()
? ? ? ? ? ? ? ? // 超時時間
? ? ? ? ? ? ? ? .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()動態(tài)切換庫,方法調用完成應切回默認庫

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;
? ? // 默認數據庫
? ? private static Integer database = 0;
? ? /**
? ? ?* 默認過期時長,單位:秒
? ? ?*/
? ? public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
? ? /**
? ? ?* 不設置過期時長
? ? ?*/
? ? 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);
? ? }

? ? // 獲取實時數據集合 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轉成JSON數據
? ? ?*/
? ? 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數據,轉成Object
? ? ?*/
? ? private <T> T fromJson(String json, Class<T> clazz) {
? ? ? ? //T t = JSONObject.parseObject(json, clazz);

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

? ? /**
? ? ?* 設置數據庫索引
? ? ?* chens
? ? ?*
? ? ?* @param dbIndex 數據庫索引
? ? ?*/
? ? 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);
? ? ? ? // 屬性設置后
? ? ? ? redisConnectionFactory.afterPropertiesSet();
? ? ? ? // 重置連接
? ? ? ? redisConnectionFactory.resetConnection();
? ? }

}

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

相關文章

  • Java如何獲取JSON中某個對象的值

    Java如何獲取JSON中某個對象的值

    這篇文章主要介紹了Java如何獲取JSON中某個對象的值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • java 代碼塊與靜態(tài)代碼塊加載順序

    java 代碼塊與靜態(tài)代碼塊加載順序

    這篇文章主要介紹了java 代碼塊與靜態(tài)代碼塊加載順序的相關資料,需要的朋友可以參考下
    2017-07-07
  • 使用Feign設置Token鑒權調用接口

    使用Feign設置Token鑒權調用接口

    這篇文章主要介紹了使用Feign設置Token鑒權調用接口,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • MyBatis-Plus聯(lián)表查詢(Mybatis-Plus-Join)的功能實現

    MyBatis-Plus聯(lián)表查詢(Mybatis-Plus-Join)的功能實現

    mybatis-plus作為mybatis的增強工具,簡化了開發(fā)中的數據庫操作,這篇文章主要介紹了MyBatis-Plus聯(lián)表查詢(Mybatis-Plus-Join),需要的朋友可以參考下
    2022-08-08
  • 堆排序算法的講解及Java版實現

    堆排序算法的講解及Java版實現

    這篇文章主要介紹了堆排序算法的講解及Java版實現,堆排序基于堆這種數據結構,在本文中對堆的概念也有補充介紹,需要的朋友可以參考下
    2016-05-05
  • SpringBoot中常用注解的使用合集

    SpringBoot中常用注解的使用合集

    注解?annotation一般是用來定義一個類、屬性和一些方法,以便程序能夠被編譯處理,本文為大家整理了SpringBoot中的常用注解以及它們的使用,需要的可以參考下
    2023-07-07
  • Java的SPI機制實例詳解

    Java的SPI機制實例詳解

    這篇文章主要介紹了Java的SPI機制實例詳解的相關資料,需要的朋友可以參考下
    2017-06-06
  • IDEA 2020 本土化,真的是全中文了(真香)

    IDEA 2020 本土化,真的是全中文了(真香)

    去年,JetBrains 網站進行了本地化,提供了 8 種不同的語言版本,而現在,團隊正在對基于 IntelliJ 的 IDE 進行本地化
    2020-12-12
  • 手寫redis@Cacheable注解?參數java對象作為key值詳解

    手寫redis@Cacheable注解?參數java對象作為key值詳解

    這篇文章主要介紹了手寫redis@Cacheable注解?參數java對象作為key值詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring運行時手動注入bean的方法實例

    Spring運行時手動注入bean的方法實例

    spring給我們提供了IOC服務,讓我們可以用注解的方式,方便的使用bean的相互引用,下面這篇文章主要給大家介紹了關于Spring運行時手動注入bean的相關資料,需要的朋友可以參考下
    2022-05-05

最新評論