SpringBoot整合Redis使用注解進(jìn)行緩存方式
Hello,各位小伙伴,最近項目中需要將一些常用的數(shù)據(jù)緩存起來,毫無疑問,我們采用了Redis來緩存數(shù)據(jù)。那么使用Redis緩存數(shù)據(jù)有哪些方式呢?接下來我會一一道來。
1.環(huán)境搭建
引入Redis依賴以及Spring相關(guān)的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>2.使用RedisTemplate
使用RedisTemplate調(diào)用API時,我們通常需要去配置key,value的序列化器,以及創(chuàng)建一個RedisUtils類來完成緩存的增刪改查以及過期時間的設(shè)置。
配置如下:
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// 設(shè)置key的序列化方式
template.setKeySerializer(RedisSerializer.string());
// 設(shè)置value的序列化方式
template.setValueSerializer(RedisSerializer.json());
// 設(shè)置hash的key的序列化方式
template.setHashKeySerializer(RedisSerializer.string());
// 設(shè)置hash的value的序列化方式
template.setHashValueSerializer(RedisSerializer.json());
template.afterPropertiesSet();
return template;
}
}RedisUtils:
public class RedisUtils {
@Resource
private RedisTemplate<String, Object> redisTemplate;
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通緩存放入
*
* @param key 鍵
* @param value 值
*/
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 普通緩存放入并設(shè)置時間
*
* @param key 鍵
* @param value 值
* @param time 時間(秒) time要大于0 如果time小于等于0 將設(shè)置無限期
*/
public void set(String key, Object value, long time) {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
}
/**
* 向一張hash表中放入數(shù)據(jù),如果不存在將創(chuàng)建
*
* @param key 鍵
* @param item 項
* @param value 值
*/
public void hset(String key, String item, Object value) {
redisTemplate.opsForHash().put(key, item, value);
}
/**
* 向一張hash表中放入數(shù)據(jù),如果不存在將創(chuàng)建
*
* @param key 鍵
* @param item 項
* @param value 值
* @param time 時間(秒) 注意:如果已存在的hash表有時間,這里將會替換原有的時間
*/
public void hset(String key, String item, Object value, long time, TimeUnit timeUnit) {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time, timeUnit);
}
}
/**
* @param key 鍵
* @param map 多個field-value
*/
public void hmset(String key, Map<String, Object> map) {
redisTemplate.opsForHash().putAll(key, map);
}
/**
* @param key 鍵
* @param map 多個field-value
* @param time 過期時間
* @param timeUnit 時間單位
*/
public void hmset(String key, Map<String, Object> map, long time, TimeUnit timeUnit) {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time, timeUnit);
}
}
/**
* @param key 鍵
* @param item Field
* @return 獲得的值
*/
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* @param key 鍵
* @return hash表中key對應(yīng)的map
*/
public Map<Object, Object> hEntries(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* @param key 鍵
* @param value 值
*/
public void sadd(String key, Object value) {
redisTemplate.opsForSet().add(key, value);
}
/**
* @param key 鍵
* @param value 值
* @param time 過期時間
* @param timeUnit 時間單位
*/
public void sadd(String key, Object value, long time, TimeUnit timeUnit) {
redisTemplate.opsForSet().add(key, value);
if (time > 0) {
expire(key, time, timeUnit);
}
}
/**
* @param key 鍵
* @return 數(shù)據(jù)
*/
public Set<Object> smembers(String key) {
return redisTemplate.opsForSet().members(key);
}
public void expire(String key, long time, TimeUnit timeUnit) {
if (time > 0) {
redisTemplate.expire(key, time, timeUnit);
}
}
/**
* 根據(jù)key 獲取過期時間
*
* @param key 鍵 不能為null
* @return 時間(秒) 返回0代表為永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
}通過以上的配置,我們在使用緩存的時候,直接調(diào)用API就可以進(jìn)行操作了。但是有個問題就是,我們在緩存一些常用數(shù)據(jù)的時候,通常我們需要先判斷一下緩存中有沒有該key,如果沒有我們再去數(shù)據(jù)庫中查詢出來,并緩存。
這樣的話我們需要去做一些判斷的操作,緩存中有就去緩存中取,沒有就從數(shù)據(jù)庫中取出來并緩存。那么有沒有更方便的操作方式呢,答案當(dāng)然是有的。
3.使用@EnableCaching+@Cacheable
Spring為我們提供了Caching模塊,我們可以該模塊給我們提供的功能,使用注解很方便完成數(shù)據(jù)緩存
在使用的過程中,我發(fā)現(xiàn),雖然我們配置了RedisTemplate的序列化,但是對于基于注解的Redis緩存來說是無效的,我們需要配置自定義的RedisCacheManager
配置如下:
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// 設(shè)置key的序列化方式
template.setKeySerializer(RedisSerializer.string());
// 設(shè)置value的序列化方式
template.setValueSerializer(RedisSerializer.json());
// 設(shè)置hash的key的序列化方式
template.setHashKeySerializer(RedisSerializer.string());
// 設(shè)置hash的value的序列化方式
template.setHashValueSerializer(RedisSerializer.json());
template.afterPropertiesSet();
return template;
}
@Bean
public RedisCacheManager redisCacheManager(RedisConnectionFactory factory) {
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(factory);
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.string()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
}
}使用方法:
- 在啟動類上加上
@EnableCaching - 在需要緩存數(shù)據(jù)的方法上加上
@Cacheable(cacheNames = "xxx"),方法的返回值就是我們需要緩存的數(shù)據(jù) - 基于以上的操作,我們就可以很方便的將需要緩存的數(shù)據(jù)緩存到Redis
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA POM文件配置profile實現(xiàn)不同環(huán)境切換的方法步驟
這篇文章主要介紹了IDEA POM文件配置profile實現(xiàn)不同環(huán)境切換的方法步驟2024-03-03
使用ehcache三步搞定springboot緩存的方法示例
本次內(nèi)容主要介紹基于Ehcache 3.0來快速實現(xiàn)Spring Boot應(yīng)用程序的數(shù)據(jù)緩存功能。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
Activiti7通過代碼動態(tài)生成工作流實現(xiàn)詳解
這篇文章主要為大家介紹了Activiti7通過代碼動態(tài)生成工作流實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
spring mvc DispatcherServlet之前端控制器架構(gòu)詳解
這篇文章主要為大家詳細(xì)介紹了spring mvc DispatcherServlet之前端控制器架構(gòu),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
spring @Scheduled定時任務(wù)注解使用方法及注意事項小結(jié)
Spring的@Scheduled注解用于定時任務(wù)調(diào)度,默認(rèn)單線程依次執(zhí)行,可以通過配置多線程調(diào)度器或使用@Async注解實現(xiàn)并行執(zhí)行,常見參數(shù)包括cron、fixedRate、fixedDelay、initialDelay等,本文介紹spring @Scheduled定時任務(wù)注解使用方法,感興趣的朋友一起看看吧2025-02-02
El表達(dá)式使用問題javax.el.ELException:Failed to parse the expression
今天小編就為大家分享一篇關(guān)于Jsp El表達(dá)式使用問題javax.el.ELException:Failed to parse the expression的解決方式,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12

