springboot2.5.0和redis整合配置詳解
基本概況
為什么使用緩存
緩存是在內(nèi)存中存儲的數(shù)據(jù)備份,當(dāng)數(shù)據(jù)沒有發(fā)生本質(zhì)變化時
就可以直接從內(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 # 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制) spring.redis.pool.max-wait=-1 # 連接池中的最大空閑連接 spring.redis.pool.max-idle=10 # 連接池中的最小空閑連接 spring.redis.pool.min-idle=0 # 連接超時時間(毫秒) 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)文章
Java SpringBoot快速集成SpringBootAdmin管控臺監(jiān)控服務(wù)詳解
這篇文章主要介紹了如何基于springboot-admin管控臺監(jiān)控服務(wù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2021-09-09
Java中動態(tài)規(guī)則的實(shí)現(xiàn)方式示例詳解
這篇文章主要介紹了Java中動態(tài)規(guī)則的實(shí)現(xiàn)方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
SpringBoot整合ElasticSearch的示例代碼
本篇文章主要介紹了SpringBoot整合ElasticSearch的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
IntelliJ IDEA 2019.1.1 for MAC 下載和注
這篇文章主要介紹了IntelliJ IDEA 2019.1.1 for MAC 下載和注冊碼激活,教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
如何使用IDEA開發(fā)Spark SQL程序(一文搞懂)
Spark SQL 是一個用來處理結(jié)構(gòu)化數(shù)據(jù)的spark組件。它提供了一個叫做DataFrames的可編程抽象數(shù)據(jù)模型,并且可被視為一個分布式的SQL查詢引擎。這篇文章主要介紹了如何使用IDEA開發(fā)Spark SQL程序(一文搞懂),需要的朋友可以參考下2021-08-08
java 實(shí)現(xiàn)讀取txt文本數(shù)據(jù)并以數(shù)組形式一行一行取值
今天小編就為大家分享一篇java 實(shí)現(xiàn)讀取txt文本數(shù)據(jù)并以數(shù)組形式一行一行取值,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

