若依管理系統新增redis
在pom.xml中引入依賴
<!-- redis 快取 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 阿里JSON解析器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
2.在application.yml中新增配置資訊
spring:
# redis 配置
redis:
# 地址
host: localhost
# 埠,預設為6379
port: 6379
# 密碼
password: 123456
# 連線超時時間
timeout: 10s
lettuce:
pool:
# 連線池中的最小空閒連線
min-idle: 0
# 連線池中的最大空閒連線
max-idle: 8
# 連線池的最大資料庫連線數
max-active: 8
# #連線池最大阻塞等待時間(使用負值表示沒有限制)
max-wait: -1ms
3新增FastJson2JsonRedisSerializer.java
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import com.alibaba.fastjson.parser.ParserConfig;
import org.springframework.util.Assert;
import java.nio.charset.Charset;
/**
* Redis使用FastJson序列化
*
* @author ruoyi
*/
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
{
@SuppressWarnings("unused")
private ObjectMapper objectMapper = new ObjectMapper();
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class<T> clazz;
static
{
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
}
public FastJson2JsonRedisSerializer(Class<T> clazz)
{
super();
this.clazz = clazz;
}
public byte[] serialize(T t) throws SerializationException
{
if (t == null)
{
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
public T deserialize(byte[] bytes) throws SerializationException
{
if (bytes == null || bytes.length <= 0)
{
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return JSON.parseObject(str, clazz);
}
public void setObjectMapper(ObjectMapper objectMapper)
{
Assert.notNull(objectMapper, "'objectMapper' must not be null");
this.objectMapper = objectMapper;
}
protected JavaType getJavaType(Class<?> clazz)
{
return TypeFactory.defaultInstance().constructType(clazz);
}
}
4.新增RedisConfig.java
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
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.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* redis配置
*
* @author ruoyi
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport
{
@Bean
@SuppressWarnings(value = { "unchecked", "rawtypes" })
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
{
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
// 使用StringRedisSerializer來序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
4.工具類
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 工具類
*
* @author ruoyi
**/
@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);
}
/**
* 刪除集合物件
*
* @param keyLike
*/
public void deleteByLike(String keyLike) {
if(keyLike!=null){
redisTemplate.delete( keys(keyLike));
}
}
/**
* 快取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;
}
/**
* 獲得快取的list物件
*
* @param collection 快取的鍵值
* @return 快取鍵值對應的資料
*/
public <T> List<T> mgetCacheList(Collection collection) {
List<T> dataList = new ArrayList<T>();
ValueOperations<String, T> operation = redisTemplate.opsForValue();
if (collection.size() <= 0) {
return dataList;
}
collection.forEach(item->{
dataList.add(operation.get(item));
});
return dataList;
}
/**
* 獲得快取的list物件
*
* @param keyLike 模糊路徑查詢 *
* @return 快取鍵值對應的資料
*/
public <T> List<T> getCacheListByLike(String keyLike) {
List<T> dataList = new ArrayList<T>();
Collection<String> keys = keys(keyLike);
return mgetCacheList(keys);
}
/**
* 快取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);
}
}
5.測試
package com;
import com.ruoyi.RuoYiApplication;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.service.ISysDeptService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Collection;
import java.util.List;
/**
* Created by 甘小濤哇 on 2020/4/27
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class)
public class Test {
@Autowired
private RedisCache redisCache;
@Autowired
private ISysDeptService sysDeptService;
@org.junit.Test
public void test(){
List<SysDept> sysDepts = sysDeptService.selectDeptList(null);
for (SysDept sysDept : sysDepts) {
redisCache.setCacheObject("cache:dept:"+sysDept.getDeptId(),sysDept);
}
List<SysDept> list1 = redisCache.getCacheListByLike("cach*");
redisCache.deleteByLike("cach*");
System.out.println();
}
}
相關文章
- 若依管理系統前端實踐前端
- 若依管理系統視訊教程
- 若依管理系統-免驗證介面實現
- 若依管理系統RuoYi-Vue(二):許可權系統設計詳解Vue
- window系統使用Tomcat部署若依微服務Tomcat微服務
- 若依管理系統RuoYi-Vue(一):專案啟動和選單建立Vue
- 若依dockerDocker
- 若依框架——>三級聯動 新增,預設修改框架
- Maven依賴管理系統Maven
- 若依如何修改logoGo
- 【Java】若依(ruoyi)——4.部署Java
- 01【若依框架】 登入許可權框架
- 若依 RuoYi4.6.0 程式碼審計
- 根據若依系統+minio實現批次下載附件並自動壓縮成zip
- (二十)美萍酒店管理系統:系統維護_系統設定_房間型別_單個新增、多個新增型別
- 若依微服務版(ruoyi-cloud)使用記錄微服務Cloud
- 若依Ruoyi分離版替換 MyBatis-PlusMyBatis
- 若依微服務專案整合人大金倉記錄微服務
- 若依解決VUE前端時間顯示問題Vue前端
- C++ 編譯依賴管理系統分析以及 srcdep 介紹C++編譯
- Redis 物件系統Redis物件
- Redis物件系統Redis物件
- 【Java】若依(ruoyi)——7.程式碼生成(二)細節操作Java
- 若依框架匯入阿里OSS報錯問題解決方案框架阿里
- redis:新增redis到服務列表Redis
- 記一次攻防演練中的若依(thymeleaf 模板注入)getshell
- Ubuntu系統新增中文方式Ubuntu
- 【學生資訊管理系統】Win7防火牆新增開放埠Win7防火牆
- FreeBSD系統怎麼新增硬碟?FreeBSD系統新增硬碟的方法教程硬碟
- 依賴管理和依賴範圍
- golang 依賴管理Golang
- 【Java】若依(ruoyi)——3.專案啟動(前後端分離版)Java後端
- Maven-Maven統一管理依賴的版本Maven
- win10專業版系統怎麼新增憑據管理器Win10
- java版工程專案管理系統原始碼+系統管理+系統設定+專案管理Java專案管理原始碼
- 能耗管理系統,能源管理系統的意義
- Linux系統管理-系統概述Linux
- 百度技術委員會主席廖若雪推薦《推薦系統》