springboot2.5.0和redis整合配置詳解
基本概況
為什么使用緩存
緩存是在內(nèi)存中存儲(chǔ)的數(shù)據(jù)備份,當(dāng)數(shù)據(jù)沒(méi)有發(fā)生本質(zhì)變化時(shí)
就可以直接從內(nèi)存中查詢數(shù)據(jù),而不用去數(shù)據(jù)庫(kù)查詢(在磁盤中)
CPU讀取內(nèi)存的速度要比讀取磁盤快,可以提高效率
Redis緩存
Remote Dictionnary Server(遠(yuǎn)程數(shù)據(jù)服務(wù)),是一款內(nèi)存高速緩存數(shù)據(jù)庫(kù)。
五種常用數(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ù)庫(kù)索引(默認(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ù)值表示沒(méi)有限制) spring.redis.pool.max-active=200 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制) 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;
/**
* 說(shuō)明: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 的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring?Boot使用線程池處理上萬(wàn)條數(shù)據(jù)插入功能
這篇文章主要介紹了Spring?Boot使用線程池處理上萬(wàn)條數(shù)據(jù)插入功能,使用步驟是先創(chuàng)建一個(gè)線程池的配置,讓Spring Boot加載,用來(lái)定義如何創(chuàng)建一個(gè)ThreadPoolTaskExecutor,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-08-08
Java使用Collections.sort對(duì)中文進(jìn)行排序方式
這篇文章主要介紹了Java使用Collections.sort對(duì)中文進(jìn)行排序方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Spring Cloud Stream異常處理過(guò)程解析
這篇文章主要介紹了Spring Cloud Stream異常處理過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
IntelliJ?IDEA?代碼運(yùn)行時(shí)中文出現(xiàn)亂碼問(wèn)題及解決方法
在我們剛接觸到IDEA時(shí),想美滋滋的敲一個(gè)“hello?world”來(lái)問(wèn)候這個(gè)世界,但難免會(huì)遇到這種問(wèn)題亂碼,這篇文章主要介紹了解決IntelliJ?IDEA?代碼運(yùn)行時(shí)中文出現(xiàn)亂碼問(wèn)題,需要的朋友可以參考下2023-09-09
Java根據(jù)模板導(dǎo)出Excel報(bào)表并復(fù)制模板生成多個(gè)Sheet頁(yè)
本文主要介紹了Java根據(jù)模板導(dǎo)出Excel報(bào)表并復(fù)制模板生成多個(gè)Sheet頁(yè)的方法,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
JAVA 靜態(tài)代理模式詳解及實(shí)例應(yīng)用
這篇文章主要介紹了JAVA 靜態(tài)代理模式詳解及實(shí)例應(yīng)用的相關(guān)資料,這里舉例說(shuō)明java 靜態(tài)代理模式該如何使用,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下2016-11-11
Java入門基礎(chǔ)之常規(guī)的命名方法和變量的值及其引用
這篇文章主要介紹了Java的命名方法和變量的值及其引用,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09

