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

springboot2.5.0和redis整合配置詳解

 更新時(shí)間:2021年06月02日 14:07:08   作者:wangzswu  
本篇文章向大家介紹springboot2.5.0 整合 redis 配置方法,教大家在pom添加依賴的方法如何調(diào)用redis,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

基本概況

為什么使用緩存

緩存是在內(nèi)存中存儲的數(shù)據(jù)備份,當(dāng)數(shù)據(jù)沒有發(fā)生本質(zhì)變化時(shí)
就可以直接從內(nèi)存中查詢數(shù)據(jù),而不用去數(shù)據(jù)庫查詢(在磁盤中)
CPU讀取內(nèi)存的速度要比讀取磁盤快,可以提高效率

Redis緩存

Remote Dictionnary Server(遠(yuǎn)程數(shù)據(jù)服務(wù)),是一款內(nèi)存高速緩存數(shù)據(jù)庫。
五種常用數(shù)據(jù)類型: String(字符串)、List(列表)、Set(集合)、Hash(散列)、ZSet(有序集合)
可持久化:一邊運(yùn)行,一邊向硬盤備份一份,防止斷電等偶然情況,導(dǎo)致內(nèi)存中數(shù)據(jù)丟失

下載Redis

鏈接: https://pan.baidu.com/s/1BMt4cIxjKTtyL3T0_iSC2w 提取碼: rkne

1. pom添加依賴

<!--redis-->
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>

2. application.properties 配置文件

#===========Redis配置===========
# Redis數(shù)據(jù)庫索引(默認(rèn)為0)  
spring.redis.database=0
# Redis服務(wù)器地址  
spring.redis.host=127.0.0.1
# Redis服務(wù)器連接端口  
spring.redis.port=6379
# Redis服務(wù)器連接密碼
spring.redis.password=root
# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)  
spring.redis.pool.max-active=200
# 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)  
spring.redis.pool.max-wait=-1
# 連接池中的最大空閑連接 
spring.redis.pool.max-idle=10
# 連接池中的最小空閑連接  
spring.redis.pool.min-idle=0
# 連接超時(shí)時(shí)間(毫秒)
spring.redis.timeout=2000ms
spring.redis.jedis.pool.max-wait=-1ms
#===========Redis配置===========

3. RedisConfig.java 配置類

package org.fh.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.annotation.JsonAutoDetect;

/**
 *  說明:Redis
 * from:www.fhadmin.org
 */
@Configuration
public class RedisConfig {

	@Bean
	@SuppressWarnings("all")
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
		template.setConnectionFactory(factory);
		Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
		ObjectMapper om = new ObjectMapper();
		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , ObjectMapper.DefaultTyping.NON_FINAL);
		jackson2JsonRedisSerializer.setObjectMapper(om);
		StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
		// key采用String的序列化方式
		template.setKeySerializer(stringRedisSerializer);
		// hash的key也采用String的序列化方式
		template.setHashKeySerializer(stringRedisSerializer);
		// value序列化方式采用jackson
		template.setValueSerializer(jackson2JsonRedisSerializer);
		// hash的value序列化方式采用jackson
		template.setHashValueSerializer(jackson2JsonRedisSerializer);
		template.afterPropertiesSet();
		return template;
	}

}

4. 調(diào)用redis

 @Autowired
   private RedisTemplate<String, Object> redisTemplate;

	/**
	 * 普通緩存獲取
	 * @param key 鍵
	 * @return 值
	 */
	public Object get(String key) {
		return key == null ? null : redisTemplate.opsForValue().get(key);
	}

	/**
	 * 普通緩存放入
	 * @param key 鍵
	 * @param value 值
	 * @return true成功 false失敗
	 */
	public boolean set(String key, Object value) {
		try {
			redisTemplate.opsForValue().set(key, value);
			return true;
		} catch (Exception e) {
			//e.printStackTrace();
			return false;
		}
	}

以上就是springboot2.5.0 整合 redis 配置詳解的詳細(xì)內(nèi)容,更多關(guān)于springboot2.5.0 整合 redis 的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Spring?Boot使用線程池處理上萬條數(shù)據(jù)插入功能

    Spring?Boot使用線程池處理上萬條數(shù)據(jù)插入功能

    這篇文章主要介紹了Spring?Boot使用線程池處理上萬條數(shù)據(jù)插入功能,使用步驟是先創(chuàng)建一個(gè)線程池的配置,讓Spring Boot加載,用來定義如何創(chuàng)建一個(gè)ThreadPoolTaskExecutor,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-08-08
  • Java使用Collections.sort對中文進(jìn)行排序方式

    Java使用Collections.sort對中文進(jìn)行排序方式

    這篇文章主要介紹了Java使用Collections.sort對中文進(jìn)行排序方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Spring Cloud Stream異常處理過程解析

    Spring Cloud Stream異常處理過程解析

    這篇文章主要介紹了Spring Cloud Stream異常處理過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Java線程池ForkJoinPool實(shí)例解析

    Java線程池ForkJoinPool實(shí)例解析

    這篇文章主要介紹了Java線程池ForkJoinPool實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • IntelliJ?IDEA?代碼運(yùn)行時(shí)中文出現(xiàn)亂碼問題及解決方法

    IntelliJ?IDEA?代碼運(yùn)行時(shí)中文出現(xiàn)亂碼問題及解決方法

    在我們剛接觸到IDEA時(shí),想美滋滋的敲一個(gè)“hello?world”來問候這個(gè)世界,但難免會遇到這種問題亂碼,這篇文章主要介紹了解決IntelliJ?IDEA?代碼運(yùn)行時(shí)中文出現(xiàn)亂碼問題,需要的朋友可以參考下
    2023-09-09
  • Java根據(jù)模板導(dǎo)出Excel報(bào)表并復(fù)制模板生成多個(gè)Sheet頁

    Java根據(jù)模板導(dǎo)出Excel報(bào)表并復(fù)制模板生成多個(gè)Sheet頁

    本文主要介紹了Java根據(jù)模板導(dǎo)出Excel報(bào)表并復(fù)制模板生成多個(gè)Sheet頁的方法,具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-03-03
  • 詳解Struts2攔截器機(jī)制

    詳解Struts2攔截器機(jī)制

    這篇文章主要介紹了詳解Struts2攔截器機(jī)制,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • JAVA 靜態(tài)代理模式詳解及實(shí)例應(yīng)用

    JAVA 靜態(tài)代理模式詳解及實(shí)例應(yīng)用

    這篇文章主要介紹了JAVA 靜態(tài)代理模式詳解及實(shí)例應(yīng)用的相關(guān)資料,這里舉例說明java 靜態(tài)代理模式該如何使用,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下
    2016-11-11
  • Mybatis常見注解有哪些(總結(jié))

    Mybatis常見注解有哪些(總結(jié))

    這篇文章主要介紹了Mybatis常見注解有哪些(總結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java入門基礎(chǔ)之常規(guī)的命名方法和變量的值及其引用

    Java入門基礎(chǔ)之常規(guī)的命名方法和變量的值及其引用

    這篇文章主要介紹了Java的命名方法和變量的值及其引用,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-09-09

最新評論