初始代码

This commit is contained in:
wangmingwei
2026-04-21 17:41:09 +08:00
parent 186ec6683a
commit b686ecac5f
493 changed files with 52349 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
<?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">
<parent>
<artifactId>yunzhupaas-boot-common</artifactId>
<groupId>com.yunzhupaas</groupId>
<version>5.2.0-RELEASE</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yunzhupaas-common-redis</artifactId>
<dependencies>
<dependency>
<groupId>com.yunzhupaas</groupId>
<artifactId>yunzhupaas-common-core</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>lock4j-redisson-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,50 @@
package com.yunzhupaas.config;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import java.nio.charset.Charset;
/**
*
* @author 云筑产品开发平台组
* @version V3.1.0
* @copyright 深圳市乐程软件有限公司
* @date 2024/3/16 10:51
*/
public class FastJsonRedis<T> implements RedisSerializer<T>
{
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class<T> clazz;
public FastJsonRedis(Class<T> clazz)
{
super();
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException
{
if (t == null)
{
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException
{
if (bytes == null || bytes.length <= 0)
{
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return (T) JSON.parseObject(str, clazz);
}
}

View File

@@ -0,0 +1,51 @@
package com.yunzhupaas.config;
import com.baomidou.lock.aop.LockAnnotationAdvisor;
import com.baomidou.lock.aop.LockInterceptor;
import org.springframework.aop.Pointcut;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
/**
* Lock4jAop开关
*/
@Configuration(proxyBeanMethods = false)
public class Lock4jAutoConfiguration {
@Bean
@ConditionalOnProperty(prefix = "lock4j.aop", name = "enabled", havingValue = "true", matchIfMissing = false)
public LockAnnotationAdvisor lockAnnotationAdvisor(LockInterceptor lockInterceptor) {
return new LockAnnotationAdvisor(lockInterceptor, Ordered.HIGHEST_PRECEDENCE){
private final AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
{
pointcut.setExpression("within(com.yunzhupaas..*) && @annotation(com.baomidou.lock.annotation.Lock4j)");
}
@Override
public Pointcut getPointcut() {
return pointcut;
}
};
}
@Bean
@ConditionalOnProperty(prefix = "lock4j.aop", name = "enabled", havingValue = "false", matchIfMissing = true)
public LockAnnotationAdvisor lockAnnotationAdvisor2(LockInterceptor lockInterceptor) {
return new LockAnnotationAdvisor(lockInterceptor, Integer.MIN_VALUE) {
private final AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
{
this.pointcut.setExpression("within(com.yunzhupaas.config.*) && @annotation(com.baomidou.lock.annotation.Lock4j)");
}
public Pointcut getPointcut() {
return this.pointcut;
}
};
}
}

View File

@@ -0,0 +1,101 @@
package com.yunzhupaas.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
*
* @author 云筑产品开发平台组
* @version V3.1.0
* @copyright 深圳市乐程软件有限公司
* @date 2024/3/16 10:51
*/
@Configuration
public class RedisConfig {
/**
* 注入 RedisConnectionFactory
*/
@Autowired
private RedisConnectionFactory factory;
/**
* 实例化 RedisTemplate 对象
*
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
Jackson2JsonRedisSerializer<?> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
/**
* 实例化 HashOperations 对象,可以使用 Hash 类型操作
*
* @param redisTemplate
* @return
*/
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
/**
* 实例化 ValueOperations 对象,可以使用 String 操作
*
* @param redisTemplate
* @return
*/
@Bean
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForValue();
}
/**
* 实例化 ListOperations 对象,可以使用 List 操作
*
* @param redisTemplate
* @return
*/
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
/**
* 实例化 SetOperations 对象,可以使用 Set 操作
*
* @param redisTemplate
* @return
*/
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
/**
* 实例化 ZSetOperations 对象,可以使用 ZSet 操作
*
* @param redisTemplate
* @return
*/
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}

View File

@@ -0,0 +1,31 @@
package com.yunzhupaas.config;
import com.yunzhupaas.consts.RedisConst;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
/**
* Redis 监听配置
*/
@Configuration(proxyBeanMethods = false)
public class RedisListenerConfig {
@Bean
public RedisConst getRedisConst(RedisProperties redisProperties){
return new RedisConst(redisProperties);
}
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}

View File

@@ -0,0 +1,18 @@
package com.yunzhupaas.consts;
import com.yunzhupaas.util.CacheKeyUtil;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
public class RedisConst {
public RedisConst(RedisProperties redisProperties) {
REDIS_EVENT_KEY = "__keyevent@" + redisProperties.getDatabase() + "__:";
}
public static String REDIS_EVENT_KEY;
public static String REDIS_LOCK4J_PREFIX = CacheKeyUtil.LOCK;
}

View File

@@ -0,0 +1,396 @@
package com.yunzhupaas.util;
import com.yunzhupaas.consts.RedisConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
*
* @author 云筑产品开发平台组
* @version V3.1.0
* @copyright 深圳市乐程软件有限公司
* @date 2024/3/16 10:51
*/
@Slf4j
@Component
public class RedisUtil {
/**
* 默认缓存时间 60S
*/
public final static int CAHCETIME = 60;
/**
* 默认缓存时间 1hr
*/
public final static int CAHCEHOUR = 60 * 60;
/**
* 默认缓存时间 1Day
*/
public final static int CAHCEDAY = 60 * 60 * 24;
/**
* 默认缓存时间 1week
*/
public final static int CAHCEWEEK = 60 * 60 * 24 * 7;
/**
* 默认缓存时间 1month
*/
public final static int CAHCEMONTH = 60 * 60 * 24 * 7 * 30;
/**
* 默认缓存时间 1年
*/
public final static int CAHCEYEAR = 60 * 60 * 24 * 7 * 30 * 12;
private static long expiresIn = TimeUnit.SECONDS.toSeconds(CAHCEHOUR * 8);
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private CacheKeyUtil cacheKeyUtil;
// =============================common============================
/**
* 获取所有的key
*
* @return
*/
public Set<String> getAllVisiualKeys() {
Set<String> allKey = new HashSet<>(16);
allKey.addAll(Objects.requireNonNull(redisTemplate.keys("*" + cacheKeyUtil.getAllUser() + "*")));
allKey.addAll(Objects.requireNonNull(redisTemplate.keys("*" + cacheKeyUtil.getCompanySelect() + "*")));
allKey.addAll(Objects.requireNonNull(redisTemplate.keys("*" + cacheKeyUtil.getDictionary() + "*")));
allKey.addAll(Objects.requireNonNull(redisTemplate.keys("*" + cacheKeyUtil.getDynamic() + "*")));
allKey.addAll(Objects.requireNonNull(redisTemplate.keys("*" + cacheKeyUtil.getOrganizeList() + "*")));
allKey.addAll(Objects.requireNonNull(redisTemplate.keys("*" + cacheKeyUtil.getPositionList() + "*")));
allKey.addAll(Objects.requireNonNull(redisTemplate.keys("*" + cacheKeyUtil.getVisiualData() + "*")));
return allKey;
}
/**
* 获取所有的key
*
* @return
*/
public Set<String> getAllKeys() {
Set<String> keys = redisTemplate.keys("*");
if (CollectionUtils.isNotEmpty(keys)) {
// 排除ID生成器的缓存记录 如果删除在集群情况下ID生成器的机器编号可能会重复导致生成的ID重复
keys = keys.stream().filter(
s -> !s.startsWith(CacheKeyUtil.IDGENERATOR) && !s.startsWith(RedisConst.REDIS_LOCK4J_PREFIX))
.collect(Collectors.toSet());
}
return keys;
}
/**
* 返回 key 的剩余的过期时间
*
* @param key
* @return
*/
public Long getLiveTime(String key) {
return redisTemplate.getExpire(key);
}
/**
* 删除指定key
*
* @param key
*/
public void remove(String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
/**
* 删除全部redis
*/
public void removeAll() {
Set<String> keys = getAllKeys();
if (CollectionUtils.isNotEmpty(keys)) {
// 兼容Redis集群 不同的KEY在不同服务器上不允许同时联合操作
keys.forEach(k -> redisTemplate.delete(k));
}
}
/**
* 判断缓存中是否有对应的value
*
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 指定缓存失效时间
*
* @param key
* @param time
* @return
*/
public void expire(String key, long time) {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
} else {
redisTemplate.expire(key, expiresIn, TimeUnit.SECONDS);
}
}
/**
* 插入缓存(无时间)
*
* @param key
* @param object
*/
public void insert(String key, Object object) {
insert(key, object, 0);
}
/**
* 插入缓存(有时间)
*
* @param key
* @param object
*/
public void insert(String key, Object object, long time) {
if (object instanceof Map) {
redisTemplate.opsForHash().putAll(key, (Map<String, String>) object);
} else if (object instanceof List) {
redisTemplate.opsForList().rightPushAll(key, (List) object);
} else if (object instanceof Set) {
redisTemplate.opsForSet().add(key, ((Set<?>) object).toArray());
} else {
redisTemplate.opsForValue().set(key, toJson(object));
}
expire(key, time);
}
/**
* Object转成JSON数据
*/
private String toJson(Object object) {
if (object instanceof Integer || object instanceof Long || object instanceof Float ||
object instanceof Double || object instanceof Boolean || object instanceof String) {
return String.valueOf(object);
}
return JsonUtil.getObjectToString(object);
}
/**
* 修改key
*
* @param oldKey 旧的key
* @param newKey 新的key
*/
public void rename(String oldKey, String newKey) {
redisTemplate.rename(oldKey, newKey);
}
/**
* 返回key存储的类型
*
* @param key
*/
public String getType(String key) {
return redisTemplate.type(key).code();
}
// ============================String=============================
/**
* 获取redis的String值
*
* @param key
* @return
*/
public Object getString(String key) {
return redisTemplate.opsForValue().get(key);
}
// ============================Map=============================
/**
* 判断hash表中是否有对应的value
*
* @param hashId
* @param key
* @return
*/
public boolean hasKey(String hashId, String key) {
return redisTemplate.opsForHash().hasKey(hashId, key);
}
/**
* 获取hashKey对应的所有键
*
* @param hashId 键
*/
public List<String> getHashKeys(String hashId) {
List<String> list = new ArrayList<>();
Map<Object, Object> map = this.getMap(hashId);
for (Object object : map.keySet()) {
if (object instanceof String) {
list.add(String.valueOf(object));
}
}
return list;
}
/**
* 获取hashKey对应的所有值
*
* @param hashId 键
*/
public List<String> getHashValues(String hashId) {
List<String> list = new ArrayList<>();
Map<Object, Object> map = this.getMap(hashId);
for (Object object : map.keySet()) {
if (map.get(object) instanceof String) {
list.add(String.valueOf(map.get(object)));
}
}
return list;
}
/**
* 查询具体map的值
*
* @param hashId
* @param key
* @return
*/
public String getHashValues(String hashId, String key) {
Object object = redisTemplate.opsForHash().get(hashId, key);
if (object != null) {
return String.valueOf(object);
} else {
return null;
}
}
/**
* 删除指定map的key
*
* @param key
*/
public void removeHash(String hashId, String key) {
if (hasKey(hashId, key)) {
redisTemplate.opsForHash().delete(hashId, key);
}
}
/**
* 获取所有的map缓存
*
* @param key
* @return
*/
public <K, V> Map<K, V> getMap(String key) {
return (Map<K, V>) redisTemplate.opsForHash().entries(key);
}
/**
* 插入map的值
*
* @param hashId 主键id
* @param key map的key
* @param value map的值
*/
public void insertHash(String hashId, String key, String value) {
redisTemplate.opsForHash().put(hashId, key, value);
}
// ============================set=============================
/**
* 根据key获取Set中的所有值
*
* @param key 键
* @return
*/
public Set<Object> getSet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
log.error(key, e);
return null;
}
}
/**
* 获取set缓存的长度
*
* @param key 键
* @return
*/
public long getSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
log.error(key, e);
return 0;
}
}
// ===============================list=================================
/**
* 获取list缓存的内容
*
* @param key 键
* @param start 开始 0 是第一个元素
* @param end 结束 -1代表所有值
* @return
* @取出来的元素 总数 end-start+1
*/
public List<Object> get(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
log.error(key, e);
return null;
}
}
/**
* 获取list缓存的长度
*
* @param key 键
* @return
*/
public long getListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
log.error(key, e);
return 0;
}
}
/**
* 通过索引 获取list中的值
*
* @param key 键
* @param index 索引 index>=0时 0 表头1 第二个元素依次类推index<0时-1表尾-2倒数第二个元素依次类推
* @return
*/
public Object getIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
log.error(key, e);
return null;
}
}
}