SpringBoot中操作spring redis的工具類
阿新 • • 發佈:2020-09-01
場景
SpringBoot+Vue+Redis實現前後端分離的字典快取機制:
https://blog.csdn.net/badao_liumang_qizhi/article/details/108333996
在SpringBoot後臺中會使用到Redis去快取一些資料。
Windows下Redis服務端的安裝與配置
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/107486313
注:
部落格:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程式猿
獲取程式設計相關電子書、教程推送與免費下載。
實現
首先pom檔案中引入相關依賴。
<!-- redis 快取操作 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
然後在application.yml配置Redis服務端的連線
# Spring配置 spring: # 資源資訊 messages: # 國際化資原始檔路徑 basename: i18n/messages profiles: active: druid # 檔案上傳 servlet: multipart: # 單個檔案大小 max-file-size: 10MB # 設定總上傳的檔案大小 max-request-size: 20MB # 服務模組 devtools: restart: # 熱部署開關 enabled: true # redis 配置 redis: # 地址 #本地測試用 host: 127.0.0.1 port:6379 password: 123456 # 連線超時時間 timeout: 10s lettuce: pool: # 連線池中的最小空閒連線 min-idle: 0 # 連線池中的最大空閒連線 max-idle: 8 # 連線池的最大資料庫連線數 max-active: 8 # #連線池最大阻塞等待時間(使用負值表示沒有限制) max-wait: -1ms
這裡配置的Redis服務端是我自己的本地
然後封裝一個Spring Redis的工具類 用來對Redis 進行快取和獲取資料等。
import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.BoundSetOperations; import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.ListOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Component; /** * spring redis 工具類 * **/ @SuppressWarnings(value = { "unchecked", "rawtypes" }) @Component public class RedisCache { @Autowired public RedisTemplate redisTemplate; /** * 快取基本的物件,Integer、String、實體類等 * * @param key 快取的鍵值 * @param value 快取的值 * @return 快取的物件 */ public <T> ValueOperations<String, T> setCacheObject(String key, T value) { ValueOperations<String, T> operation = redisTemplate.opsForValue(); operation.set(key, value); return operation; } /** * 快取基本的物件,Integer、String、實體類等 * * @param key 快取的鍵值 * @param value 快取的值 * @param timeout 時間 * @param timeUnit 時間顆粒度 * @return 快取的物件 */ public <T> ValueOperations<String, T> setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit) { ValueOperations<String, T> operation = redisTemplate.opsForValue(); operation.set(key, value, timeout, timeUnit); return operation; } /** * 獲得快取的基本物件。 * * @param key 快取鍵值 * @return 快取鍵值對應的資料 */ public <T> T getCacheObject(String key) { ValueOperations<String, T> operation = redisTemplate.opsForValue(); return operation.get(key); } /** * 刪除單個物件 * * @param key */ public void deleteObject(String key) { redisTemplate.delete(key); } /** * 刪除集合物件 * * @param collection */ public void deleteObject(Collection collection) { redisTemplate.delete(collection); } /** * 快取List資料 * * @param key 快取的鍵值 * @param dataList 待快取的List資料 * @return 快取的物件 */ public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList) { ListOperations listOperation = redisTemplate.opsForList(); if (null != dataList) { int size = dataList.size(); for (int i = 0; i < size; i++) { listOperation.leftPush(key, dataList.get(i)); } } return listOperation; } /** * 獲得快取的list物件 * * @param key 快取的鍵值 * @return 快取鍵值對應的資料 */ public <T> List<T> getCacheList(String key) { List<T> dataList = new ArrayList<T>(); ListOperations<String, T> listOperation = redisTemplate.opsForList(); Long size = listOperation.size(key); for (int i = 0; i < size; i++) { dataList.add(listOperation.index(key, i)); } return dataList; } /** * 快取Set * * @param key 快取鍵值 * @param dataSet 快取的資料 * @return 快取資料的物件 */ public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet) { BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key); Iterator<T> it = dataSet.iterator(); while (it.hasNext()) { setOperation.add(it.next()); } return setOperation; } /** * 獲得快取的set * * @param key * @return */ public <T> Set<T> getCacheSet(String key) { Set<T> dataSet = new HashSet<T>(); BoundSetOperations<String, T> operation = redisTemplate.boundSetOps(key); dataSet = operation.members(); return dataSet; } /** * 快取Map * * @param key * @param dataMap * @return */ public <T> HashOperations<String, String, T> setCacheMap(String key, Map<String, T> dataMap) { HashOperations hashOperations = redisTemplate.opsForHash(); if (null != dataMap) { for (Map.Entry<String, T> entry : dataMap.entrySet()) { hashOperations.put(key, entry.getKey(), entry.getValue()); } } return hashOperations; } /** * 獲得快取的Map * * @param key * @return */ public <T> Map<String, T> getCacheMap(String key) { Map<String, T> map = redisTemplate.opsForHash().entries(key); return map; } /** * 獲得快取的基本物件列表 * * @param pattern 字串字首 * @return 物件列表 */ public Collection<String> keys(String pattern) { return redisTemplate.keys(pattern); } }
那麼在進行設定資料快取時
public static void setDictCache(String key, List<SysDictData> dictDatas) { SpringUtils.getBean(RedisCache.class).setCacheObject(key, dictDatas); }
這裡的key就是設定的快取的鍵,後面的物件的list就是要快取的資料。
這裡獲取RedisCache的方式是通過
SpringUtils.getBean(RedisCache.class)
因為這是在一個不受spring管理的工具類中。
如果是在其他受spring管理的bean中的話直接通過註解獲取RedisCache即可。
同理在獲取快取的資料時
Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
同理其他工具類方法都可使用。