feat():增加缓存代码示例
parent
9b41098f32
commit
2a3d68f51d
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
<artifactId>cloud-common-cache</artifactId>
|
<artifactId>cloud-common-cache</artifactId>
|
||||||
<description>
|
<description>
|
||||||
cloud-common-cache 本地换存
|
cloud-common-cache 缓存基准
|
||||||
</description>
|
</description>
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>17</maven.compiler.source>
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
@ -19,28 +19,12 @@
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
|
||||||
<groupId>com.google.guava</groupId>
|
|
||||||
<artifactId>guava</artifactId>
|
|
||||||
<version>33.0.0-jre</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
|
<!-- 基础缓存模块 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>logkit</groupId>
|
<groupId>com.muyu</groupId>
|
||||||
<artifactId>logkit</artifactId>
|
<artifactId>cloud-common-redis</artifactId>
|
||||||
<version>1.0.1</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.projectlombok</groupId>
|
|
||||||
<artifactId>lombok</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
|
||||||
<artifactId>caffeine</artifactId>
|
|
||||||
<version>2.9.3</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.muyu.common.cache;
|
||||||
|
|
||||||
|
import com.muyu.common.cache.decoration.DecorationKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 原子序列缓存基准
|
||||||
|
* @Date 2024-4-1 下午 08:07
|
||||||
|
*/
|
||||||
|
public interface AtomicSequenceCache<K> extends DecorationKey<K> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取存储的值
|
||||||
|
* @param key 键
|
||||||
|
* @return 值
|
||||||
|
*/
|
||||||
|
public Long get(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自增
|
||||||
|
*/
|
||||||
|
public Long increment(K key);
|
||||||
|
/**
|
||||||
|
* 自减
|
||||||
|
*/
|
||||||
|
public Long decrement(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 增加数值
|
||||||
|
*/
|
||||||
|
public Long increment(K key, Long number);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 减少数值
|
||||||
|
*/
|
||||||
|
public Long decrement(K key, Long number);
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.muyu.common.cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据转换接口
|
||||||
|
* @param <K> 数据键
|
||||||
|
* @param <V> 数据值
|
||||||
|
*/
|
||||||
|
public interface BasicCacheData <K,V>{
|
||||||
|
|
||||||
|
public V apply(K key);
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.muyu.common.cache;
|
||||||
|
|
||||||
|
import com.muyu.common.cache.decoration.DecorationKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 缓存接口基类
|
||||||
|
* @Date 2024-3-26 下午 03:25
|
||||||
|
*/
|
||||||
|
public interface Cache <K, V> extends DecorationKey<K> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Key获取value值
|
||||||
|
* @param key 键
|
||||||
|
* @return 值
|
||||||
|
*/
|
||||||
|
public V get(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存添加/修改
|
||||||
|
* @param key 键
|
||||||
|
* @param value 值
|
||||||
|
*/
|
||||||
|
public void put(K key, V value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过键删除
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
public void remove(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新缓存时间
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
public void refreshTime (K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新缓存数据
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
public void refreshData (K key);
|
||||||
|
}
|
|
@ -1,37 +0,0 @@
|
||||||
package com.muyu.common.cache;
|
|
||||||
|
|
||||||
import com.github.benmanes.caffeine.cache.Cache;
|
|
||||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
public class CaffeineTest {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Cache<String, String> cache = Caffeine.newBuilder()
|
|
||||||
.initialCapacity(5)
|
|
||||||
// 超出时淘汰
|
|
||||||
.maximumSize(10)
|
|
||||||
//设置写缓存后n秒钟过期
|
|
||||||
.expireAfterWrite(60, TimeUnit.SECONDS)
|
|
||||||
//设置读写缓存后n秒钟过期,实际很少用到,类似于expireAfterWrite
|
|
||||||
//.expireAfterAccess(17, TimeUnit.SECONDS)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
String orderId = String.valueOf(123456789);
|
|
||||||
String orderInfo = cache.get(orderId, key -> getInfo(key));
|
|
||||||
System.out.println(orderInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String getInfo(String orderId) {
|
|
||||||
String info = "";
|
|
||||||
// 先查询redis缓存
|
|
||||||
log.info("get data from redis");
|
|
||||||
|
|
||||||
// 当redis缓存不存在查db
|
|
||||||
log.info("get data from mysql");
|
|
||||||
info = String.format("{orderId=%s}", orderId);
|
|
||||||
return info;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,36 +0,0 @@
|
||||||
package com.muyu.common.cache;
|
|
||||||
|
|
||||||
import com.google.common.cache.Cache;
|
|
||||||
import com.google.common.cache.CacheBuilder;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
public class GuavaCacheTest {
|
|
||||||
public static void main(String[] args) throws ExecutionException {
|
|
||||||
Cache<String, String> cache = CacheBuilder.newBuilder()
|
|
||||||
.initialCapacity(5) // 初始容量
|
|
||||||
.maximumSize(10) // 最大缓存数,超出淘汰
|
|
||||||
.expireAfterWrite(60, TimeUnit.SECONDS) // 过期时间
|
|
||||||
.build();
|
|
||||||
|
|
||||||
String orderId = String.valueOf(123456789);
|
|
||||||
// 获取orderInfo,如果key不存在,callable中调用getInfo方法返回数据
|
|
||||||
String orderInfo = cache.get(orderId, () -> getInfo(orderId));
|
|
||||||
log.info("orderInfo = {}", orderInfo);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String getInfo(String orderId) {
|
|
||||||
String info = "";
|
|
||||||
// 先查询redis缓存
|
|
||||||
log.info("get data from redis");
|
|
||||||
|
|
||||||
// 当redis缓存不存在查db
|
|
||||||
log.info("get data from mysql");
|
|
||||||
info = String.format("{orderId=%s}", orderId);
|
|
||||||
return info;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,104 @@
|
||||||
|
package com.muyu.common.cache;
|
||||||
|
|
||||||
|
import com.muyu.common.cache.decoration.DecorationKey;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: Hash缓存基准
|
||||||
|
* @Date 2024-3-29 下午 03:16
|
||||||
|
*/
|
||||||
|
public interface HashCache <K, HK, HV> extends DecorationKey<K> {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编码
|
||||||
|
* @param hashKey ID
|
||||||
|
* @return 键
|
||||||
|
*/
|
||||||
|
public String encodeHashKey(HK hashKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解码
|
||||||
|
* @param redisHashKey 数据库键
|
||||||
|
* @return ID
|
||||||
|
*/
|
||||||
|
public HK decodeHashKey(String redisHashKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Key获取所有的map
|
||||||
|
* @param key 数据库键
|
||||||
|
* @return 所有集合Map
|
||||||
|
*/
|
||||||
|
public Map<HK, HV> get(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过键和hashKey获取数据库hashValue
|
||||||
|
* @param key 键
|
||||||
|
* @param hashKey hash键
|
||||||
|
* @return hash值
|
||||||
|
*/
|
||||||
|
public HV get(K key, HK hashKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过键和hashKey获取数据库hashValue
|
||||||
|
* @param key 键
|
||||||
|
* @param hashKeyList hash键集合
|
||||||
|
* @return hash值
|
||||||
|
*/
|
||||||
|
public List<HV> get(K key, HK... hashKeyList);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取hash值集合
|
||||||
|
* @param key 键
|
||||||
|
* @return hash值集合
|
||||||
|
*/
|
||||||
|
public List<HV> getToList(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储数据
|
||||||
|
* @param key redis键
|
||||||
|
* @param map hashMap集合
|
||||||
|
*/
|
||||||
|
public void put(K key, Map<HK, HV> map);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储数据
|
||||||
|
* @param key redis键
|
||||||
|
* @param dataList 数据值
|
||||||
|
* @param hashKey hash键
|
||||||
|
*/
|
||||||
|
public void put(K key, List<HV> dataList, Function<HV, HK> hashKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储数据
|
||||||
|
* @param key redis键
|
||||||
|
* @param hashKey hash键
|
||||||
|
* @param hashValue hash值
|
||||||
|
*/
|
||||||
|
public void put(K key, HK hashKey, HV hashValue);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过redis键删除
|
||||||
|
* @param key hash键
|
||||||
|
*/
|
||||||
|
public void remove(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过redis键和hash键删除
|
||||||
|
* @param key redis键
|
||||||
|
* @param hashKey hash键
|
||||||
|
*/
|
||||||
|
public void remove(K key, HK hashKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断redis中hashKey是否存在
|
||||||
|
* @param key redis键
|
||||||
|
* @param hashKey hash键
|
||||||
|
*/
|
||||||
|
public boolean hasKey(K key, HK hashKey);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,99 @@
|
||||||
|
package com.muyu.common.cache.abs;
|
||||||
|
|
||||||
|
import com.muyu.common.cache.AtomicSequenceCache;
|
||||||
|
import com.muyu.common.redis.service.RedisService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 原子序列缓存抽象类
|
||||||
|
* @Date 2024-4-1 下午 08:33
|
||||||
|
*/
|
||||||
|
public abstract class AtomicSequenceCacheAbs<K> implements AtomicSequenceCache<K> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisService redisService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取存储的值
|
||||||
|
* @param key 键
|
||||||
|
* @return 值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Long get (K key) {
|
||||||
|
Long cacheValue = this.redisService.getCacheObject(encode(key));
|
||||||
|
if (cacheValue == null){
|
||||||
|
Long data = getData(key);
|
||||||
|
cacheValue = data == null ? 0L : data;
|
||||||
|
this.redisService.setCacheObject(encode(key), cacheValue);
|
||||||
|
}
|
||||||
|
return cacheValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自增
|
||||||
|
* @param key
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Long increment (K key) {
|
||||||
|
return this.increment(key, 1L);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自减
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Long decrement (K key) {
|
||||||
|
return this.decrement(key, 1L);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 增加数值
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* @param number
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Long increment (K key, Long number) {
|
||||||
|
Long numberValue = redisService.getCacheObject(encode(key));
|
||||||
|
if (numberValue == null){
|
||||||
|
Long data = getData(key);
|
||||||
|
data = data == null ? 0L : data;
|
||||||
|
redisService.setCacheObject(encode(key), data);
|
||||||
|
}
|
||||||
|
return redisService.increment(encode(key), number);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 减少数值
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* @param number
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Long decrement (K key, Long number) {
|
||||||
|
Long numberValue = redisService.getCacheObject(encode(key));
|
||||||
|
if (numberValue == null){
|
||||||
|
Long data = getData(key);
|
||||||
|
data = data == null ? 0L : data;
|
||||||
|
redisService.setCacheObject(encode(key), data);
|
||||||
|
}
|
||||||
|
return redisService.decrement(encode(key), number);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编码
|
||||||
|
*
|
||||||
|
* @param key ID
|
||||||
|
*
|
||||||
|
* @return 键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String encode (K key) {
|
||||||
|
return keyPre() + key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract Long getData(K key);
|
||||||
|
}
|
|
@ -0,0 +1,96 @@
|
||||||
|
package com.muyu.common.cache.abs;
|
||||||
|
|
||||||
|
import com.muyu.common.cache.Cache;
|
||||||
|
import com.muyu.common.redis.service.RedisService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 缓存抽象类
|
||||||
|
* @Date 2024-3-27 下午 03:10
|
||||||
|
*/
|
||||||
|
public abstract class CacheAbs<K, V> implements Cache<K, V> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisService redisService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编码
|
||||||
|
* @param key ID
|
||||||
|
* @return 键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String encode (K key) {
|
||||||
|
return keyPre() + key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Key获取value值
|
||||||
|
* @param key 键
|
||||||
|
* @return 值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public V get (K key) {
|
||||||
|
V value = redisService.getCacheObject(encode(key));
|
||||||
|
if (value == null){
|
||||||
|
value = getData(key);
|
||||||
|
if (value == null){
|
||||||
|
value = defaultValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.put(key, value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存添加/修改
|
||||||
|
* @param key 键
|
||||||
|
* @param value 值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void put (K key, V value) {
|
||||||
|
this.redisService.setCacheObject(encode(key), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过键删除
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void remove (K key) {
|
||||||
|
this.redisService.deleteObject(encode(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新缓存
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void refreshTime (K key) {
|
||||||
|
this.redisService.expire(encode(key), 60, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新缓存数据
|
||||||
|
*
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void refreshData (K key) {
|
||||||
|
this.put(key, getData(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从数据库获取数据
|
||||||
|
* @param key ID
|
||||||
|
* @return 缓存对象
|
||||||
|
*/
|
||||||
|
public abstract V getData(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认值
|
||||||
|
*/
|
||||||
|
public abstract V defaultValue();
|
||||||
|
}
|
|
@ -0,0 +1,223 @@
|
||||||
|
package com.muyu.common.cache.abs;
|
||||||
|
|
||||||
|
import com.muyu.common.cache.HashCache;
|
||||||
|
import com.muyu.common.redis.service.RedisService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: hash缓存抽象类
|
||||||
|
* @Date 2024-3-29 下午 07:40
|
||||||
|
*/
|
||||||
|
public abstract class HashCacheAbs<K, HK, HV> implements HashCache<K, HK, HV> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisService redisService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编码
|
||||||
|
*
|
||||||
|
* @param key ID
|
||||||
|
*
|
||||||
|
* @return 键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String encode (K key) {
|
||||||
|
return keyPre() + key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编码
|
||||||
|
* @param hashKey ID
|
||||||
|
* @return 键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String encodeHashKey (HK hashKey) {
|
||||||
|
return hashKey.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Key获取所有的map
|
||||||
|
* @param key 数据库键
|
||||||
|
* @return 所有集合Map
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Map<HK, HV> get (K key) {
|
||||||
|
// 获取为null的情况
|
||||||
|
Map<String, HV> cacheMap = redisService.getCacheMap(encode(key));
|
||||||
|
if (cacheMap == null || cacheMap.isEmpty()){
|
||||||
|
Map<HK, HV> dataMap = getData(key);
|
||||||
|
if (dataMap != null && !dataMap.isEmpty()){
|
||||||
|
cacheMap = encodeMap(dataMap);
|
||||||
|
}else {
|
||||||
|
cacheMap = encodeMap(defaultValue());
|
||||||
|
}
|
||||||
|
redisService.setCacheMap(encode(key), cacheMap);
|
||||||
|
}
|
||||||
|
return decodeMap(cacheMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过键和hashKey获取数据库hashValue
|
||||||
|
*
|
||||||
|
* @param key 键
|
||||||
|
* @param hashKey hash键
|
||||||
|
*
|
||||||
|
* @return hash值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public HV get (K key, HK hashKey) {
|
||||||
|
HV hashValue = redisService.getCacheMapValue(encode(key), encodeHashKey(hashKey));
|
||||||
|
if (hashValue == null){
|
||||||
|
HV dataValue = getData(key, hashKey);
|
||||||
|
hashValue = dataValue != null ? dataValue : defaultHashValue();
|
||||||
|
put(key, hashKey, hashValue);
|
||||||
|
}
|
||||||
|
return hashValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过键和hashKey获取数据库hashValue
|
||||||
|
*
|
||||||
|
* @param key 键
|
||||||
|
* @param hashKeyList hash键集合
|
||||||
|
*
|
||||||
|
* @return hash值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<HV> get (K key, HK... hashKeyList) {
|
||||||
|
List<String> encodeHashKeyList = Arrays.stream(hashKeyList).map(this::encodeHashKey).toList();
|
||||||
|
return redisService.getMultiCacheMapValue(encode(key), encodeHashKeyList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取hash值集合
|
||||||
|
*
|
||||||
|
* @param key 键
|
||||||
|
*
|
||||||
|
* @return hash值集合
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<HV> getToList (K key) {
|
||||||
|
Map<HK, HV> hkhvMap = get(key);
|
||||||
|
return hkhvMap.values().stream().toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储数据
|
||||||
|
*
|
||||||
|
* @param key redis键
|
||||||
|
* @param map hashMap集合
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void put (K key, Map<HK, HV> map) {
|
||||||
|
redisService.setCacheMap(encode(key), encodeMap(map));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储数据
|
||||||
|
*
|
||||||
|
* @param key redis键
|
||||||
|
* @param dataList 数据值
|
||||||
|
* @param hashKey hash键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void put (K key, List<HV> dataList, Function<HV, HK> hashKey) {
|
||||||
|
Map<HK, HV> dataMap = new HashMap<>();
|
||||||
|
dataList.forEach((data) -> dataMap.put(hashKey.apply(data), data));
|
||||||
|
redisService.setCacheMap(encode(key), encodeMap(dataMap));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储数据
|
||||||
|
*
|
||||||
|
* @param key redis键
|
||||||
|
* @param hashKey hash键
|
||||||
|
* @param hashValue hash值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void put (K key, HK hashKey, HV hashValue) {
|
||||||
|
redisService.setCacheMapValue(encode(key), encodeHashKey(hashKey), hashValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过redis键删除
|
||||||
|
*
|
||||||
|
* @param key hash键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void remove (K key) {
|
||||||
|
redisService.deleteObject(encode(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过redis键和hash键删除
|
||||||
|
*
|
||||||
|
* @param key redis键
|
||||||
|
* @param hashKey hash键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void remove (K key, HK hashKey) {
|
||||||
|
redisService.deleteCacheMapValue(encode(key), encodeHashKey(hashKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断redis中hashKey是否存在
|
||||||
|
*
|
||||||
|
* @param key redis键
|
||||||
|
* @param hashKey hash键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean hasKey (K key, HK hashKey) {
|
||||||
|
return redisService.hashKey(encode(key), encodeHashKey(hashKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 原始数据转编码数据
|
||||||
|
* @param dataMap 原始数据
|
||||||
|
* @return 编码数据
|
||||||
|
*/
|
||||||
|
private Map<String, HV> encodeMap(Map<HK, HV> dataMap){
|
||||||
|
Map<String, HV> encodeDataMap = new HashMap<>();
|
||||||
|
dataMap.forEach((hashKey, HashValue) -> encodeDataMap.put(encodeHashKey(hashKey), HashValue));
|
||||||
|
return encodeDataMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编码数据转原始数据
|
||||||
|
* @param encodeDataMap 编码数据
|
||||||
|
* @return 原始数据
|
||||||
|
*/
|
||||||
|
private Map<HK, HV> decodeMap(Map<String, HV> encodeDataMap){
|
||||||
|
Map<HK, HV> dataMap = new HashMap<>();
|
||||||
|
encodeDataMap.forEach((hashKey, hashValue) -> dataMap.put(decodeHashKey(hashKey), hashValue));
|
||||||
|
return dataMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过键获取所有的hash数据
|
||||||
|
* @param key 键
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public abstract Map<HK, HV> getData(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过缓存键和hash键获取hash值
|
||||||
|
* @param key 缓存键
|
||||||
|
* @param hashKey hash键
|
||||||
|
* @return hash值
|
||||||
|
*/
|
||||||
|
public abstract HV getData(K key, HK hashKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认值
|
||||||
|
*/
|
||||||
|
public abstract Map<HK, HV> defaultValue();
|
||||||
|
public abstract HV defaultHashValue();
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.muyu.common.cache.decoration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 装饰Key
|
||||||
|
* @Date 2024-3-29 下午 03:19
|
||||||
|
*/
|
||||||
|
public interface DecorationKey <K>{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* key前缀
|
||||||
|
* @return key前缀
|
||||||
|
*/
|
||||||
|
public String keyPre();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编码
|
||||||
|
* @param key ID
|
||||||
|
* @return 键
|
||||||
|
*/
|
||||||
|
public String encode(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解码
|
||||||
|
* @param redisKey 数据库键
|
||||||
|
* @return ID
|
||||||
|
*/
|
||||||
|
public K decode(String redisKey);
|
||||||
|
}
|
|
@ -261,4 +261,23 @@ public class RedisService {
|
||||||
public Collection<String> keys (final String pattern) {
|
public Collection<String> keys (final String pattern) {
|
||||||
return redisTemplate.keys(pattern);
|
return redisTemplate.keys(pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 减少序列值
|
||||||
|
* @param key key
|
||||||
|
* @param number 值
|
||||||
|
* @return 操作后的值
|
||||||
|
*/
|
||||||
|
public Long decrement (final String key, Long number) {
|
||||||
|
return redisTemplate.opsForValue().decrement(key,number);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 增加序列值
|
||||||
|
* @param key key
|
||||||
|
* @param number 值
|
||||||
|
* @return 操作后的值
|
||||||
|
*/
|
||||||
|
public Long increment (final String key, Long number) {
|
||||||
|
return redisTemplate.opsForValue().increment(key,number);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>cloud-modules-openbusiness</artifactId>
|
||||||
|
<version>3.6.3</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>cloud-modules-openbusiness-cache</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- 缓存基准 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>cloud-common-cache</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 企业公共包 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>cloud-modules-openbusiness-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 企业公共包 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>cloud-modules-openbusiness-remote</artifactId>
|
||||||
|
<version>3.6.3</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.muyu.openbusiness;
|
||||||
|
|
||||||
|
import com.muyu.common.cache.BasicCacheData;
|
||||||
|
import com.muyu.common.cache.abs.CacheAbs;
|
||||||
|
import com.muyu.openbusiness.domain.SysCar;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class VehicleCacheService extends CacheAbs<String, SysCar> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BasicCacheData<String, SysCar> basicCacheData;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SysCar getData(String key) {
|
||||||
|
return basicCacheData.apply(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SysCar defaultValue() {
|
||||||
|
return SysCar.builder().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String keyPre() {
|
||||||
|
return "car:info:";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String decode(String redisKey) {
|
||||||
|
return redisKey.replace(keyPre(), "");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.muyu.openbusiness.apply;
|
||||||
|
|
||||||
|
import com.muyu.common.cache.BasicCacheData;
|
||||||
|
import com.muyu.openbusiness.domain.SysCar;
|
||||||
|
import com.muyu.openbusiness.remote.SysCarRemoteService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 键和值转换 - 车辆数据
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class SysCarCacheRemoteData implements BasicCacheData<String, SysCar> {
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SysCarRemoteService sysCarRemoteService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SysCar apply(String key) {
|
||||||
|
return sysCarRemoteService.findByVin(key).getData();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,93 +0,0 @@
|
||||||
package com.muyu.openbusiness.DTO;//package com.muyu.breakdown.DTO;
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//import com.muyu.openbusiness.domain.Messages;
|
|
||||||
//import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
//import org.springframework.stereotype.Component;
|
|
||||||
//
|
|
||||||
//import java.sql.*;
|
|
||||||
//import java.util.*;
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * @ Tool:IntelliJ IDEA
|
|
||||||
// * @ Author:CHX
|
|
||||||
// * @ Date:2024-09-18-15:00
|
|
||||||
// * @ Version:1.0
|
|
||||||
// * @ Description:数据库连接层
|
|
||||||
// * @author Lenovo
|
|
||||||
// */
|
|
||||||
//@Component
|
|
||||||
//public class MessageDTO {
|
|
||||||
// private static final String DB_URL = "jdbc:mysql://106.54.193.225:3306/one";
|
|
||||||
// private static final String USER = "root";
|
|
||||||
// private static final String PASSWORD = "bawei2112A";
|
|
||||||
//
|
|
||||||
// // 2. 建立数据库连接
|
|
||||||
// Connection connection;
|
|
||||||
// // 构造函数,初始化数据库连接
|
|
||||||
// // 保存消息到数据库
|
|
||||||
// public void saveMessage(Messages message) {
|
|
||||||
// String sql = "INSERT INTO sys_messages (sender_id, receiver_id, content) VALUES (?, ?, ?)";
|
|
||||||
// try {
|
|
||||||
// Class.forName("com.mysql.cj.jdbc.Driver");
|
|
||||||
// } catch (ClassNotFoundException e) {
|
|
||||||
// throw new RuntimeException(e);
|
|
||||||
// }
|
|
||||||
// try {
|
|
||||||
// connection = DriverManager.getConnection(DB_URL, USER, PASSWORD);
|
|
||||||
// } catch (SQLException e) {
|
|
||||||
// throw new RuntimeException(e);
|
|
||||||
// }
|
|
||||||
// try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
|
||||||
// preparedStatement.setInt(1, message.getSenderId());
|
|
||||||
// preparedStatement.setInt(2, message.getReceiverId());
|
|
||||||
// preparedStatement.setString(3, message.getContent());
|
|
||||||
// // 执行添加操作
|
|
||||||
// preparedStatement.executeUpdate();
|
|
||||||
// } catch (SQLException e) {
|
|
||||||
// throw new RuntimeException(e);
|
|
||||||
// }
|
|
||||||
// try {
|
|
||||||
// connection.close();
|
|
||||||
// } catch (SQLException e) {
|
|
||||||
// throw new RuntimeException(e);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // 获取所有消息
|
|
||||||
// public List<Messages> getAllMessages(int receiverId){
|
|
||||||
// String sql = "SELECT * FROM sys_messages WHERE receiver_id = ?";
|
|
||||||
// try {
|
|
||||||
// Class.forName("com.mysql.cj.jdbc.Driver");
|
|
||||||
// } catch (ClassNotFoundException e) {
|
|
||||||
// throw new RuntimeException(e);
|
|
||||||
// }
|
|
||||||
// List<Messages> messages = new ArrayList<>();
|
|
||||||
// try {
|
|
||||||
// connection = DriverManager.getConnection(DB_URL, USER, PASSWORD);
|
|
||||||
// } catch (SQLException e) {
|
|
||||||
// throw new RuntimeException(e);
|
|
||||||
// }
|
|
||||||
// try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
|
||||||
// preparedStatement.setInt(1, receiverId);
|
|
||||||
// // 执行查询操作
|
|
||||||
// ResultSet rs = preparedStatement.executeQuery();
|
|
||||||
// while (rs.next()) {
|
|
||||||
// Messages message = new Messages(rs.getInt("sender_id"), receiverId, rs.getString("content"));
|
|
||||||
//
|
|
||||||
// // 添加到消息列表
|
|
||||||
// messages.add(message);
|
|
||||||
// }
|
|
||||||
// } catch (SQLException e) {
|
|
||||||
// throw new RuntimeException(e);
|
|
||||||
// }
|
|
||||||
// try {
|
|
||||||
// connection.close();
|
|
||||||
// } catch (SQLException e) {
|
|
||||||
// throw new RuntimeException(e);
|
|
||||||
// }
|
|
||||||
// // 返回消息列表
|
|
||||||
// return messages;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//}
|
|
|
@ -13,6 +13,8 @@ import lombok.NoArgsConstructor;
|
||||||
import lombok.experimental.SuperBuilder;
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
|
* 故障实体类
|
||||||
* @author Lenovo
|
* @author Lenovo
|
||||||
* @ Tool:IntelliJ IDEA
|
* @ Tool:IntelliJ IDEA
|
||||||
* @ Author:CHX
|
* @ Author:CHX
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
package com.muyu.openbusiness.domain;
|
|
||||||
|
|
||||||
import io.micrometer.observation.annotation.Observed;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import lombok.ToString;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@ToString
|
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
|
||||||
public class Person {
|
|
||||||
private String person;
|
|
||||||
private Integer Age;
|
|
||||||
private String name;
|
|
||||||
private String Nation;
|
|
||||||
private String Animal;
|
|
||||||
private Integer id;
|
|
||||||
}
|
|
|
@ -1,4 +0,0 @@
|
||||||
package com.muyu.openbusiness.domain;
|
|
||||||
|
|
||||||
public interface RedisService {
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
package com.muyu.openbusiness.domain;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
|
|
||||||
public class SerializeUtil {
|
|
||||||
public static byte[] serialize(Object object){
|
|
||||||
ObjectOutputStream oos =null;
|
|
||||||
ByteArrayOutputStream baos =null;
|
|
||||||
try{
|
|
||||||
//序列化
|
|
||||||
baos =new ByteArrayOutputStream();
|
|
||||||
oos =new ObjectOutputStream(baos);
|
|
||||||
oos.writeObject(object);
|
|
||||||
byte[] bytes =baos.toByteArray();
|
|
||||||
return bytes;
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
public static Object unserialize(byte[] bytes) throws IOException {
|
|
||||||
ByteArrayInputStream bais =null;
|
|
||||||
bais =new ByteArrayInputStream(bytes);
|
|
||||||
ObjectInputStream ois =new ObjectInputStream(bais);
|
|
||||||
return ois.available();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,38 +0,0 @@
|
||||||
package com.muyu.openbusiness.domain;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.IdType;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
|
||||||
import com.muyu.common.core.annotation.Excel;
|
|
||||||
import lombok.*;
|
|
||||||
import lombok.experimental.SuperBuilder;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@Setter
|
|
||||||
@Getter
|
|
||||||
@SuperBuilder
|
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
|
||||||
@TableName("sys_corpuscle_fence")
|
|
||||||
public class SysFenceRail {
|
|
||||||
private static final long seriaversionUID =1L;
|
|
||||||
/** 自增主键 */
|
|
||||||
@TableId(type = IdType.AUTO)
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
/** 围栏编码 */
|
|
||||||
@Excel(name="围栏编码")
|
|
||||||
private String fenceCode;
|
|
||||||
/**围栏名称**/
|
|
||||||
@Excel(name = "围栏名称")
|
|
||||||
private String fenceName;
|
|
||||||
|
|
||||||
/**围栏类型**/
|
|
||||||
@Excel(name = "围栏类型")
|
|
||||||
private Long fenceType;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>cloud-modules-openbusiness</artifactId>
|
||||||
|
<version>3.6.3</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>cloud-modules-openbusiness-remote</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- 企业公共包 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>cloud-modules-openbusiness-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.muyu.openbusiness.remote;
|
||||||
|
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
import com.muyu.openbusiness.domain.SysCar;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
|
||||||
|
@FeignClient()
|
||||||
|
public interface SysCarRemoteService {
|
||||||
|
|
||||||
|
public Result<SysCar> findByVin(String vin);
|
||||||
|
}
|
|
@ -106,8 +106,12 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.muyu</groupId>
|
<groupId>com.muyu</groupId>
|
||||||
<artifactId>cloud-modules-openbusiness-common</artifactId>
|
<artifactId>cloud-modules-openbusiness-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>cloud-modules-openbusiness-cache</artifactId>
|
||||||
<version>3.6.3</version>
|
<version>3.6.3</version>
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
@ -117,10 +121,6 @@
|
||||||
<version>3.6.3</version>
|
<version>3.6.3</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.muyu.openbusiness.cahce.apply;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.muyu.common.cache.BasicCacheData;
|
||||||
|
import com.muyu.openbusiness.domain.SysCar;
|
||||||
|
import com.muyu.openbusiness.remote.SysCarRemoteService;
|
||||||
|
import com.muyu.openbusiness.service.impl.SysCarServiceImpl;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 键和值转换 - 车辆数据
|
||||||
|
*/
|
||||||
|
@Primary
|
||||||
|
@Component
|
||||||
|
public class SysCarCacheRemoteData implements BasicCacheData<String, SysCar> {
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SysCarServiceImpl sysCarService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SysCar apply(String key) {
|
||||||
|
return sysCarService.getOne(
|
||||||
|
new LambdaQueryWrapper<>(){{
|
||||||
|
eq(SysCar::getCarVin, key);
|
||||||
|
}}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,12 +0,0 @@
|
||||||
//package com.muyu.openbusiness.feign;
|
|
||||||
//
|
|
||||||
//import com.muyu.common.core.domain.Result;
|
|
||||||
//import com.muyu.common.system.remote.factory.RemoteUserFallbackFactory;
|
|
||||||
//import org.springframework.cloud.openfeign.FeignClient;
|
|
||||||
//import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
//
|
|
||||||
//@FeignClient(name = "cloud-system",fallbackFactory = RemoteUserFallbackFactory.class)
|
|
||||||
//public interface SystemFeign {
|
|
||||||
// @GetMapping("/user/getInfo")
|
|
||||||
// public Result getInfo ();
|
|
||||||
//}
|
|
|
@ -1,153 +0,0 @@
|
||||||
package com.muyu.openbusiness.redis;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson2.JSON;
|
|
||||||
import com.muyu.openbusiness.constant.RedisConstant;
|
|
||||||
import com.muyu.openbusiness.domain.VehicleMessage;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class RedisInitialize {
|
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private RedisTemplate<String,String>redisTemplate;
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void a() {
|
|
||||||
|
|
||||||
new Thread(()->{
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000);
|
|
||||||
}catch (Exception exception){
|
|
||||||
throw new RuntimeException(exception);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
VehicleMessage message1 = new VehicleMessage();
|
|
||||||
message1.setStartTime(System.currentTimeMillis());
|
|
||||||
message1.setSpeed("50");
|
|
||||||
message1.setLongitude("126.397428");
|
|
||||||
message1.setLatitude("37.90923");
|
|
||||||
message1.setTotalMileage("1010");
|
|
||||||
message1.setTotalVoltage("22.5");
|
|
||||||
message1.setAcceleratorPedalTravelValue("1.5");
|
|
||||||
message1.setBrakePedalTravelValue("1.2");
|
|
||||||
message1.setSpecificFuelConsumption("1.8");
|
|
||||||
message1.setMotorControllerTemperature("59");
|
|
||||||
message1.setMotorSpeed("850");
|
|
||||||
message1.setMotorTorque("110");
|
|
||||||
message1.setMotorTemperature("53");
|
|
||||||
message1.setMotorVoltage("12.5");
|
|
||||||
message1.setMotorCurrent("1.1");
|
|
||||||
message1.setPowerBatteryRemainingSOC("88");
|
|
||||||
message1.setMaximumPower("999");
|
|
||||||
message1.setMaximumDischargePower("950");
|
|
||||||
message1.setDcdc("2");
|
|
||||||
message1.setChg("2");
|
|
||||||
message1.setBMSSelfCheckCounter("2");
|
|
||||||
message1.setElectricCurrent("2.3");
|
|
||||||
message1.setTotalVoltageV3("13.1");
|
|
||||||
message1.setSingleMaximumVoltage("14.1");
|
|
||||||
message1.setMinimumVoltageOfABattery("12.2");
|
|
||||||
message1.setMaximumBatteryTemperature("85");
|
|
||||||
message1.setMinimumBatteryTemperature("51");
|
|
||||||
message1.setPowerBatteryAvailableCapacity("560");
|
|
||||||
message1.setCombinedCurrent("1.1");
|
|
||||||
message1.setRunningState("2");
|
|
||||||
message1.setWorkStatus("2");
|
|
||||||
message1.setDriveMotorCondition("1");
|
|
||||||
message1.setVehicleStatus("1");
|
|
||||||
message1.setChargingState("1");
|
|
||||||
message1.setHeatingState("1");
|
|
||||||
message1.setCarVin("1HGCM826X3A004352");
|
|
||||||
|
|
||||||
redisTemplate.opsForValue().set(RedisConstant.VEHICLE_ENTERPRISE + message1.getCarVin(), JSON.toJSONString(message1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void initialize() {
|
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
// new Thread(() -> {
|
|
||||||
// try {
|
|
||||||
// Thread.sleep(500);
|
|
||||||
// } catch (InterruptedException e) {
|
|
||||||
// throw new RuntimeException(e);
|
|
||||||
// }
|
|
||||||
// List<VehicleMessageMiddle> vehicleMessageMiddleList = vehicleMessageMiddleService.list();
|
|
||||||
// vehicleMessageMiddleList.forEach(vehicleMessageMiddle -> {
|
|
||||||
// List<MessageDetail> messageDetailList = messageDetailService.list(new LambdaQueryWrapper<>() {{
|
|
||||||
// in(MessageDetail::getId, Arrays.asList(vehicleMessageMiddle.getMessageIds().split(",")));
|
|
||||||
// }});
|
|
||||||
// String jsonString = JSON.toJSONString(messageDetailList);
|
|
||||||
// redisTemplate.opsForHash().put(RedisConstant.MESSAGE_DETAIL, vehicleMessageMiddle.getCarVin(), jsonString);
|
|
||||||
// });
|
|
||||||
// });
|
|
||||||
|
|
||||||
// MessageDetail messageDetail = new MessageDetail();
|
|
||||||
// messageDetail.setKeyCode("1");
|
|
||||||
// messageDetail.setLabel("测试");
|
|
||||||
// messageDetail.setStartBit(0);
|
|
||||||
// messageDetail.setStopBit(8);
|
|
||||||
// messageDetail.setType("1");
|
|
||||||
|
|
||||||
// List<VehicleMessageMiddle> list = vehicleMessageMiddleService.list();
|
|
||||||
// list.forEach(vehicleMessageMiddle -> {
|
|
||||||
// List<MessageDetail> messageDetailList = messageDetailService.list(new LambdaQueryWrapper<>() {{
|
|
||||||
// in(MessageDetail::getId, Arrays.asList(vehicleMessageMiddle.getMessageIds().split(",")));
|
|
||||||
// });
|
|
||||||
// String jsonString = JSON.toJSONString(messageDetailList);
|
|
||||||
// redisTemplate.opsForHash().put(RedisConstant.VEHICLE_ENTERPRISE, message1.getCarVin(), String.valueOf(jsonString));
|
|
||||||
|
|
||||||
new Thread(()->{
|
|
||||||
try {
|
|
||||||
Thread.sleep(500);
|
|
||||||
}catch (Exception exception){
|
|
||||||
throw new RuntimeException(exception);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
VehicleMessage message1 = new VehicleMessage();
|
|
||||||
message1.setStartTime(System.currentTimeMillis());
|
|
||||||
message1.setSpeed("50");
|
|
||||||
message1.setLongitude("116.397428");
|
|
||||||
message1.setLatitude("39.90923");
|
|
||||||
message1.setTotalMileage("1000");
|
|
||||||
message1.setTotalVoltage("12.5");
|
|
||||||
message1.setAcceleratorPedalTravelValue("0.5");
|
|
||||||
message1.setBrakePedalTravelValue("0.2");
|
|
||||||
message1.setSpecificFuelConsumption("0.8");
|
|
||||||
message1.setMotorControllerTemperature("60");
|
|
||||||
message1.setMotorSpeed("800");
|
|
||||||
message1.setMotorTorque("100");
|
|
||||||
message1.setMotorTemperature("70");
|
|
||||||
message1.setMotorVoltage("12.6");
|
|
||||||
message1.setMotorCurrent("1.2");
|
|
||||||
message1.setPowerBatteryRemainingSOC("80");
|
|
||||||
message1.setMaximumPower("1000");
|
|
||||||
message1.setMaximumDischargePower("900");
|
|
||||||
message1.setDcdc("1");
|
|
||||||
message1.setChg("1");
|
|
||||||
message1.setBMSSelfCheckCounter("1");
|
|
||||||
message1.setElectricCurrent("2.5");
|
|
||||||
message1.setTotalVoltageV3("13.5");
|
|
||||||
message1.setSingleMaximumVoltage("14.5");
|
|
||||||
message1.setMinimumVoltageOfABattery("12.0");
|
|
||||||
message1.setMaximumBatteryTemperature("80");
|
|
||||||
message1.setMinimumBatteryTemperature("50");
|
|
||||||
message1.setPowerBatteryAvailableCapacity("800");
|
|
||||||
message1.setCombinedCurrent("1.5");
|
|
||||||
message1.setRunningState("1");
|
|
||||||
message1.setWorkStatus("1");
|
|
||||||
message1.setDriveMotorCondition("1");
|
|
||||||
message1.setVehicleStatus("1");
|
|
||||||
message1.setChargingState("1");
|
|
||||||
message1.setHeatingState("1");
|
|
||||||
message1.setCarVin("1HGCM826X3A004352");
|
|
||||||
|
|
||||||
redisTemplate.opsForValue().set(RedisConstant.VEHICLE_ENTERPRISE + message1.getCarVin(), JSON.toJSONString(message1));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -16,6 +16,8 @@
|
||||||
<modules>
|
<modules>
|
||||||
<module>cloud-modules-openbusiness-server</module>
|
<module>cloud-modules-openbusiness-server</module>
|
||||||
<module>cloud-modules-openbusiness-common</module>
|
<module>cloud-modules-openbusiness-common</module>
|
||||||
|
<module>cloud-modules-openbusiness-cache</module>
|
||||||
|
<module>cloud-modules-openbusiness-remote</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|
|
@ -16,7 +16,9 @@ import com.muyu.system.domain.SysMenu;
|
||||||
import com.muyu.system.domain.req.SysFirmReq;
|
import com.muyu.system.domain.req.SysFirmReq;
|
||||||
import com.muyu.system.service.ISysFirmService;
|
import com.muyu.system.service.ISysFirmService;
|
||||||
import com.muyu.system.service.SysEntService;
|
import com.muyu.system.service.SysEntService;
|
||||||
|
import com.muyu.system.service.impl.SysConfigServiceImpl;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.core.io.support.EncodedResource;
|
import org.springframework.core.io.support.EncodedResource;
|
||||||
import org.springframework.jdbc.datasource.init.ScriptUtils;
|
import org.springframework.jdbc.datasource.init.ScriptUtils;
|
||||||
|
@ -59,6 +61,8 @@ public class SysFirmController extends BaseController
|
||||||
|
|
||||||
|
|
||||||
private static final String FIRM_CREDIT_CODE = "^[0-9A-HJ-NPQRTUWXY]{2}\\d{6}[0-9A-HJ-NPQRTUWXY]{10}$";
|
private static final String FIRM_CREDIT_CODE = "^[0-9A-HJ-NPQRTUWXY]{2}\\d{6}[0-9A-HJ-NPQRTUWXY]{10}$";
|
||||||
|
@Autowired
|
||||||
|
private SysConfigServiceImpl sysConfigServiceImpl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询企业基础信息列表
|
* 查询企业基础信息列表
|
||||||
|
@ -119,33 +123,8 @@ public class SysFirmController extends BaseController
|
||||||
return error("新增 企业基础信息 '" + sysFirm + "'失败,统一社会信用代码格式不正确");
|
return error("新增 企业基础信息 '" + sysFirm + "'失败,统一社会信用代码格式不正确");
|
||||||
}
|
}
|
||||||
|
|
||||||
//添加角色与角色权限
|
|
||||||
SysRole role = new SysRole();
|
|
||||||
role.setFirmCode(firmCode);
|
|
||||||
role.setRoleName(firmName+"超级管理员");
|
|
||||||
role.setRoleKey(firmCode+"_admin");
|
|
||||||
role.setRoleSort(1);
|
|
||||||
role.setDataScope("1");
|
|
||||||
role.setMenuCheckStrictly(true);
|
|
||||||
role.setDeptCheckStrictly(true);
|
|
||||||
role.setStatus("0");
|
|
||||||
role.setDelFlag("0");
|
|
||||||
SysMenu sysMenu = new SysMenu();
|
|
||||||
Result<List<SysMenu>> sysMenus = sysMenuController.list(sysMenu);
|
|
||||||
List<SysMenu> menulist = sysMenus.getData();
|
|
||||||
Long[] menuIds = menulist.stream()
|
|
||||||
.map(SysMenu::getMenuId)
|
|
||||||
.filter(Objects::nonNull) // 过滤掉 null 值
|
|
||||||
.toArray(Long[]::new);
|
|
||||||
role.setCreateBy(SecurityUtils.getUsername());
|
|
||||||
role.setMenuIds(menuIds);
|
|
||||||
|
|
||||||
sysRoleController.add(role);
|
|
||||||
|
|
||||||
//添加部门
|
//添加部门
|
||||||
SysDept sysDept = new SysDept();
|
SysDept sysDept = new SysDept();
|
||||||
Result list = sysDeptController.list(sysDept);
|
|
||||||
List<SysDept> depts = (List<SysDept>) list.getData();
|
|
||||||
sysDept.setFirmCode(firmCode);
|
sysDept.setFirmCode(firmCode);
|
||||||
sysDept.setParentId(100L);
|
sysDept.setParentId(100L);
|
||||||
sysDept.setDeptName(firmName);
|
sysDept.setDeptName(firmName);
|
||||||
|
@ -155,7 +134,6 @@ public class SysFirmController extends BaseController
|
||||||
sysDept.setDelFlag("0");
|
sysDept.setDelFlag("0");
|
||||||
|
|
||||||
sysDeptController.add(sysDept);
|
sysDeptController.add(sysDept);
|
||||||
SysDept sysDepts = depts.get(depts.size() - 1);
|
|
||||||
|
|
||||||
// 设置创建者和状态
|
// 设置创建者和状态
|
||||||
sysFirm.setCreateBy(SecurityUtils.getUsername());
|
sysFirm.setCreateBy(SecurityUtils.getUsername());
|
||||||
|
@ -163,18 +141,11 @@ public class SysFirmController extends BaseController
|
||||||
//添加用户与用户角色
|
//添加用户与用户角色
|
||||||
SysUser sysUser = new SysUser();
|
SysUser sysUser = new SysUser();
|
||||||
sysUser.setFirmCode(firmCode);
|
sysUser.setFirmCode(firmCode);
|
||||||
sysUser.setUserName(sysFirm.getUserName());
|
sysUser.setUserName(sysFirm.getFirmCode()+"-admin");
|
||||||
sysUser.setNickName(sysFirm.getUserName());
|
sysUser.setNickName(sysFirm.getFirmName()+"管理员");
|
||||||
sysUser.setPassword(sysFirm.getPassword());
|
sysUser.setPassword(sysConfigServiceImpl.selectConfigByKey("sys.user.initPassword"));
|
||||||
sysUser.setDeptId(sysDept.getDeptId());
|
sysUser.setDeptId(sysDept.getDeptId());
|
||||||
sysUser.setDept(sysDepts);
|
sysUser.setRoleIds(new Long[]{119L});
|
||||||
Result<TableDataInfo<SysRole>> resultRole = sysRoleController.list(role);
|
|
||||||
List<SysRole> roleList = resultRole.getData().getRows();
|
|
||||||
Long[] roleIds = roleList.stream()
|
|
||||||
.map(SysRole::getRoleId)
|
|
||||||
.filter(Objects::nonNull) // 过滤掉 null 值
|
|
||||||
.toArray(Long[]::new);
|
|
||||||
sysUser.setRoleIds(new Long[]{roleIds[roleList.size() - 1]});
|
|
||||||
sysUserController.add(sysUser);
|
sysUserController.add(sysUser);
|
||||||
|
|
||||||
// 创建数据库和表
|
// 创建数据库和表
|
||||||
|
@ -196,7 +167,7 @@ public class SysFirmController extends BaseController
|
||||||
|
|
||||||
sysFirm.setMemberId(1L);
|
sysFirm.setMemberId(1L);
|
||||||
|
|
||||||
return toAjax(sysFirmService.save((SysFirm) sysFirm));
|
return toAjax(sysFirmService.save(sysFirm));
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
@ -2,12 +2,11 @@ package com.muyu.system.domain.req;
|
||||||
|
|
||||||
import com.muyu.common.core.annotation.Excel;
|
import com.muyu.common.core.annotation.Excel;
|
||||||
import com.muyu.system.domain.SysFirm;
|
import com.muyu.system.domain.SysFirm;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
import lombok.experimental.SuperBuilder;
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Setter
|
|
||||||
@Getter
|
|
||||||
@SuperBuilder
|
@SuperBuilder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@ -18,6 +17,10 @@ public class SysFirmReq extends SysFirm {
|
||||||
*/
|
*/
|
||||||
@Excel(name = "登录名称")
|
@Excel(name = "登录名称")
|
||||||
private String userName;
|
private String userName;
|
||||||
|
|
||||||
|
/** 统一社会信用代码 */
|
||||||
|
@NotBlank(message = "统一社会信用代码不可为空")
|
||||||
|
private String firmCreditCode;
|
||||||
/**
|
/**
|
||||||
* 密码
|
* 密码
|
||||||
*/
|
*/
|
||||||
|
|
14
pom.xml
14
pom.xml
|
@ -296,6 +296,20 @@
|
||||||
<artifactId>cloud-modules-data-processing</artifactId>
|
<artifactId>cloud-modules-data-processing</artifactId>
|
||||||
<version>${muyu.version}</version>
|
<version>${muyu.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 缓存基准 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>cloud-common-cache</artifactId>
|
||||||
|
<version>${muyu.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 企业公共包 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>cloud-modules-openbusiness-common</artifactId>
|
||||||
|
<version>${muyu.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue