temp
This commit is contained in:
67
urbanLifelineServ/common/common-redis/pom.xml
Normal file
67
urbanLifelineServ/common/common-redis/pom.xml
Normal file
@@ -0,0 +1,67 @@
|
||||
<?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>org.xyzh</groupId>
|
||||
<artifactId>common</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.xyzh.common</groupId>
|
||||
<artifactId>common-redis</artifactId>
|
||||
<version>${urban-lifeline.version}</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring Boot Redis Starter -->
|
||||
<!-- Redis 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
<!-- 排除默认的logback依赖 -->
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Data Redis -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- FastJSON2 for JSON serialization -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Common Core dependency -->
|
||||
<dependency>
|
||||
<groupId>org.xyzh.common</groupId>
|
||||
<artifactId>common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Context for @Component annotation -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot Auto Configuration -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.xyzh.redis.config;
|
||||
|
||||
import org.xyzh.common.core.constant.Constants;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.SerializationException;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONReader;
|
||||
import com.alibaba.fastjson2.JSONWriter;
|
||||
import com.alibaba.fastjson2.filter.Filter;
|
||||
|
||||
/**
|
||||
* @description FastJson2JsonRedisSerializer.java文件描述
|
||||
* @filename FastJson2JsonRedisSerializer.java
|
||||
* @author yslg
|
||||
* @copyright yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
|
||||
{
|
||||
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
|
||||
|
||||
static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter(Constants.JSON_WHITELIST_STR);
|
||||
|
||||
private Class<T> clazz;
|
||||
|
||||
public FastJson2JsonRedisSerializer(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, JSONWriter.Feature.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 JSON.parseObject(str, clazz, AUTO_TYPE_FILTER);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.xyzh.redis.config;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @description RedisConfig.java文件描述 Redis配置
|
||||
* @filename RedisConfig.java
|
||||
* @author yslg
|
||||
* @copyright yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
@AutoConfigureBefore(RedisAutoConfiguration.class)
|
||||
public class RedisConfig {
|
||||
|
||||
@Bean
|
||||
@SuppressWarnings(value = { "unchecked", "rawtypes" })
|
||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory)
|
||||
{
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
||||
template.setConnectionFactory(connectionFactory);
|
||||
|
||||
FastJson2JsonRedisSerializer<Object> serializer = new FastJson2JsonRedisSerializer<>(Object.class);
|
||||
|
||||
// 使用StringRedisSerializer来序列化和反序列化redis的key值
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setValueSerializer(serializer);
|
||||
|
||||
// Hash的key也采用StringRedisSerializer的序列化方式
|
||||
template.setHashKeySerializer(new StringRedisSerializer());
|
||||
template.setHashValueSerializer(serializer);
|
||||
|
||||
template.afterPropertiesSet();
|
||||
return template;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,384 @@
|
||||
package org.xyzh.redis.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @description RedisService.java Redis工具服务类,封装常用Redis操作
|
||||
* @filename RedisService.java
|
||||
* @author yslg
|
||||
* @copyright yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
@SuppressWarnings(value = { "unchecked", "rawtypes" })
|
||||
@Component
|
||||
public class RedisService {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
/**
|
||||
* @description 设置key-value
|
||||
* @param key String 键
|
||||
* @param value Object 值
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void set(String key, Object value) {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 设置key-value并指定过期时间
|
||||
* @param key String 键
|
||||
* @param value Object 值
|
||||
* @param timeout long 过期时间
|
||||
* @param unit TimeUnit 时间单位
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void set(String key, Object value, long timeout, TimeUnit unit) {
|
||||
redisTemplate.opsForValue().set(key, value, timeout, unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取key对应的value
|
||||
* @param key String 键
|
||||
* @return Object 值
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public Object get(String key) {
|
||||
return redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 删除key
|
||||
* @param key String 键
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void delete(String key) {
|
||||
redisTemplate.delete(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 批量删除key
|
||||
* @param keys Collection<String> 键集合
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void delete(Collection<String> keys) {
|
||||
redisTemplate.delete(keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 判断key是否存在
|
||||
* @param key String 键
|
||||
* @return boolean 是否存在
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public boolean hasKey(String key) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 设置key过期时间(秒)
|
||||
* @param key String 键
|
||||
* @param timeout long 过期秒数
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void setExpire(String key, long timeout) {
|
||||
redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 设置key过期时间(自定义单位)
|
||||
* @param key String 键
|
||||
* @param timeout long 过期时间
|
||||
* @param unit TimeUnit 时间单位
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void setExpire(String key, long timeout, TimeUnit unit) {
|
||||
redisTemplate.expire(key, timeout, unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取key剩余过期时间(秒)
|
||||
* @param key String 键
|
||||
* @return long 剩余秒数
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public long getExpire(String key) {
|
||||
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 原子递增
|
||||
* @param key String 键
|
||||
* @param delta long 增量
|
||||
* @return long 递增后值
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public long incr(String key, long delta) {
|
||||
Long result = redisTemplate.opsForValue().increment(key, delta);
|
||||
return result != null ? result : 0L;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 原子递减
|
||||
* @param key String 键
|
||||
* @param delta long 减量
|
||||
* @return long 递减后值
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public long decr(String key, long delta) {
|
||||
Long result = redisTemplate.opsForValue().increment(key, -delta);
|
||||
return result != null ? result : 0L;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Hash操作-put
|
||||
* @param key String 键
|
||||
* @param hashKey String 哈希键
|
||||
* @param value Object 值
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void hSet(String key, String hashKey, Object value) {
|
||||
redisTemplate.opsForHash().put(key, hashKey, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Hash操作-get
|
||||
* @param key String 键
|
||||
* @param hashKey String 哈希键
|
||||
* @return Object 值
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public Object hGet(String key, String hashKey) {
|
||||
return redisTemplate.opsForHash().get(key, hashKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Hash操作-获取所有
|
||||
* @param key String 键
|
||||
* @return Map<Object, Object> 哈希所有键值对
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public Map<Object, Object> hGetAll(String key) {
|
||||
return redisTemplate.opsForHash().entries(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Hash操作-删除
|
||||
* @param key String 键
|
||||
* @param hashKeys String[] 哈希键数组
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void hDelete(String key, String... hashKeys) {
|
||||
redisTemplate.opsForHash().delete(key, (Object[]) hashKeys);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description List操作-左入队
|
||||
* @param key String 键
|
||||
* @param value Object 值
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void lPush(String key, Object value) {
|
||||
redisTemplate.opsForList().leftPush(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description List操作-右入队
|
||||
* @param key String 键
|
||||
* @param value Object 值
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void rPush(String key, Object value) {
|
||||
redisTemplate.opsForList().rightPush(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description List操作-左出队
|
||||
* @param key String 键
|
||||
* @return Object 出队值
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public Object lPop(String key) {
|
||||
return redisTemplate.opsForList().leftPop(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description List操作-右出队
|
||||
* @param key String 键
|
||||
* @return Object 出队值
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public Object rPop(String key) {
|
||||
return redisTemplate.opsForList().rightPop(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description List操作-获取区间元素
|
||||
* @param key String 键
|
||||
* @param start long 起始索引
|
||||
* @param end long 结束索引
|
||||
* @return List<Object> 元素列表
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public List<Object> lRange(String key, long start, long end) {
|
||||
return redisTemplate.opsForList().range(key, start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Set操作-添加元素
|
||||
* @param key String 键
|
||||
* @param values Object[] 元素数组
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void sAdd(String key, Object... values) {
|
||||
redisTemplate.opsForSet().add(key, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Set操作-移除元素
|
||||
* @param key String 键
|
||||
* @param values Object[] 元素数组
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void sRemove(String key, Object... values) {
|
||||
redisTemplate.opsForSet().remove(key, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Set操作-获取所有元素
|
||||
* @param key String 键
|
||||
* @return Set<Object> 元素集合
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public Set<Object> sMembers(String key) {
|
||||
return redisTemplate.opsForSet().members(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description ZSet操作-添加元素
|
||||
* @param key String 键
|
||||
* @param value Object 元素
|
||||
* @param score double 分数
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void zAdd(String key, Object value, double score) {
|
||||
redisTemplate.opsForZSet().add(key, value, score);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description ZSet操作-移除元素
|
||||
* @param key String 键
|
||||
* @param values Object[] 元素数组
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void zRemove(String key, Object... values) {
|
||||
redisTemplate.opsForZSet().remove(key, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description ZSet操作-按分数区间获取元素
|
||||
* @param key String 键
|
||||
* @param min double 最小分数
|
||||
* @param max double 最大分数
|
||||
* @return Set<Object> 元素集合
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public Set<Object> zRangeByScore(String key, double min, double max) {
|
||||
return redisTemplate.opsForZSet().rangeByScore(key, min, max);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description ZSet操作-获取全部元素
|
||||
* @param key String 键
|
||||
* @param start long 起始索引
|
||||
* @param end long 结束索引
|
||||
* @return Set<Object> 元素集合
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public Set<Object> zRange(String key, long start, long end) {
|
||||
return redisTemplate.opsForZSet().range(key, start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 发布消息(Pub/Sub)
|
||||
* @param channel String 通道
|
||||
* @param message Object 消息内容
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void publish(String channel, Object message) {
|
||||
redisTemplate.convertAndSend(channel, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 执行Redis原生命令
|
||||
* @param action RedisCallback<?> 回调命令
|
||||
* @return Object 执行结果
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public Object execute(RedisCallback<?> action) {
|
||||
return redisTemplate.execute(action);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取所有key(慎用)
|
||||
* @param pattern String 匹配模式
|
||||
* @return Set<String> key集合
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public Set<String> keys(String pattern) {
|
||||
Set<String> keys = redisTemplate.keys(pattern);
|
||||
Set<String> stringKeys = new HashSet<>();
|
||||
if (keys != null) {
|
||||
for (String key : keys) {
|
||||
stringKeys.add(key);
|
||||
}
|
||||
}
|
||||
return stringKeys;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 清空当前数据库(慎用)
|
||||
* @author yslg
|
||||
* @since 2025-11-02
|
||||
*/
|
||||
public void flushDb() {
|
||||
redisTemplate.execute((RedisCallback<Void>) connection -> { connection.flushDb(); return null; });
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user