新聞中心
Spring Boot 整合 Redis 實(shí)現(xiàn)緩存功能入門教程

未央網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站設(shè)計(jì)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)公司于2013年成立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。
概述
在當(dāng)今互聯(lián)網(wǎng)時(shí)代,系統(tǒng)的性能和用戶體驗(yàn)至關(guān)重要,緩存技術(shù)作為一種優(yōu)化手段,能夠有效提高系統(tǒng)的訪問(wèn)速度和并發(fā)處理能力,Redis 是一款高性能的鍵值對(duì)存儲(chǔ)系統(tǒng),被廣泛應(yīng)用于緩存、消息隊(duì)列等領(lǐng)域,Spring Boot 是一款基于 Spring 框架的微服務(wù)開發(fā)框架,它簡(jiǎn)化了配置和部署過(guò)程,讓開發(fā)者能夠快速構(gòu)建獨(dú)立的、生產(chǎn)級(jí)別的應(yīng)用程序,本文將介紹如何使用 Spring Boot 整合 Redis 實(shí)現(xiàn)緩存功能。
環(huán)境準(zhǔn)備
1、安裝 Redis
需要在系統(tǒng)中安裝 Redis,可以從 Redis 官網(wǎng)下載相應(yīng)版本的安裝包,然后按照官方文檔進(jìn)行安裝,安裝完成后,啟動(dòng) Redis 服務(wù)。
2、創(chuàng)建 Spring Boot 項(xiàng)目
使用 Spring Initializr(https://start.spring.io/)創(chuàng)建一個(gè) Spring Boot 項(xiàng)目,選擇相應(yīng)的依賴:
– Spring Web
– Spring Data Redis
– Jedis
下載項(xiàng)目后,解壓并導(dǎo)入到開發(fā)工具(如 IntelliJ IDEA、Eclipse 等)。
配置 Redis
在 Spring Boot 項(xiàng)目中,需要在 application.properties 或 application.yml 文件中配置 Redis 相關(guān)屬性。
1、application.properties
Redis 數(shù)據(jù)庫(kù)索引(默認(rèn)為 0) spring.redis.database=0 Redis 服務(wù)器地址 spring.redis.host=localhost Redis 服務(wù)器連接端口 spring.redis.port=6379 Redis 服務(wù)器連接密碼(默認(rèn)為空) spring.redis.password= 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制) spring.redis.jedis.pool.max-active=8 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制) spring.redis.jedis.pool.max-wait=-1 連接池中的最大空閑連接 spring.redis.jedis.pool.max-idle=8 連接池中的最小空閑連接 spring.redis.jedis.pool.min-idle=0 連接超時(shí)時(shí)間(毫秒) spring.redis.timeout=5000
2、application.yml
spring:
redis:
database: 0
host: localhost
port: 6379
password:
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
timeout: 5000
使用 RedisTemplate
Spring Boot 提供了 RedisTemplate 和 StringRedisTemplate 兩個(gè)模板類,用于簡(jiǎn)化 Redis 操作,RedisTemplate 是泛型模板,可以操作任意的 Java 對(duì)象;StringRedisTemplate 是 RedisTemplate 的子類,專門用于操作字符串。
1、注入 RedisTemplate
在 Spring Boot 主配置類中注入 RedisTemplate:
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;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// 使用 Jackson2JsonRedisSerializer 來(lái)序列化和反序列化 redis 的 value 值(默認(rèn)使用 JdkSerializationRedisSerializer)
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
//指定要序列化的域,field,get和set,以及修飾范圍,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//指定序列化輸入的類型,類必須是非final修飾的
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om);
// 值采用json序列化
template.setValueSerializer(jacksonSeial);
//使用StringRedisSerializer來(lái)序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
// 設(shè)置hash key 和value序列化模式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
}
}
2、使用 RedisTemplate
在業(yè)務(wù)類中,注入 RedisTemplate,并使用它進(jìn)行緩存操作:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class CacheService {
@Autowired
private RedisTemplate redisTemplate;
public void setCache(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
public Object getCache(String key) {
return redisTemplate.opsForValue().get(key);
}
}
使用緩存注解
Spring Boot 提供了 @Cacheable、@CachePut 和 @CacheEvict 等注解,簡(jiǎn)化緩存操作。
1、@Cacheable
@Cacheable 注解用于將方法的返回值緩存到 Redis。
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Cacheable(value = "user", key = "#id")
public User findById(Long id) {
// 模擬數(shù)據(jù)庫(kù)查詢操作
return new User(id, "張三");
}
}
2、@CachePut
@CachePut 注解用于更新 Redis 中的緩存。
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@CachePut(value = "user", key = "#user.id")
public User updateUser(User user) {
// 模擬數(shù)據(jù)庫(kù)更新操作
return user;
}
}
3、@CacheEvict
@CacheEvict 注解用于刪除 Redis 中的緩存。
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@CacheEvict(value = "user", key = "#id")
public void deleteUser(Long id) {
// 模擬數(shù)據(jù)庫(kù)刪除操作
}
}
本文介紹了如何使用 Spring Boot 整合 Redis 實(shí)現(xiàn)緩存功能,配置了 Redis 相關(guān)屬性;通過(guò) RedisTemplate 和緩存注解簡(jiǎn)化了 Redis 操作,通過(guò)本文的學(xué)習(xí),讀者可以快速上手 Spring Boot 與 Redis 的集成開發(fā),提高系統(tǒng)的性能和并發(fā)處理能力。
網(wǎng)站題目:springboot使用Redis作緩存使用入門教程
本文路徑:http://www.5511xx.com/article/djscjgi.html


咨詢
建站咨詢
