Redisson
Redisson 是一个基于 Redis 的 Java 客户端,提供了丰富的分布式数据结构和服务,如分布式锁、集合、队列、Map 等。它简化了与 Redis 的交互,并且支持高可用性、分布式事务、监控等特性,非常适合构建高性能和高可扩展性的应用。
基础配置
本节用于完成 Redisson 在 Spring Boot 项目中的基础接入,包括 Maven 依赖、application.yml 配置、配置属性类以及 RedissonClient Bean 的创建。配置方式采用 Redisson 原生 YAML 内容,便于在单机、集群、哨兵等不同 Redis 部署模式之间切换。
添加依赖
在 pom.xml 中添加 Redisson Spring Boot Starter 依赖。版本号建议统一放在 properties 中,便于后续升级维护。
<!-- 项目属性 -->
<properties>
<redisson.version>3.52.0</redisson.version>
</properties>
<!-- Redisson 依赖 -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>${redisson.version}</version>
</dependency>2
3
4
5
6
7
8
9
10
编辑配置文件
Redisson 支持通过 YAML 字符串加载原生配置。这里将配置统一放在 application.yml 的 redisson.config 字段中,再通过 Config.fromYAML(...) 解析成 Redisson 配置对象。
这种方式的优点是配置结构和 Redisson 官方配置保持一致,后续从单机模式切换到集群模式时,只需要调整配置文件,不需要修改 Java 代码。
文件位置:src/main/resources/application.yml
单机模式
单机模式适用于开发环境、测试环境,或者 Redis 本身没有部署为集群的简单业务场景。需要注意的是,address 必须带上协议前缀,普通 Redis 使用 redis://,启用 TLS 的 Redis 使用 rediss://。
---
# Redisson 配置
redisson:
config: |
singleServerConfig:
# Redis 节点地址,必须包含 redis:// 或 rediss:// 协议
address: "redis://192.168.1.12:40003"
# Redis 密码;如果 Redis 未设置密码,可以删除该配置项
password: "Admin@123"
# Redis 数据库编号,单机模式支持 database 配置
database: 0
# 客户端名称,便于在 Redis CLIENT LIST 中识别连接来源
clientName: "redisson-client"
# 连接池最大连接数
connectionPoolSize: 64
# 连接池最小空闲连接数
connectionMinimumIdleSize: 24
# 空闲连接超时时间,单位:毫秒
idleConnectionTimeout: 10000
# 建立连接超时时间,单位:毫秒
connectTimeout: 5000
# Redis 命令等待超时时间,单位:毫秒
timeout: 3000
# Redis 命令重试次数
retryAttempts: 3
# Redis 命令重试间隔,单位:毫秒
retryInterval: 1500
# Redisson 业务线程数,主要用于监听器、异步回调等任务
threads: 16
# Netty IO 线程数,负责 Redis 网络通信
nettyThreads: 32
# 对象序列化方式,推荐使用 JSON,便于排查和跨语言读取
codec: !<org.redisson.codec.JsonJacksonCodec> {}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
集群模式
集群模式适用于生产环境中 Redis Cluster 部署场景。nodeAddresses 建议填写所有主从节点地址,Redisson 会根据集群拓扑自动发现主从关系并刷新节点状态。
Redis Cluster 不支持按客户端选择普通 database,因此集群配置中不要配置 database。
文件位置:src/main/resources/application.yml
---
# Redisson 配置
redisson:
config: |
clusterServersConfig:
# Redis Cluster 节点地址,建议填写全部主从节点
nodeAddresses:
- "redis://192.168.1.41:6379"
- "redis://192.168.1.42:6379"
- "redis://192.168.1.43:6379"
- "redis://192.168.1.44:6379"
- "redis://192.168.1.45:6379"
- "redis://192.168.1.46:6379"
# Redis 集群密码;如果集群未设置密码,可以删除该配置项
password: "Admin@123"
# 集群拓扑扫描间隔,单位:毫秒
scanInterval: 2000
# 读取模式:
# MASTER:只从主节点读取
# SLAVE:优先从从节点读取
# MASTER_SLAVE:主从节点都可读取
readMode: "SLAVE"
# 订阅模式:
# MASTER:只使用主节点订阅
# SLAVE:使用从节点订阅
# MASTER_SLAVE:主从节点都可订阅
subscriptionMode: "SLAVE"
# 负载均衡策略
loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
# 主节点连接池最大连接数
masterConnectionPoolSize: 64
# 从节点连接池最大连接数
slaveConnectionPoolSize: 64
# 主节点连接池最小空闲连接数
masterConnectionMinimumIdleSize: 24
# 从节点连接池最小空闲连接数
slaveConnectionMinimumIdleSize: 24
# 空闲连接超时时间,单位:毫秒
idleConnectionTimeout: 10000
# 建立连接超时时间,单位:毫秒
connectTimeout: 5000
# Redis 命令等待超时时间,单位:毫秒
timeout: 3000
# Redis 命令重试次数
retryAttempts: 3
# Redis 命令重试间隔,单位:毫秒
retryInterval: 1500
# 从节点重连间隔,单位:毫秒
failedSlaveReconnectionInterval: 3000
# 从节点健康检查间隔,单位:毫秒
failedSlaveCheckInterval: 60000
# Redisson 业务线程数,主要用于监听器、异步回调等任务
threads: 16
# Netty IO 线程数,负责 Redis 网络通信
nettyThreads: 32
# 对象序列化方式,推荐使用 JSON,便于排查和跨语言读取
codec: !<org.redisson.codec.JsonJacksonCodec> {}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
配置说明
| 配置项 | 说明 | 建议 |
|---|---|---|
address | 单机 Redis 地址 | 必须包含 redis:// 或 rediss:// |
nodeAddresses | Redis Cluster 节点地址列表 | 生产环境建议填写全部主从节点 |
password | Redis 密码 | 无密码时删除该配置项 |
database | Redis 数据库编号 | 仅单机模式使用,集群模式不要配置 |
clientName | Redis 客户端名称 | 建议配置,便于定位连接来源 |
connectionPoolSize | 单机连接池最大连接数 | 根据并发量和 Redis 承载能力调整 |
masterConnectionPoolSize | 主节点连接池最大连接数 | 集群模式使用 |
slaveConnectionPoolSize | 从节点连接池最大连接数 | 集群模式使用 |
connectionMinimumIdleSize | 单机最小空闲连接数 | 不宜过大,避免空闲连接浪费 |
masterConnectionMinimumIdleSize | 主节点最小空闲连接数 | 集群模式使用 |
slaveConnectionMinimumIdleSize | 从节点最小空闲连接数 | 集群模式使用 |
connectTimeout | 建立连接超时时间 | 网络较差时可适当增大 |
timeout | 命令等待超时时间 | 过小容易误判超时,过大影响失败响应速度 |
retryAttempts | 命令重试次数 | 常用值为 3 |
retryInterval | 命令重试间隔 | 常用值为 1000 到 1500 毫秒 |
threads | Redisson 业务线程数 | 默认可满足多数场景,高并发可适当增加 |
nettyThreads | Netty IO 线程数 | 一般按 CPU 核心数和连接规模调整 |
codec | 序列化方式 | 推荐 JsonJacksonCodec,便于查看 Redis 中的数据 |
创建配置属性
创建配置属性类,用于读取 application.yml 中 redisson 前缀下的配置内容。
文件位置:src/main/java/local/ateng/java/redis/config/RedissonProperties.java
package local.ateng.java.redis.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* Redisson 配置属性
* 用于读取配置文件中 redisson 前缀下的配置项。
*
* @author Ateng
* @since 2026-04-27
*/
@ConfigurationProperties(prefix = "redisson")
@Configuration
@Data
public class RedissonProperties {
/**
* Redisson YAML 配置内容。
* <p>
* 通常在 application.yml 中配置为完整的 Redisson YAML 字符串,
* 然后通过 {@link org.redisson.config.Config#fromYAML(String)} 解析为 Redisson 配置对象。
* </p>
*/
private String config;
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
创建客户端Bean
创建 Redisson 自动配置类,用于解析配置文件中的 YAML 内容,并注册 RedissonClient 到 Spring 容器中。
文件位置:src/main/java/local/ateng/java/redis/config/RedissonConfig.java
package local.ateng.java.redis.config;
import lombok.RequiredArgsConstructor;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JacksonCodec;
import org.redisson.codec.JsonCodec;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
/**
* Redisson 自动配置
* 用于根据配置文件创建 RedissonClient,并提供 RedisJSON 使用的 JsonCodec。
*
* @author Ateng
* @since 2026-04-27
*/
@Configuration
@RequiredArgsConstructor
public class RedissonConfig {
/**
* Redisson 配置属性。
*/
private final RedissonProperties redissonProperties;
/**
* 创建 Redisson 客户端。
* <p>
* 这里从配置文件读取 Redisson YAML 配置内容,
* 并通过 {@link Config#fromYAML(String)} 转换为 Redisson 原生配置对象。
* </p>
*
* @return RedissonClient
* @throws IOException Redisson YAML 配置解析异常
*/
@Bean
public RedissonClient redissonClient() throws IOException {
// 解析 application.yml 中配置的 Redisson YAML 内容
Config config = Config.fromYAML(redissonProperties.getConfig());
// 如果需要统一使用自定义 Jackson 序列化配置,可以启用下面这一行
// config.setCodec(new CustomJacksonCodec());
// 根据配置创建 Redisson 客户端实例
return Redisson.create(config);
}
/**
* 创建 Redisson JSON 编解码器。
* <p>
* 该 Bean 主要用于 RJsonBucket / RedisJSON 相关能力。
* 注意:这里需要使用实现 JsonCodec 的 JacksonCodec,
* 不能使用普通对象序列化用的 JsonJacksonCodec。
* </p>
*
* @return JsonCodec
*/
@Bean
public JsonCodec jsonCodec() {
return new JacksonCodec(Object.class);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
自定义序列化(可选)
ObjectMapper 构建工厂
package local.ateng.java.redis.config;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import java.io.IOException;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;
/**
* Jackson ObjectMapper 统一构建工厂。
*
* <p>
* 该工厂用于集中管理 JSON 序列化与反序列化策略,避免项目中分散配置导致行为不一致。
* 当前提供三类构建能力:
* </p>
* <ul>
* <li>默认配置:适用于通用 JSON 处理场景</li>
* <li>存储配置:适用于 Redis、数据库 JSON 字段等持久化场景</li>
* <li>Web 配置:适用于 Spring Web 接口入参与返回值场景</li>
* </ul>
*
* @author Ateng
* @since 2026-04-13
*/
public final class JacksonObjectMapperFactory {
/**
* 默认信任的业务包前缀。
*/
private static final String[] TRUSTED_BASE_PACKAGES = {
"io.github.atengk",
"local.ateng.java",
};
/**
* 默认时区标识。
*/
private static final String DEFAULT_TIME_ZONE_ID = "Asia/Shanghai";
/**
* 定义日期时间格式(精确到秒)
*/
private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 定义日期格式(不包含时间)
*/
private static final String DATE_PATTERN = "yyyy-MM-dd";
/**
* 定义时间格式(仅时间部分)
*/
private static final String TIME_PATTERN = "HH:mm:ss";
/**
* 禁止实例化。
*/
private JacksonObjectMapperFactory() {
}
/**
* 构建默认 ObjectMapper。
*
* <p>
* 默认配置仅保留所有场景都需要的公共能力,适合通用 JSON 处理。
* </p>
*
* @return 默认 ObjectMapper
*/
public static ObjectMapper buildDefaultObjectMapper() {
ObjectMapper objectMapper = baseObjectMapper();
registerDefaultJavaTimeModule(objectMapper);
return objectMapper;
}
/**
* 构建用于 Redis / 数据库存储的 ObjectMapper。
*
* <p>
* 该配置优先保证序列化结果稳定、类型信息完整、BigDecimal 精度不丢失,并支持异常对象与复杂对象结构。
* </p>
*
* @return 存储场景专用 ObjectMapper
*/
public static ObjectMapper buildStorageObjectMapper() {
ObjectMapper objectMapper = baseObjectMapper();
applyVisibility(
objectMapper,
JsonAutoDetect.Visibility.ANY,
JsonAutoDetect.Visibility.NONE,
JsonAutoDetect.Visibility.NONE,
JsonAutoDetect.Visibility.NONE,
JsonAutoDetect.Visibility.NONE
);
applyStableSerialization(objectMapper);
registerDefaultJavaTimeModule(objectMapper);
applyDefaultTyping(objectMapper);
applyThrowableMixIn(objectMapper);
return objectMapper;
}
/**
* 构建用于 Spring Web(前后端交互)的 ObjectMapper。
*
* <p>
* 该配置优先保证接口输出可读、输入兼容、反序列化安全边界清晰,适合 Controller 层统一使用。
* </p>
*
* @return Web 场景专用 ObjectMapper
*/
public static ObjectMapper buildWebObjectMapper() {
ObjectMapper objectMapper = baseObjectMapper();
applyVisibility(
objectMapper,
JsonAutoDetect.Visibility.PUBLIC_ONLY,
JsonAutoDetect.Visibility.PUBLIC_ONLY,
JsonAutoDetect.Visibility.PUBLIC_ONLY,
JsonAutoDetect.Visibility.PUBLIC_ONLY,
JsonAutoDetect.Visibility.DEFAULT
);
registerUnifiedDateTimeModule(objectMapper);
configureLegacyDateFormat(objectMapper);
applyEnumStrategy(objectMapper);
applyLenientDeserialization(objectMapper);
applyJsonReadFeature(objectMapper);
applyNumberSerialization(objectMapper);
disableStableSerializationOptions(objectMapper);
return objectMapper;
}
/**
* 构建用于审计日志(Audit Log)的 ObjectMapper。
*
* <p>
* 该配置用于日志落库、操作审计、数据变更记录等场景,
* 重点保证序列化结果具备“稳定性、完整性、可追溯性”:
* </p>
*
* @return 审计日志专用 ObjectMapper
*/
public static ObjectMapper buildAuditObjectMapper() {
ObjectMapper objectMapper = baseObjectMapper();
// 可见性:字段优先(保证完整数据输出)
applyVisibility(
objectMapper,
JsonAutoDetect.Visibility.ANY,
JsonAutoDetect.Visibility.NONE,
JsonAutoDetect.Visibility.NONE,
JsonAutoDetect.Visibility.NONE,
JsonAutoDetect.Visibility.NONE
);
applyStableSerialization(objectMapper);
registerDefaultJavaTimeModule(objectMapper);
configureLegacyDateFormat(objectMapper);
applyEnumStrategy(objectMapper);
applyNumberSerialization(objectMapper);
applyDefaultTyping(objectMapper);
return objectMapper;
}
/**
* 构建基础 ObjectMapper。
*
* <p>
* 该方法只负责装载所有场景都需要的公共能力,包括时区、时间模块、空值策略和未知字段处理策略。
* 具体差异化能力由上层构建方法按场景追加。
* </p>
*
* @return 基础 ObjectMapper
*/
private static ObjectMapper baseObjectMapper() {
// 创建 ObjectMapper 实例,用于统一 JSON 序列化与反序列化配置
ObjectMapper objectMapper = new ObjectMapper();
// 设置全局默认时区,确保时间序列化与反序列化行为一致
objectMapper.setTimeZone(TimeZone.getTimeZone(DEFAULT_TIME_ZONE_ID));
// 注册 JDK8 模块,支持 Optional 等类型
objectMapper.registerModule(new Jdk8Module());
// 注册构造参数名模块,支持基于构造方法参数名进行反序列化(需开启 -parameters 编译参数)
objectMapper.registerModule(new ParameterNamesModule());
// 禁用时间戳格式输出,统一使用字符串格式,提升可读性
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// 禁用空 Bean 序列化失败,避免无属性对象导致异常
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
// 序列化时忽略值为 null 的字段,减少无效数据输出
//objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// 保留 null 字段
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
// 反序列化时忽略未知字段,增强兼容性,避免字段扩展导致失败
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 禁用反序列化自动时区调整,避免时间偏移
objectMapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false);
// 返回基础配置完成的 ObjectMapper
return objectMapper;
}
/**
* 统一设置对象可见性策略。
*
* @param objectMapper ObjectMapper 实例
* @param fieldVisibility 字段可见性
* @param getterVisibility getter 可见性
* @param setterVisibility setter 可见性
* @param isGetterVisibility isGetter 可见性
* @param creatorVisibility 构造器可见性
*/
private static void applyVisibility(ObjectMapper objectMapper,
JsonAutoDetect.Visibility fieldVisibility,
JsonAutoDetect.Visibility getterVisibility,
JsonAutoDetect.Visibility setterVisibility,
JsonAutoDetect.Visibility isGetterVisibility,
JsonAutoDetect.Visibility creatorVisibility) {
objectMapper.setVisibility(
objectMapper.getSerializationConfig()
.getDefaultVisibilityChecker()
.withFieldVisibility(fieldVisibility)
.withGetterVisibility(getterVisibility)
.withSetterVisibility(setterVisibility)
.withIsGetterVisibility(isGetterVisibility)
.withCreatorVisibility(creatorVisibility)
);
}
/**
* 启用存储场景所需的稳定序列化能力。
*
* <p>
* 该配置用于保证序列化结果具备较强可比性和可读性,同时避免 BigDecimal 精度问题。
* </p>
*
* @param objectMapper ObjectMapper 实例
*/
private static void applyStableSerialization(ObjectMapper objectMapper) {
// 启用 BigDecimal 按原始字符串输出,避免科学计数法导致精度或格式问题
objectMapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
// 启用属性按字母排序,保证序列化结果稳定,便于缓存比对与签名计算
objectMapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
// 启用 Map 按 key 排序,确保输出顺序一致
objectMapper.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
}
/**
* 注册默认 Java 8 时间模块。
*
* <p>
* 仅用于启用 java.time 类型的基础支持,例如 LocalDate、LocalDateTime、LocalTime 等。
* 具体的日期时间格式由后续的统一时间模块单独配置。
* </p>
*
* @param objectMapper ObjectMapper 实例
*/
private static void registerDefaultJavaTimeModule(ObjectMapper objectMapper) {
objectMapper.registerModule(new JavaTimeModule());
}
/**
* 注册统一日期时间模块。
*
* <p>
* 用于统一 java.time 各类型的序列化与反序列化策略,
* 明确区分“展示格式”和“时间语义格式”,避免时区信息丢失问题。
* </p>
*
* @param objectMapper ObjectMapper 实例
*/
private static void registerUnifiedDateTimeModule(ObjectMapper objectMapper) {
// 构建统一时区(用于默认时间处理)
ZoneId zoneId = ZoneId.of(DEFAULT_TIME_ZONE_ID);
// 构建日期时间格式化器(精确到秒)
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
// 构建日期格式化器
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
// 构建时间格式化器
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(TIME_PATTERN);
// 创建 JavaTimeModule,用于统一管理时间序列化规则
JavaTimeModule module = new JavaTimeModule();
// 注册 LocalDateTime 序列化器
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
// 注册 LocalDateTime 反序列化器
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
// 注册 LocalDate 序列化器
module.addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter));
// 注册 LocalDate 反序列化器
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter));
// 注册 LocalTime 序列化器
module.addSerializer(LocalTime.class, new LocalTimeSerializer(timeFormatter));
// 注册 LocalTime 反序列化器
module.addDeserializer(LocalTime.class, new LocalTimeDeserializer(timeFormatter));
// 注册 Instant 序列化器(统一输出为 UTC 标准时间)
module.addSerializer(Instant.class, new JsonSerializer<Instant>() {
@Override
public void serialize(Instant value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
// 空值直接写 null
if (value == null) {
gen.writeNull();
return;
}
// 使用 ISO_INSTANT 格式输出(带 Z 标识)
gen.writeString(DateTimeFormatter.ISO_INSTANT.format(value));
}
});
// 注册 Instant 反序列化器(基于 ISO_INSTANT 解析)
module.addDeserializer(Instant.class, new JsonDeserializer<Instant>() {
@Override
public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
// 获取文本值
String text = p.getText();
// 空字符串返回 null
if (text == null || text.trim().isEmpty()) {
return null;
}
// 按 ISO_INSTANT 解析为 Instant
return Instant.parse(text);
}
});
// 注册 OffsetDateTime 序列化器(保留 offset 信息)
module.addSerializer(OffsetDateTime.class, new JsonSerializer<OffsetDateTime>() {
@Override
public void serialize(OffsetDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
// 空值直接写 null
if (value == null) {
gen.writeNull();
return;
}
// 使用 ISO_OFFSET_DATE_TIME 输出(包含偏移量)
gen.writeString(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(value));
}
});
// 注册 OffsetDateTime 反序列化器
module.addDeserializer(OffsetDateTime.class, new JsonDeserializer<OffsetDateTime>() {
@Override
public OffsetDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
// 获取文本值
String text = p.getText();
// 空字符串返回 null
if (text == null || text.trim().isEmpty()) {
return null;
}
// 按 ISO_OFFSET_DATE_TIME 解析
return OffsetDateTime.parse(text, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}
});
// 注册 ZonedDateTime 序列化器(保留完整时区信息)
module.addSerializer(ZonedDateTime.class, new JsonSerializer<ZonedDateTime>() {
@Override
public void serialize(ZonedDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
// 空值直接写 null
if (value == null) {
gen.writeNull();
return;
}
// 使用 ISO_ZONED_DATE_TIME 输出(包含 ZoneId)
gen.writeString(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(value));
}
});
// 注册 ZonedDateTime 反序列化器
module.addDeserializer(ZonedDateTime.class, new JsonDeserializer<ZonedDateTime>() {
@Override
public ZonedDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
// 获取文本值
String text = p.getText();
// 空字符串返回 null
if (text == null || text.trim().isEmpty()) {
return null;
}
// 按 ISO_ZONED_DATE_TIME 解析
return ZonedDateTime.parse(text, DateTimeFormatter.ISO_ZONED_DATE_TIME);
}
});
// 注册时间模块
objectMapper.registerModule(module);
// 设置全局时区
objectMapper.setTimeZone(TimeZone.getTimeZone(zoneId));
// 禁用时间戳输出(统一为字符串格式)
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// 禁用反序列化自动时区调整,避免时间偏移
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
}
/**
* 配置传统 Date 类型的全局格式。
*
* <p>
* 用于统一 java.util.Date 的序列化与反序列化行为,
* 避免与 java.time 类型出现格式不一致问题。
* </p>
*
* @param objectMapper ObjectMapper 实例
*/
private static void configureLegacyDateFormat(ObjectMapper objectMapper) {
// 创建日期格式化对象(非线程安全,但 ObjectMapper 内部会安全使用)
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TIME_PATTERN);
// 设置统一时区
dateFormat.setTimeZone(TimeZone.getTimeZone(DEFAULT_TIME_ZONE_ID));
// 应用到 ObjectMapper
objectMapper.setDateFormat(dateFormat);
}
/**
* 应用枚举序列化与反序列化策略。
*
* <p>
* 默认情况下,Jackson 使用 Enum.name() 进行序列化,
* 当枚举名称变更时容易导致反序列化失败。
* </p>
*
* @param objectMapper ObjectMapper 实例
*/
private static void applyEnumStrategy(ObjectMapper objectMapper) {
// 使用 toString() 进行序列化(而不是 name)
objectMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
// 反序列化也基于 toString
objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
}
/**
* 应用宽松反序列化策略。
*
* <p>
* 用于增强接口输入的容错能力,适用于 Web 场景,
* 避免因前端类型不规范导致反序列化失败。
* </p>
*
* @param objectMapper ObjectMapper 实例
*/
private static void applyLenientDeserialization(ObjectMapper objectMapper) {
// 允许字符串转数字("1" -> 1)
objectMapper.enable(MapperFeature.ALLOW_COERCION_OF_SCALARS);
// 允许单值当数组使用("a" -> ["a"])
objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
// 允许空字符串当 null
//objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
// 允许空数组当 null
//objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);
// 忽略枚举非法值(避免直接报错)
objectMapper.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
}
/**
* 应用 JSON 读取容错特性。
*
* <p>
* 用于兼容非标准 JSON 输入(常见于前端或第三方系统),
* 提升系统整体兼容性。
* </p>
*
* @param objectMapper ObjectMapper 实例
*/
private static void applyJsonReadFeature(ObjectMapper objectMapper) {
// 允许 JSON 末尾存在多余逗号
objectMapper.enable(JsonParser.Feature.ALLOW_TRAILING_COMMA);
// 允许 JSON 中带注释,方便开发阶段使用
objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
// 允许字段名不带引号(可处理某些特殊格式的 JSON)
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
// 允许单引号作为 JSON 字符串的定界符(适用于某些特殊格式)
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
// 允许控制字符的转义(例如,`\n` 或 `\t`)
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
// 允许反斜杠转义任何字符(如:`\\`)
objectMapper.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
// 允许无效的 UTF-8 字符(如果 JSON 编码不完全符合标准)
objectMapper.configure(JsonParser.Feature.IGNORE_UNDEFINED, true);
// 允许 JSON 中无序字段(通常是为了性能优化)
objectMapper.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true);
}
/**
* 统一数值序列化策略(安全 + 前端兼容)
*
* <p>
* 解决 Java Long / BigInteger 在前端 JS 精度丢失问题,
* </p>
*/
private static void applyNumberSerialization(ObjectMapper objectMapper) {
// 创建自定义模块,用于扩展数值序列化策略
SimpleModule module = new SimpleModule();
// 使用 ToStringSerializer,将数值序列化为字符串
ToStringSerializer stringSerializer = ToStringSerializer.instance;
// 注册 Long 包装类型序列化器
module.addSerializer(Long.class, stringSerializer);
// 注册 long 基本类型序列化器
module.addSerializer(Long.TYPE, stringSerializer);
// 注册 BigInteger 序列化器,避免精度丢失
module.addSerializer(BigInteger.class, stringSerializer);
// 防止 float/double 精度问题
objectMapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
// 防止 int 溢出
objectMapper.enable(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS);
// 将模块注册到 ObjectMapper
objectMapper.registerModule(module);
}
/**
* 关闭 Web 场景不需要的稳定排序能力。
*
* <p>
* Web 层更关注接口可读性与自然输出顺序,因此不强制属性和 Map 键排序。
* </p>
*
* @param objectMapper ObjectMapper 实例
*/
private static void disableStableSerializationOptions(ObjectMapper objectMapper) {
// 关闭属性字母排序,保留原始定义顺序,提高可读性
objectMapper.disable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
// 关闭 Map key 排序,避免影响前端展示顺序
objectMapper.disable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
}
/**
* 启用默认多态能力,并限制反序列化来源类型范围。
*
* <p>
* 仅允许业务包前缀和常用 JDK 容器类型参与 default typing,用于收敛反序列化攻击面。
* </p>
*
* @param objectMapper ObjectMapper 实例
*/
private static void applyDefaultTyping(ObjectMapper objectMapper) {
// 反序列化时遇到非法或未被允许的子类型直接失败(配合 PolymorphicTypeValidator 使用,增强多态反序列化安全性)
objectMapper.enable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);
// 启用默认多态机制,用于保留对象类型信息(适用于 Object 或抽象类型)
objectMapper.activateDefaultTyping(
// 使用受限的多态校验器,控制可反序列化的类型范围
buildPolymorphicTypeValidator(),
// 仅对非 final 类启用类型信息
ObjectMapper.DefaultTyping.NON_FINAL,
// 以 JSON 属性形式写入类型信息(默认字段为 @class)
JsonTypeInfo.As.PROPERTY
);
}
/**
* 为异常类型挂载混入配置。
*
* <p>
* 该配置用于增强 Throwable 的序列化兼容性,并降低 cause 链和对象引用环路带来的问题。
* </p>
*
* @param objectMapper ObjectMapper 实例
*/
private static void applyThrowableMixIn(ObjectMapper objectMapper) {
// 为 Throwable 类型绑定混入类,增强异常对象序列化能力
objectMapper.addMixIn(Throwable.class, ThrowableMixIn.class);
}
/**
* 构建默认多态类型校验器。
*
* <p>
* 只允许受信任业务包及常用 JDK 容器类型参与多态反序列化,避免宽松校验带来的安全风险。
* </p>
*
* @return 多态类型校验器
*/
private static PolymorphicTypeValidator buildPolymorphicTypeValidator() {
// 创建多态类型校验器构建器
BasicPolymorphicTypeValidator.Builder builder = BasicPolymorphicTypeValidator.builder();
// 遍历受信任的业务包前缀
for (String basePackage : TRUSTED_BASE_PACKAGES) {
// 允许该包路径下的所有子类型参与多态反序列化
builder.allowIfSubType(basePackage);
}
// 基础类型
builder.allowIfSubType("java.lang");
// 集合类型
builder.allowIfSubType("java.util");
// 时间类型
builder.allowIfSubType("java.time");
// 数值类型(BigDecimal / BigInteger)
builder.allowIfSubType("java.math");
// 构建并返回多态类型校验器
return builder.build();
}
/**
* 异常对象序列化混入配置。
*
* <p>
* 该混入仅用于补充 Throwable 的序列化视图,不修改业务异常本身的代码结构。
* </p>
*
* @author Ateng
* @since 2026-04-13
*/
@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY,
setterVisibility = JsonAutoDetect.Visibility.NONE,
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
creatorVisibility = JsonAutoDetect.Visibility.NONE
)
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public static class ThrowableMixIn {
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
创建序列化器
package local.ateng.java.redis.config;
import org.redisson.codec.JsonJacksonCodec;
/**
* 自定义 Redisson Jackson 编解码器
* 用于让 Redisson 普通对象序列化使用项目自定义的 ObjectMapper 配置。
*
* @author Ateng
* @since 2026-04-27
*/
public class CustomJacksonCodec extends JsonJacksonCodec {
/**
* 创建自定义 Jackson 编解码器。
* <p>
* 这里使用 {@link JacksonObjectMapperFactory#buildStorageObjectMapper()}
* 构建用于 Redis 存储的 ObjectMapper,便于统一处理时间格式、类型信息、
* 空值策略、反序列化兼容性等配置。
* </p>
*/
public CustomJacksonCodec() {
super(JacksonObjectMapperFactory.buildStorageObjectMapper());
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
创建Bean
package local.ateng.java.redisjdk8.config;
import lombok.RequiredArgsConstructor;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
@Configuration
@RequiredArgsConstructor
public class RedissonConfig {
private final RedissonProperties redissonProperties;
@Bean
public RedissonClient redissonClient() throws IOException {
Config config = Config.fromYAML(redissonProperties.getConfig());
config.setCodec(new CustomJacksonCodec());
return Redisson.create(config);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
创建Redisson Service
创建Service接口
package local.ateng.java.redis.service;
import com.fasterxml.jackson.core.type.TypeReference;
import org.redisson.api.*;
import org.redisson.api.geo.GeoSearchArgs;
import org.redisson.api.queue.*;
import org.redisson.api.queue.event.QueueEventListener;
import org.redisson.api.stream.StreamAddArgs;
import org.redisson.api.stream.StreamReadArgs;
import org.redisson.api.stream.StreamReadGroupArgs;
import org.redisson.api.stream.StreamTrimArgs;
import org.redisson.client.protocol.ScoredEntry;
import org.redisson.codec.JsonCodec;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.Duration;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* Redis 服务接口
* 基于 Spring Boot 3 + Redisson 封装 Redis 常用能力。
*
* @author Ateng
* @since 2026-04-26
*/
public interface RedissonService {
// -------------------------------------------------------------------------
// Redisson 原生对象访问
// -------------------------------------------------------------------------
/**
* 获取 RedissonClient 实例。
*
* @return RedissonClient 实例
*/
RedissonClient getClient();
/**
* 获取对象桶。
*
* @param key Redis 键
* @param <T> 值类型
* @return RBucket
*/
<T> RBucket<T> getBucket(String key);
/**
* 获取哈希 Map。
*
* @param key Redis 键
* @param <K> 字段类型
* @param <V> 值类型
* @return RMap
*/
<K, V> RMap<K, V> getMap(String key);
/**
* 获取带 TTL 能力的 MapCache。
*
* @param key Redis 键
* @param <K> 字段类型
* @param <V> 值类型
* @return RMapCache
*/
<K, V> RMapCache<K, V> getMapCache(String key);
/**
* 获取列表。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RList
*/
<T> RList<T> getList(String key);
/**
* 获取双端队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RDeque
*/
<T> RDeque<T> getDeque(String key);
/**
* 获取集合。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RSet
*/
<T> RSet<T> getSet(String key);
/**
* 获取带元素 TTL 能力的 SetCache。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RSetCache
*/
<T> RSetCache<T> getSetCache(String key);
/**
* 获取有序集合。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RScoredSortedSet
*/
<T> RScoredSortedSet<T> getScoredSortedSet(String key);
/**
* 获取 BitSet。
*
* @param key Redis 键
* @return RBitSet
*/
RBitSet getBitSet(String key);
/**
* 获取 HyperLogLog。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RHyperLogLog
*/
<T> RHyperLogLog<T> getHyperLogLog(String key);
/**
* 获取 Geo。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RGeo
*/
<T> RGeo<T> getGeo(String key);
/**
* 获取普通队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RQueue
*/
<T> RQueue<T> getQueue(String key);
/**
* 获取阻塞队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RBlockingQueue
*/
<T> RBlockingQueue<T> getBlockingQueue(String key);
/**
* 获取可靠队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RReliableQueue
*/
<T> RReliableQueue<T> getReliableQueue(String key);
/**
* 获取消息主题。
*
* @param topic 主题名称
* @return RTopic
*/
RTopic getTopic(String topic);
/**
* 获取 Stream。
*
* @param key Redis 键
* @param <K> 字段类型
* @param <V> 值类型
* @return RStream
*/
<K, V> RStream<K, V> getStream(String key);
/**
* 获取脚本执行对象。
*
* @return RScript
*/
RScript getScript();
// -------------------------------------------------------------------------
// 通用 Key 管理
// -------------------------------------------------------------------------
/**
* 判断指定 key 是否存在。
*
* @param key Redis 键
* @return 存在返回 true
*/
boolean hasKey(String key);
/**
* 统计多个 key 中实际存在的数量。
*
* @param keys Redis 键集合
* @return 存在数量
*/
long countExists(String... keys);
/**
* 删除指定 key。
*
* @param key Redis 键
* @return 是否删除成功
*/
boolean deleteKey(String key);
/**
* 批量删除 key。
*
* @param keys Redis 键集合
* @return 删除数量
*/
long deleteKeys(Collection<String> keys);
/**
* 根据通配符删除 key。
*
* @param pattern 通配符表达式
* @return 删除数量
*/
long deleteByPattern(String pattern);
/**
* 设置 key 过期时间。
*
* @param key Redis 键
* @param timeout 超时时间
* @param unit 时间单位
* @return 是否设置成功
*/
boolean expire(String key, long timeout, TimeUnit unit);
/**
* 设置 key 过期时间。
*
* @param key Redis 键
* @param ttl 过期时间
* @return 是否设置成功
*/
boolean expire(String key, Duration ttl);
/**
* 获取 key 剩余过期时间。
*
* @param key Redis 键
* @param unit 时间单位
* @return 剩余时间,-1 表示永久,-2 表示不存在
*/
long getTtl(String key, TimeUnit unit);
/**
* 移除 key 过期时间。
*
* @param key Redis 键
* @return 是否成功
*/
boolean persist(String key);
/**
* 修改 key 名称。
*
* @param oldKey 旧 key
* @param newKey 新 key
* @return 是否成功
*/
boolean renameKey(String oldKey, String newKey);
/**
* 新 key 不存在时修改 key 名称。
*
* @param oldKey 旧 key
* @param newKey 新 key
* @return 是否成功
*/
boolean renameKeyIfAbsent(String oldKey, String newKey);
/**
* 查询匹配通配符的 key。
*
* @param pattern 通配符表达式
* @return key 集合
*/
Set<String> keys(String pattern);
/**
* 查询匹配通配符的 key,并限制返回数量。
*
* @param pattern 通配符表达式
* @param count 最大数量
* @return key 集合
*/
Set<String> scanKeys(String pattern, int count);
/**
* 判断 key 是否已过期或不存在。
*
* @param key Redis 键
* @return 已过期或不存在返回 true
*/
boolean isExpired(String key);
/**
* 获取 key 类型。
*
* @param key Redis 键
* @return 类型名称
*/
String getKeyType(String key);
// -------------------------------------------------------------------------
// 类型转换
// -------------------------------------------------------------------------
/**
* 将对象转换为指定类型。
*
* @param value 原始值
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 转换后的值
*/
<T> T convertValue(Object value, Class<T> clazz);
/**
* 将对象转换为指定泛型类型。
*
* @param value 原始值
* @param typeReference 目标类型引用
* @param <T> 泛型类型
* @return 转换后的值
*/
<T> T convertValue(Object value, TypeReference<T> typeReference);
// -------------------------------------------------------------------------
// 字符串 / Bucket 操作
// -------------------------------------------------------------------------
/**
* 设置缓存值。
*
* @param key Redis 键
* @param value 缓存值
*/
void set(String key, Object value);
/**
* 设置缓存值并指定过期时间。
*
* @param key Redis 键
* @param value 缓存值
* @param timeout 超时时间
* @param unit 时间单位
*/
void set(String key, Object value, long timeout, TimeUnit unit);
/**
* 设置缓存值并指定过期时间。
*
* @param key Redis 键
* @param value 缓存值
* @param ttl 过期时间
*/
void set(String key, Object value, Duration ttl);
/**
* 获取缓存值。
*
* @param key Redis 键
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 缓存值
*/
<T> T get(String key, Class<T> clazz);
/**
* 获取缓存值。
*
* @param key Redis 键
* @param typeReference 目标泛型类型
* @param <T> 泛型类型
* @return 缓存值
*/
<T> T get(String key, TypeReference<T> typeReference);
/**
* key 不存在时设置缓存值。
*
* @param key Redis 键
* @param value 缓存值
* @param timeout 超时时间
* @param unit 时间单位
* @return 是否设置成功
*/
boolean setIfAbsent(String key, Object value, long timeout, TimeUnit unit);
/**
* key 不存在时设置缓存值。
*
* @param key Redis 键
* @param value 缓存值
* @param ttl 过期时间
* @return 是否设置成功
*/
boolean setIfAbsent(String key, Object value, Duration ttl);
/**
* key 存在时设置缓存值。
*
* @param key Redis 键
* @param value 缓存值
* @return 是否设置成功
*/
boolean setIfExists(String key, Object value);
/**
* key 存在时设置缓存值并指定过期时间。
*
* @param key Redis 键
* @param value 缓存值
* @param ttl 过期时间
* @return 是否设置成功
*/
boolean setIfExists(String key, Object value, Duration ttl);
/**
* 原子替换并返回旧值。
*
* @param key Redis 键
* @param value 新值
* @param clazz 旧值类型
* @param <T> 泛型类型
* @return 旧值
*/
<T> T getAndSet(String key, Object value, Class<T> clazz);
/**
* 原子替换并返回旧值。
*
* @param key Redis 键
* @param value 新值
* @param typeReference 旧值类型
* @param <T> 泛型类型
* @return 旧值
*/
<T> T getAndSet(String key, Object value, TypeReference<T> typeReference);
/**
* 获取并删除缓存值。
*
* @param key Redis 键
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 删除前的值
*/
<T> T getAndDelete(String key, Class<T> clazz);
/**
* 批量获取缓存值。
*
* @param keys Redis 键集合
* @return 键值 Map
*/
Map<String, Object> entries(Collection<String> keys);
/**
* 批量获取缓存值并转换类型。
*
* @param keys Redis 键集合
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 键值 Map
*/
<T> Map<String, T> entries(Collection<String> keys, Class<T> clazz);
/**
* 批量获取缓存值并转换泛型类型。
*
* @param keys Redis 键集合
* @param typeReference 目标类型
* @param <T> 泛型类型
* @return 键值 Map
*/
<T> Map<String, T> entries(Collection<String> keys, TypeReference<T> typeReference);
/**
* 获取序列化后的字节大小。
*
* @param key Redis 键
* @return 字节大小
*/
long size(String key);
// -------------------------------------------------------------------------
// 原子数值 / 计数器 / ID
// -------------------------------------------------------------------------
/**
* 获取长整型原子对象。
*
* @param key Redis 键
* @return RAtomicLong
*/
RAtomicLong getAtomicLong(String key);
/**
* 获取浮点型原子对象。
*
* @param key Redis 键
* @return RAtomicDouble
*/
RAtomicDouble getAtomicDouble(String key);
/**
* 整数自增。
*
* @param key Redis 键
* @param delta 增量
* @return 最新值
*/
long increment(String key, long delta);
/**
* 整数自减。
*
* @param key Redis 键
* @param delta 减量
* @return 最新值
*/
long decrement(String key, long delta);
/**
* 浮点数自增。
*
* @param key Redis 键
* @param delta 增量
* @return 最新值
*/
double incrementDouble(String key, double delta);
/**
* 浮点数自减。
*
* @param key Redis 键
* @param delta 减量
* @return 最新值
*/
double decrementDouble(String key, double delta);
/**
* 设置整数计数器值。
*
* @param key Redis 键
* @param value 值
*/
void setAtomicLong(String key, long value);
/**
* 获取整数计数器值。
*
* @param key Redis 键
* @return 当前值
*/
long getAtomicLongValue(String key);
/**
* 重置整数计数器。
*
* @param key Redis 键
*/
void resetAtomicLong(String key);
/**
* 获取分布式 ID 生成器。
*
* @param key Redis 键
* @return RIdGenerator
*/
RIdGenerator getIdGenerator(String key);
/**
* 初始化分布式 ID 生成器。
*
* @param key Redis 键
* @param initialValue 初始值
* @param allocationSize 每次分配步长
* @return 是否初始化成功
*/
boolean idGeneratorInit(String key, long initialValue, long allocationSize);
/**
* 获取下一个分布式 ID。
*
* @param key Redis 键
* @return ID
*/
long nextId(String key);
// -------------------------------------------------------------------------
// Hash / Map 操作
// -------------------------------------------------------------------------
/**
* 设置哈希字段值。
*
* @param key Redis 键
* @param field 字段名
* @param value 字段值
*/
void hPut(String key, String field, Object value);
/**
* 批量设置哈希字段值。
*
* @param key Redis 键
* @param map 字段 Map
*/
void hPutAll(String key, Map<String, ?> map);
/**
* 字段不存在时设置哈希字段值。
*
* @param key Redis 键
* @param field 字段名
* @param value 字段值
* @return 是否设置成功
*/
boolean hPutIfAbsent(String key, String field, Object value);
/**
* 获取哈希字段值。
*
* @param key Redis 键
* @param field 字段名
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 字段值
*/
<T> T hGet(String key, String field, Class<T> clazz);
/**
* 获取哈希字段值。
*
* @param key Redis 键
* @param field 字段名
* @param typeReference 目标类型
* @param <T> 泛型类型
* @return 字段值
*/
<T> T hGet(String key, String field, TypeReference<T> typeReference);
/**
* 批量获取哈希字段值。
*
* @param key Redis 键
* @param fields 字段集合
* @return 字段值 Map
*/
Map<String, Object> hMultiGet(String key, Collection<String> fields);
/**
* 删除哈希字段。
*
* @param key Redis 键
* @param fields 字段名
* @return 删除数量
*/
long hDelete(String key, String... fields);
/**
* 判断哈希字段是否存在。
*
* @param key Redis 键
* @param field 字段名
* @return 存在返回 true
*/
boolean hHasKey(String key, String field);
/**
* 获取哈希全部字段和值。
*
* @param key Redis 键
* @return 字段值 Map
*/
Map<String, Object> hEntries(String key);
/**
* 获取哈希全部字段和值并转换类型。
*
* @param key Redis 键
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 字段值 Map
*/
<T> Map<String, T> hEntries(String key, Class<T> clazz);
/**
* 获取哈希全部字段和值并转换泛型类型。
*
* @param key Redis 键
* @param typeReference 目标类型
* @param <T> 泛型类型
* @return 字段值 Map
*/
<T> Map<String, T> hEntries(String key, TypeReference<T> typeReference);
/**
* 获取哈希字段名集合。
*
* @param key Redis 键
* @return 字段集合
*/
Set<String> hKeys(String key);
/**
* 获取哈希字段值集合。
*
* @param key Redis 键
* @return 字段值集合
*/
Collection<Object> hValues(String key);
/**
* 获取哈希字段数量。
*
* @param key Redis 键
* @return 字段数量
*/
int hSize(String key);
/**
* 哈希字段整数自增。
*
* @param key Redis 键
* @param field 字段名
* @param delta 增量
* @return 最新值
*/
long hIncrement(String key, String field, long delta);
/**
* 哈希字段浮点数自增。
*
* @param key Redis 键
* @param field 字段名
* @param delta 增量
* @return 最新值
*/
double hIncrementDouble(String key, String field, double delta);
/**
* 清空哈希。
*
* @param key Redis 键
*/
void hClear(String key);
/**
* 设置 MapCache 字段值并指定字段级 TTL。
*
* @param key Redis 键
* @param field 字段名
* @param value 字段值
* @param ttl 字段 TTL
*/
void hcPut(String key, String field, Object value, Duration ttl);
/**
* 设置 MapCache 字段值并指定字段级 TTL 与最大空闲时间。
*
* @param key Redis 键
* @param field 字段名
* @param value 字段值
* @param ttl 字段 TTL
* @param maxIdle 最大空闲时间
*/
void hcPut(String key, String field, Object value, Duration ttl, Duration maxIdle);
/**
* 获取 MapCache 字段值。
*
* @param key Redis 键
* @param field 字段名
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 字段值
*/
<T> T hcGet(String key, String field, Class<T> clazz);
/**
* 删除 MapCache 字段。
*
* @param key Redis 键
* @param fields 字段名
* @return 删除数量
*/
long hcDelete(String key, String... fields);
// -------------------------------------------------------------------------
// List / Deque 操作
// -------------------------------------------------------------------------
/**
* 左侧压入列表。
*
* @param key Redis 键
* @param value 元素
*/
void lLeftPush(String key, Object value);
/**
* 右侧压入列表。
*
* @param key Redis 键
* @param value 元素
*/
void lRightPush(String key, Object value);
/**
* 批量右侧压入列表。
*
* @param key Redis 键
* @param values 元素集合
*/
void lRightPushAll(String key, Collection<?> values);
/**
* 左侧弹出列表元素。
*
* @param key Redis 键
* @return 元素
*/
Object lLeftPop(String key);
/**
* 左侧弹出列表元素并转换类型。
*
* @param key Redis 键
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 元素
*/
<T> T lLeftPop(String key, Class<T> clazz);
/**
* 右侧弹出列表元素。
*
* @param key Redis 键
* @return 元素
*/
Object lRightPop(String key);
/**
* 阻塞式左侧弹出列表元素。
*
* @param key Redis 键
* @param timeout 超时时间
* @param unit 时间单位
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 元素
* @throws InterruptedException 线程中断时抛出
*/
<T> T lLeftPop(String key, long timeout, TimeUnit unit, Class<T> clazz) throws InterruptedException;
/**
* 获取列表范围。
*
* @param key Redis 键
* @param start 开始索引
* @param end 结束索引
* @return 元素集合
*/
List<Object> lRange(String key, long start, long end);
/**
* 获取列表范围并转换类型。
*
* @param key Redis 键
* @param start 开始索引
* @param end 结束索引
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 元素集合
*/
<T> List<T> lRange(String key, long start, long end, Class<T> clazz);
/**
* 获取列表长度。
*
* @param key Redis 键
* @return 长度
*/
long lSize(String key);
/**
* 删除列表元素。
*
* @param key Redis 键
* @param count 删除数量规则
* @param value 元素
* @return 删除数量
*/
long lRemove(String key, long count, Object value);
/**
* 获取列表指定索引元素。
*
* @param key Redis 键
* @param index 索引
* @return 元素
*/
Object lIndex(String key, long index);
/**
* 获取列表指定索引元素并转换类型。
*
* @param key Redis 键
* @param index 索引
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 元素
*/
<T> T lIndex(String key, long index, Class<T> clazz);
/**
* 设置列表指定索引元素。
*
* @param key Redis 键
* @param index 索引
* @param value 元素
*/
void lSet(String key, long index, Object value);
/**
* 裁剪列表范围。
*
* @param key Redis 键
* @param start 开始索引
* @param end 结束索引
*/
void lTrim(String key, int start, int end);
/**
* 清空列表。
*
* @param key Redis 键
*/
void lClear(String key);
// -------------------------------------------------------------------------
// Set / SetCache 操作
// -------------------------------------------------------------------------
/**
* 添加集合元素。
*
* @param key Redis 键
* @param values 元素
* @return 是否有新增
*/
boolean sAdd(String key, Object... values);
/**
* 添加集合元素。
*
* @param key Redis 键
* @param values 元素集合
* @return 是否有新增
*/
boolean sAdd(String key, Collection<?> values);
/**
* 添加 SetCache 元素并指定元素 TTL。
*
* @param key Redis 键
* @param value 元素
* @param ttl TTL
* @return 是否添加成功
*/
boolean scAdd(String key, Object value, Duration ttl);
/**
* 判断集合中是否存在元素。
*
* @param key Redis 键
* @param value 元素
* @return 存在返回 true
*/
boolean sIsMember(String key, Object value);
/**
* 获取集合所有元素。
*
* @param key Redis 键
* @return 元素集合
*/
Set<Object> sMembers(String key);
/**
* 获取集合所有元素并转换类型。
*
* @param key Redis 键
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 元素集合
*/
<T> Set<T> sMembers(String key, Class<T> clazz);
/**
* 获取集合大小。
*
* @param key Redis 键
* @return 大小
*/
long sSize(String key);
/**
* 随机弹出集合元素。
*
* @param key Redis 键
* @return 元素
*/
Object sPop(String key);
/**
* 删除集合元素。
*
* @param key Redis 键
* @param values 元素
* @return 是否删除成功
*/
boolean sRemove(String key, Object... values);
/**
* 随机获取集合元素但不删除。
*
* @param key Redis 键
* @return 元素
*/
Object sRandomMember(String key);
/**
* 随机获取多个集合元素。
*
* @param key Redis 键
* @param count 数量
* @return 元素集合
*/
Set<Object> sRandomMembers(String key, int count);
/**
* 获取并集。
*
* @param key1 第一个 key
* @param key2 第二个 key
* @return 并集
*/
Set<Object> sUnion(String key1, String key2);
/**
* 获取交集。
*
* @param key1 第一个 key
* @param key2 第二个 key
* @return 交集
*/
Set<Object> sIntersect(String key1, String key2);
/**
* 获取差集。
*
* @param key1 第一个 key
* @param key2 第二个 key
* @return 差集
*/
Set<Object> sDifference(String key1, String key2);
/**
* 将集合并集存储到目标 key。
*
* @param destKey 目标 key
* @param keys 源 key 集合
* @return 存储数量
*/
long sUnionStore(String destKey, String... keys);
/**
* 将集合交集存储到目标 key。
*
* @param destKey 目标 key
* @param keys 源 key 集合
* @return 存储数量
*/
long sIntersectStore(String destKey, String... keys);
/**
* 将集合差集存储到目标 key。
*
* @param destKey 目标 key
* @param keys 源 key 集合
* @return 存储数量
*/
long sDifferenceStore(String destKey, String... keys);
// -------------------------------------------------------------------------
// ZSet / 排行榜操作
// -------------------------------------------------------------------------
/**
* 添加有序集合元素。
*
* @param key Redis 键
* @param value 元素
* @param score 分数
* @return 是否新增
*/
boolean zAdd(String key, Object value, double score);
/**
* 批量添加有序集合元素。
*
* @param key Redis 键
* @param scoreMap 元素分数 Map
* @return 新增数量
*/
int zAddAll(String key, Map<Object, Double> scoreMap);
/**
* 删除有序集合元素。
*
* @param key Redis 键
* @param values 元素
* @return 是否删除成功
*/
boolean zRemove(String key, Object... values);
/**
* 获取元素分数。
*
* @param key Redis 键
* @param value 元素
* @return 分数
*/
Double zScore(String key, Object value);
/**
* 获取升序排名,从 0 开始。
*
* @param key Redis 键
* @param value 元素
* @return 排名
*/
Integer zRank(String key, Object value);
/**
* 获取降序排名,从 0 开始。
*
* @param key Redis 键
* @param value 元素
* @return 排名
*/
Integer zRevRank(String key, Object value);
/**
* 获取分数区间元素。
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 元素集合
*/
Set<Object> zRangeByScore(String key, double min, double max);
/**
* 获取分数区间元素并分页。
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @param offset 偏移量
* @param count 数量
* @return 元素集合
*/
Collection<Object> zRangeByScore(String key, double min, double max, int offset, int count);
/**
* 获取分数区间元素及分数。
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 元素分数 Map
*/
Map<Object, Double> zRangeByScoreWithScores(String key, double min, double max);
/**
* 获取降序分数区间元素及分数。
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 元素分数 Map
*/
Map<Object, Double> zRevRangeByScoreWithScores(String key, double min, double max);
/**
* 获取排名区间元素。
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 元素集合
*/
Set<Object> zRange(String key, int start, int end);
/**
* 获取排名区间元素及分数。
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 元素分数 Map
*/
Map<Object, Double> zRangeWithScores(String key, int start, int end);
/**
* 获取降序排名区间元素。
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 元素集合
*/
Set<Object> zRevRange(String key, int start, int end);
/**
* 获取降序排名区间元素及分数。
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 元素分数 Map
*/
Map<Object, Double> zRevRangeWithScores(String key, int start, int end);
/**
* 增加元素分数。
*
* @param key Redis 键
* @param value 元素
* @param delta 分数增量
* @return 最新分数
*/
Double zIncrBy(String key, Object value, double delta);
/**
* 获取有序集合元素数量。
*
* @param key Redis 键
* @return 数量
*/
int zCard(String key);
/**
* 获取分数区间元素数量。
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 数量
*/
long zCount(String key, double min, double max);
/**
* 删除分数区间元素。
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 删除数量
*/
long zRemoveRangeByScore(String key, double min, double max);
/**
* 删除排名区间元素。
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 删除数量
*/
long zRemoveRangeByRank(String key, int start, int end);
/**
* 弹出分数最小的元素。
*
* @param key Redis 键
* @return 元素及分数
*/
ScoredEntry<Object> zPopFirst(String key);
/**
* 弹出分数最大的元素。
*
* @param key Redis 键
* @return 元素及分数
*/
ScoredEntry<Object> zPopLast(String key);
// -------------------------------------------------------------------------
// 分布式锁与同步器
// -------------------------------------------------------------------------
/**
* 获取可重入锁。
*
* @param lockKey 锁 key
* @return RLock
*/
RLock getLock(String lockKey);
/**
* 获取公平锁。
*
* @param lockKey 锁 key
* @return RLock
*/
RLock getFairLock(String lockKey);
/**
* 获取自旋锁。
*
* @param lockKey 锁 key
* @return RLock
*/
RLock getSpinLock(String lockKey);
/**
* 获取联锁。
*
* @param locks 多个锁
* @return RLock
*/
RLock getMultiLock(RLock... locks);
/**
* 阻塞加锁。
*
* @param lockKey 锁 key
*/
void lock(String lockKey);
/**
* 阻塞加锁并指定自动释放时间。
*
* @param lockKey 锁 key
* @param leaseTime 持有时间
* @param unit 时间单位
*/
void lock(String lockKey, long leaseTime, TimeUnit unit);
/**
* 尝试获取锁。
*
* @param lockKey 锁 key
* @param waitTime 等待时间
* @param leaseTime 持有时间
* @param unit 时间单位
* @return 是否获取成功
* @throws InterruptedException 线程中断时抛出
*/
boolean tryLock(String lockKey, long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException;
/**
* 释放锁。
*
* @param lockKey 锁 key
*/
void unlock(String lockKey);
/**
* 执行带锁任务。
*
* @param lockKey 锁 key
* @param task 任务
*/
void executeWithLock(String lockKey, Runnable task);
/**
* 执行带锁任务并返回结果。
*
* @param lockKey 锁 key
* @param supplier 任务
* @param <T> 返回类型
* @return 任务结果
*/
<T> T executeWithLock(String lockKey, Supplier<T> supplier);
/**
* 尝试执行带锁任务。
*
* @param lockKey 锁 key
* @param waitTime 等待时间
* @param leaseTime 持有时间,-1 表示使用 watchdog
* @param unit 时间单位
* @param task 任务
* @return 是否执行成功
*/
boolean tryExecuteWithLock(String lockKey, long waitTime, long leaseTime, TimeUnit unit, Runnable task);
/**
* 判断当前线程是否持有锁。
*
* @param lockKey 锁 key
* @return 当前线程持有返回 true
*/
boolean isHeldByCurrentThread(String lockKey);
/**
* 判断锁是否被任意线程持有。
*
* @param lockKey 锁 key
* @return 已加锁返回 true
*/
boolean isLocked(String lockKey);
/**
* 获取读写锁。
*
* @param lockKey 锁 key
* @return RReadWriteLock
*/
RReadWriteLock getReadWriteLock(String lockKey);
/**
* 获取读锁。
*
* @param lockKey 锁 key
*/
void readLock(String lockKey);
/**
* 获取写锁。
*
* @param lockKey 锁 key
*/
void writeLock(String lockKey);
/**
* 尝试获取读锁。
*
* @param lockKey 锁 key
* @param waitTime 等待时间
* @param leaseTime 持有时间
* @param unit 时间单位
* @return 是否成功
* @throws InterruptedException 线程中断时抛出
*/
boolean tryReadLock(String lockKey, long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException;
/**
* 尝试获取写锁。
*
* @param lockKey 锁 key
* @param waitTime 等待时间
* @param leaseTime 持有时间
* @param unit 时间单位
* @return 是否成功
* @throws InterruptedException 线程中断时抛出
*/
boolean tryWriteLock(String lockKey, long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException;
/**
* 释放读锁。
*
* @param lockKey 锁 key
*/
void unlockRead(String lockKey);
/**
* 释放写锁。
*
* @param lockKey 锁 key
*/
void unlockWrite(String lockKey);
/**
* 获取闭锁。
*
* @param latchKey 闭锁 key
* @return RCountDownLatch
*/
RCountDownLatch getCountDownLatch(String latchKey);
/**
* 设置闭锁计数。
*
* @param latchKey 闭锁 key
* @param count 计数
*/
void setCount(String latchKey, int count);
/**
* 闭锁计数减一。
*
* @param latchKey 闭锁 key
*/
void countDown(String latchKey);
/**
* 等待闭锁完成。
*
* @param latchKey 闭锁 key
* @throws InterruptedException 线程中断时抛出
*/
void await(String latchKey) throws InterruptedException;
/**
* 等待闭锁完成。
*
* @param latchKey 闭锁 key
* @param timeout 超时时间
* @param unit 时间单位
* @return 是否完成
* @throws InterruptedException 线程中断时抛出
*/
boolean await(String latchKey, long timeout, TimeUnit unit) throws InterruptedException;
/**
* 获取信号量。
*
* @param semaphoreKey 信号量 key
* @return RSemaphore
*/
RSemaphore getSemaphore(String semaphoreKey);
/**
* 初始化信号量许可数量。
*
* @param semaphoreKey 信号量 key
* @param permits 许可数量
*/
void trySetPermits(String semaphoreKey, int permits);
/**
* 获取一个信号量许可。
*
* @param semaphoreKey 信号量 key
* @throws InterruptedException 线程中断时抛出
*/
void acquire(String semaphoreKey) throws InterruptedException;
/**
* 尝试获取信号量许可。
*
* @param semaphoreKey 信号量 key
* @param permits 许可数量
* @param waitTime 等待时间
* @param unit 时间单位
* @return 是否获取成功
* @throws InterruptedException 线程中断时抛出
*/
boolean tryAcquire(String semaphoreKey, int permits, long waitTime, TimeUnit unit) throws InterruptedException;
/**
* 释放信号量许可。
*
* @param semaphoreKey 信号量 key
*/
void release(String semaphoreKey);
/**
* 获取可用许可数量。
*
* @param semaphoreKey 信号量 key
* @return 可用许可数量
*/
int availablePermits(String semaphoreKey);
/**
* 获取可过期信号量。
*
* @param semaphoreKey 信号量 key
* @return RPermitExpirableSemaphore
*/
RPermitExpirableSemaphore getPermitExpirableSemaphore(String semaphoreKey);
// -------------------------------------------------------------------------
// 限流器
// -------------------------------------------------------------------------
/**
* 初始化限流器。
*
* @param key 限流器 key
* @param rateType 限流类型
* @param rate 令牌数
* @param interval 间隔
* @param unit 间隔单位
* @return 是否初始化成功
*/
boolean rateLimiterInit(String key, RateType rateType, long rate, long interval, RateIntervalUnit unit);
/**
* 更新限流器速率。
*
* @param key 限流器 key
* @param rateType 限流类型
* @param rate 令牌数
* @param interval 间隔
* @param unit 间隔单位
*/
void rateLimiterSetRate(String key, RateType rateType, long rate, long interval, RateIntervalUnit unit);
/**
* 尝试获取一个令牌。
*
* @param key 限流器 key
* @return 是否获取成功
*/
boolean rateLimiterTryAcquire(String key);
/**
* 尝试获取指定数量令牌。
*
* @param key 限流器 key
* @param permits 令牌数量
* @return 是否获取成功
*/
boolean rateLimiterTryAcquire(String key, long permits);
/**
* 阻塞获取指定数量令牌。
*
* @param key 限流器 key
* @param permits 令牌数量
*/
void rateLimiterAcquire(String key, long permits);
/**
* 尝试等待获取令牌。
*
* @param key 限流器 key
* @param timeout 等待时间
* @param unit 时间单位
* @return 是否获取成功
*/
boolean rateLimiterTryAcquire(String key, long timeout, TimeUnit unit);
/**
* 获取限流器对象。
*
* @param key 限流器 key
* @return RRateLimiter
*/
RRateLimiter rateLimiterGet(String key);
/**
* 删除限流器。
*
* @param key 限流器 key
* @return 是否删除成功
*/
boolean rateLimiterDelete(String key);
// -------------------------------------------------------------------------
// 布隆过滤器
// -------------------------------------------------------------------------
/**
* 获取布隆过滤器。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RBloomFilter
*/
<T> RBloomFilter<T> getBloomFilter(String key);
/**
* 初始化布隆过滤器。
*
* @param key Redis 键
* @param expectedInsertions 预计插入量
* @param falseProbability 误判率
*/
void bloomInit(String key, long expectedInsertions, double falseProbability);
/**
* 判断布隆过滤器是否可能包含元素。
*
* @param key Redis 键
* @param value 元素
* @return 可能包含返回 true
*/
boolean bloomContains(String key, Object value);
/**
* 添加布隆过滤器元素。
*
* @param key Redis 键
* @param value 元素
* @return 是否新增
*/
boolean bloomAdd(String key, Object value);
/**
* 批量添加布隆过滤器元素。
*
* @param key Redis 键
* @param values 元素集合
* @return 新增数量
*/
long bloomAddAll(String key, Collection<?> values);
/**
* 删除布隆过滤器。
*
* @param key Redis 键
* @return 是否删除成功
*/
boolean bloomDelete(String key);
/**
* 判断布隆过滤器是否存在。
*
* @param key Redis 键
* @return 存在返回 true
*/
boolean bloomExists(String key);
/**
* 获取预计插入量。
*
* @param key Redis 键
* @return 预计插入量
*/
long bloomGetExpectedInsertions(String key);
/**
* 获取误判率。
*
* @param key Redis 键
* @return 误判率
*/
double bloomGetFalseProbability(String key);
// -------------------------------------------------------------------------
// BitSet / 签到 / 位图统计
// -------------------------------------------------------------------------
/**
* 设置位图指定位置。
*
* @param key Redis 键
* @param index 位索引
* @param value 位值
*/
void bitSet(String key, long index, boolean value);
/**
* 获取位图指定位置。
*
* @param key Redis 键
* @param index 位索引
* @return 位值
*/
boolean bitGet(String key, long index);
/**
* 获取位图中 true 的数量。
*
* @param key Redis 键
* @return 数量
*/
long bitCount(String key);
/**
* 清空位图。
*
* @param key Redis 键
*/
void bitClear(String key);
/**
* 用户指定日期签到。
*
* @param keyPrefix 业务 key 前缀
* @param userId 用户 ID
* @param date 日期
*/
void sign(String keyPrefix, Object userId, LocalDate date);
/**
* 判断用户指定日期是否签到。
*
* @param keyPrefix 业务 key 前缀
* @param userId 用户 ID
* @param date 日期
* @return 已签到返回 true
*/
boolean isSigned(String keyPrefix, Object userId, LocalDate date);
/**
* 获取用户指定年份签到天数。
*
* @param keyPrefix 业务 key 前缀
* @param userId 用户 ID
* @param year 年份
* @return 签到天数
*/
long getSignCount(String keyPrefix, Object userId, int year);
/**
* 获取从指定日期向前连续签到天数。
*
* @param keyPrefix 业务 key 前缀
* @param userId 用户 ID
* @param date 日期
* @return 连续签到天数
*/
int getContinuousSignCount(String keyPrefix, Object userId, LocalDate date);
// -------------------------------------------------------------------------
// HyperLogLog / UV 统计
// -------------------------------------------------------------------------
/**
* 添加 HyperLogLog 元素。
*
* @param key Redis 键
* @param value 元素
* @return 是否改变基数估算
*/
boolean hllAdd(String key, Object value);
/**
* 批量添加 HyperLogLog 元素。
*
* @param key Redis 键
* @param values 元素集合
* @return 是否改变基数估算
*/
boolean hllAddAll(String key, Collection<?> values);
/**
* 获取 HyperLogLog 基数估算。
*
* @param key Redis 键
* @return 基数估算
*/
long hllCount(String key);
/**
* 合并 HyperLogLog。
*
* @param destKey 目标 key
* @param keys 源 key
* @return 合并后基数估算
*/
long hllMerge(String destKey, String... keys);
/**
* 按日期记录 UV。
*
* @param keyPrefix 业务 key 前缀
* @param bizKey 业务标识
* @param userFlag 用户唯一标识
* @param date 日期
* @return 是否改变基数估算
*/
boolean uvRecord(String keyPrefix, String bizKey, Object userFlag, LocalDate date);
/**
* 获取指定日期 UV。
*
* @param keyPrefix 业务 key 前缀
* @param bizKey 业务标识
* @param date 日期
* @return UV 数量
*/
long uvCount(String keyPrefix, String bizKey, LocalDate date);
// -------------------------------------------------------------------------
// Geo / LBS 地理位置
// -------------------------------------------------------------------------
/**
* 添加地理位置。
*
* @param key Redis 键
* @param longitude 经度
* @param latitude 纬度
* @param member 成员
* @return 添加数量
*/
long geoAdd(String key, double longitude, double latitude, Object member);
/**
* 批量添加地理位置。
*
* @param key Redis 键
* @param entries 坐标集合
* @return 添加数量
*/
long geoAdd(String key, GeoEntry... entries);
/**
* 仅成员不存在时添加地理位置。
*
* @param key Redis 键
* @param longitude 经度
* @param latitude 纬度
* @param member 成员
* @return 是否添加成功
*/
boolean geoTryAdd(String key, double longitude, double latitude, Object member);
/**
* 计算两个成员距离。
*
* @param key Redis 键
* @param member1 成员 1
* @param member2 成员 2
* @param unit 单位
* @return 距离
*/
Double geoDistance(String key, Object member1, Object member2, GeoUnit unit);
/**
* 查询成员 GeoHash。
*
* @param key Redis 键
* @param members 成员
* @return GeoHash Map
*/
Map<Object, String> geoHash(String key, Object... members);
/**
* 查询成员坐标。
*
* @param key Redis 键
* @param members 成员
* @return 坐标 Map
*/
Map<Object, GeoPosition> geoPosition(String key, Object... members);
/**
* 根据条件搜索地理位置成员。
*
* @param key Redis 键
* @param args 搜索参数
* @return 成员列表
*/
List<Object> geoSearch(String key, GeoSearchArgs args);
/**
* 根据条件搜索地理位置成员和距离。
*
* @param key Redis 键
* @param args 搜索参数
* @return 成员距离 Map
*/
Map<Object, Double> geoSearchWithDistance(String key, GeoSearchArgs args);
/**
* 根据条件搜索地理位置成员和坐标。
*
* @param key Redis 键
* @param args 搜索参数
* @return 成员坐标 Map
*/
Map<Object, GeoPosition> geoSearchWithPosition(String key, GeoSearchArgs args);
/**
* 删除地理位置成员。
*
* @param key Redis 键
* @param members 成员
* @return 删除数量
*/
long geoRemove(String key, Object... members);
// -------------------------------------------------------------------------
// Queue / Deque / DelayedQueue / MQ
// -------------------------------------------------------------------------
/**
* 入普通队列。
*
* @param queueKey 队列 key
* @param value 元素
* @param <T> 元素类型
* @return 是否入队成功
*/
<T> boolean enqueue(String queueKey, T value);
/**
* 出普通队列。
*
* @param queueKey 队列 key
* @param <T> 元素类型
* @return 元素
*/
<T> T dequeue(String queueKey);
/**
* 阻塞入队。
*
* @param queueKey 队列 key
* @param value 元素
* @param <T> 元素类型
* @throws InterruptedException 线程中断时抛出
*/
<T> void enqueueBlocking(String queueKey, T value) throws InterruptedException;
/**
* 超时阻塞入队。
*
* @param queueKey 队列 key
* @param value 元素
* @param timeout 超时时间
* @param unit 时间单位
* @param <T> 元素类型
* @return 是否入队成功
* @throws InterruptedException 线程中断时抛出
*/
<T> boolean enqueueBlocking(String queueKey, T value, long timeout, TimeUnit unit) throws InterruptedException;
/**
* 超时阻塞出队。
*
* @param queueKey 队列 key
* @param timeout 超时时间
* @param unit 时间单位
* @param <T> 元素类型
* @return 元素
* @throws InterruptedException 线程中断时抛出
*/
<T> T dequeueBlocking(String queueKey, long timeout, TimeUnit unit) throws InterruptedException;
/**
* 获取队列长度。
*
* @param queueKey 队列 key
* @return 长度
*/
long queueSize(String queueKey);
/**
* 添加延迟队列任务。
*
* @param queueKey 队列 key
* @param value 元素
* @param delay 延迟时间
* @param unit 时间单位
* @param <T> 元素类型
*/
<T> void enqueueDelayed(String queueKey, T value, long delay, TimeUnit unit);
/**
* 获取延迟队列对象。
*
* @param queueKey 队列 key
* @param <T> 元素类型
* @return RDelayedQueue
*/
<T> RDelayedQueue<T> getDelayedQueue(String queueKey);
/**
* 清空队列。
*
* @param queueKey 队列 key
*/
void clearQueue(String queueKey);
/**
* 判断队列是否为空。
*
* @param queueKey 队列 key
* @return 为空返回 true
*/
boolean isQueueEmpty(String queueKey);
/**
* 删除队列元素。
*
* @param queueKey 队列 key
* @param value 元素
* @return 是否删除成功
*/
boolean removeFromQueue(String queueKey, Object value);
/**
* 获取环形缓冲队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RRingBuffer
*/
<T> RRingBuffer<T> getRingBuffer(String key);
/**
* 获取优先级队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RPriorityQueue
*/
<T> RPriorityQueue<T> getPriorityQueue(String key);
/**
* 获取阻塞双端队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RBlockingDeque
*/
<T> RBlockingDeque<T> getBlockingDeque(String key);
// -------------------------------------------------------------------------
// 发布订阅 / Topic
// -------------------------------------------------------------------------
/**
* 发布消息。
*
* @param channel 频道
* @param message 消息
* @return 接收客户端数量
*/
long publish(String channel, Object message);
/**
* 订阅频道。
*
* @param channel 频道
* @param messageConsumer 消费回调
* @return 监听器 ID
*/
int subscribe(String channel, Consumer<Object> messageConsumer);
/**
* 取消订阅频道。
*
* @param channel 频道
* @param listenerId 监听器 ID
*/
void unsubscribe(String channel, int listenerId);
/**
* 取消订阅频道全部监听器。
*
* @param channel 频道
*/
void unsubscribe(String channel);
/**
* 获取模式主题。
*
* @param pattern 频道通配符
* @return RPatternTopic
*/
RPatternTopic getPatternTopic(String pattern);
/**
* 获取可靠主题。
*
* @param topic 主题名
* @return RReliableTopic
*/
RReliableTopic getReliableTopic(String topic);
// -------------------------------------------------------------------------
// Stream 消息流
// -------------------------------------------------------------------------
/**
* 添加 Stream 消息。
*
* @param streamKey Stream key
* @param entries 消息字段
* @return 消息 ID
*/
StreamMessageId streamAdd(String streamKey, Map<Object, Object> entries);
/**
* 添加 Stream 消息。
*
* @param streamKey Stream key
* @param args 添加参数
* @param <K> 字段类型
* @param <V> 值类型
* @return 消息 ID
*/
<K, V> StreamMessageId streamAdd(String streamKey, StreamAddArgs<K, V> args);
/**
* 读取 Stream 消息。
*
* @param streamKey Stream key
* @param args 读取参数
* @return 消息 Map
*/
Map<StreamMessageId, Map<Object, Object>> streamRead(String streamKey, StreamReadArgs args);
/**
* 创建消费组。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param id 起始消息 ID
*/
void streamCreateGroup(String streamKey, String groupName, StreamMessageId id);
/**
* 读取消费组消息。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 消费者
* @param args 读取参数
* @return 消息 Map
*/
Map<StreamMessageId, Map<Object, Object>> streamReadGroup(String streamKey, String groupName, String consumerName, StreamReadGroupArgs args);
/**
* 确认 Stream 消息。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param ids 消息 ID
* @return 确认数量
*/
long streamAck(String streamKey, String groupName, StreamMessageId... ids);
/**
* 删除 Stream 消息。
*
* @param streamKey Stream key
* @param ids 消息 ID
* @return 删除数量
*/
long streamRemove(String streamKey, StreamMessageId... ids);
/**
* 获取 Stream 长度。
*
* @param streamKey Stream key
* @return 长度
*/
long streamSize(String streamKey);
// -------------------------------------------------------------------------
// 分布式会话
// -------------------------------------------------------------------------
/**
* 创建或覆盖会话。
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @param session 会话对象
* @param ttl 过期时间
*/
void sessionSet(String keyPrefix, String token, Object session, Duration ttl);
/**
* 获取会话。
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 会话对象
*/
<T> T sessionGet(String keyPrefix, String token, Class<T> clazz);
/**
* 获取会话。
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @param typeReference 目标类型
* @param <T> 泛型类型
* @return 会话对象
*/
<T> T sessionGet(String keyPrefix, String token, TypeReference<T> typeReference);
/**
* 刷新会话过期时间。
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @param ttl 过期时间
* @return 是否刷新成功
*/
boolean sessionRefresh(String keyPrefix, String token, Duration ttl);
/**
* 删除会话。
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @return 是否删除成功
*/
boolean sessionDelete(String keyPrefix, String token);
// -------------------------------------------------------------------------
// Lua / 脚本 / 批处理 / 事务
// -------------------------------------------------------------------------
/**
* 执行 Lua 脚本。
*
* @param script Lua 脚本
* @param mode 执行模式
* @param returnType 返回类型
* @param keys KEYS
* @param values ARGV
* @param <T> 返回泛型
* @return 执行结果
*/
<T> T eval(String script, RScript.Mode mode, RScript.ReturnType returnType, List<Object> keys, Object... values);
/**
* 执行 Lua 脚本并返回指定类型。
*
* @param script Lua 脚本
* @param returnType 目标类型
* @param keys KEYS
* @param args ARGV
* @param <T> 返回泛型
* @return 执行结果
*/
<T> T eval(String script, Class<T> returnType, List<Object> keys, Object... args);
/**
* 执行 Lua 脚本但不关心结果。
*
* @param script Lua 脚本
* @param keys KEYS
* @param args ARGV
*/
void evalNoResult(String script, List<Object> keys, Object... args);
/**
* 根据 SHA1 执行 Lua 脚本。
*
* @param sha1 脚本 SHA1
* @param returnType 目标类型
* @param keys KEYS
* @param values ARGV
* @param <T> 返回泛型
* @return 执行结果
*/
<T> T evalBySha(String sha1, Class<T> returnType, List<Object> keys, Object... values);
/**
* 加载 Lua 脚本。
*
* @param script Lua 脚本
* @return SHA1
*/
String loadScript(String script);
/**
* 创建批处理对象。
*
* @return RBatch
*/
RBatch createBatch();
/**
* 创建事务对象。
*
* @return RTransaction
*/
RTransaction createTransaction();
// -------------------------------------------------------------------------
// LocalCachedMap / 本地缓存 Map
// -------------------------------------------------------------------------
/**
* 获取本地缓存 Map。
* 注意:同一个 RedissonClient 内,相同 name 建议复用相同 LocalCachedMapOptions。
*
* @param key Redis 键
* @param options 本地缓存配置
* @param <K> 字段类型
* @param <V> 值类型
* @return RLocalCachedMap
*/
<K, V> RLocalCachedMap<K, V> getLocalCachedMap(String key, LocalCachedMapOptions<K, V> options);
/**
* 设置本地缓存 Map 字段值。
*
* @param key Redis 键
* @param field 字段
* @param value 字段值
* @param options 本地缓存配置
*/
void lcPut(String key, Object field, Object value, LocalCachedMapOptions<Object, Object> options);
/**
* 字段不存在时设置本地缓存 Map 字段值。
*
* @param key Redis 键
* @param field 字段
* @param value 字段值
* @param options 本地缓存配置
* @return 是否设置成功
*/
boolean lcPutIfAbsent(String key, Object field, Object value, LocalCachedMapOptions<Object, Object> options);
/**
* 获取本地缓存 Map 字段值。
*
* @param key Redis 键
* @param field 字段
* @param clazz 目标类型
* @param options 本地缓存配置
* @param <T> 泛型类型
* @return 字段值
*/
<T> T lcGet(String key, Object field, Class<T> clazz, LocalCachedMapOptions<Object, Object> options);
/**
* 获取本地缓存 Map 字段值。
*
* @param key Redis 键
* @param field 字段
* @param typeReference 目标类型
* @param options 本地缓存配置
* @param <T> 泛型类型
* @return 字段值
*/
<T> T lcGet(String key, Object field, TypeReference<T> typeReference, LocalCachedMapOptions<Object, Object> options);
/**
* 删除本地缓存 Map 字段。
*
* @param key Redis 键
* @param field 字段
* @param options 本地缓存配置
* @return 删除前的值
*/
Object lcRemove(String key, Object field, LocalCachedMapOptions<Object, Object> options);
/**
* 判断本地缓存 Map 字段是否存在。
*
* @param key Redis 键
* @param field 字段
* @param options 本地缓存配置
* @return 存在返回 true
*/
boolean lcContainsKey(String key, Object field, LocalCachedMapOptions<Object, Object> options);
/**
* 获取本地缓存 Map 大小。
*
* @param key Redis 键
* @param options 本地缓存配置
* @return 大小
*/
int lcSize(String key, LocalCachedMapOptions<Object, Object> options);
/**
* 清空本地缓存 Map。
*
* @param key Redis 键
* @param options 本地缓存配置
*/
void lcClear(String key, LocalCachedMapOptions<Object, Object> options);
/**
* 仅清空当前 JVM 内的本地缓存,不清空 Redis 远端数据。
*
* @param key Redis 键
* @param options 本地缓存配置
*/
void lcClearLocalCache(String key, LocalCachedMapOptions<Object, Object> options);
// -------------------------------------------------------------------------
// JsonBucket / RedisJSON
// -------------------------------------------------------------------------
/**
* 获取 JSON Bucket。
*
* @param key Redis 键
* @param codec JSON 编解码器
* @param <T> 值类型
* @return RJsonBucket
*/
<T> RJsonBucket<T> getJsonBucket(String key, JsonCodec codec);
/**
* 设置 JSON 文档。
*
* @param key Redis 键
* @param value JSON 对象
* @param codec JSON 编解码器
* @param <T> 值类型
*/
<T> void jsonSet(String key, T value, JsonCodec codec);
/**
* 设置 JSON 文档并指定过期时间。
*
* @param key Redis 键
* @param value JSON 对象
* @param ttl 过期时间
* @param codec JSON 编解码器
* @param <T> 值类型
*/
<T> void jsonSet(String key, T value, Duration ttl, JsonCodec codec);
/**
* 获取完整 JSON 文档。
*
* @param key Redis 键
* @param codec JSON 编解码器
* @param <T> 值类型
* @return JSON 对象
*/
<T> T jsonGet(String key, JsonCodec codec);
/**
* 根据 JSONPath 获取 JSON 局部内容。
*
* @param key Redis 键
* @param path JSONPath
* @param codec JSON 编解码器
* @param <T> 返回类型
* @return JSONPath 对应的值
*/
<T> T jsonGet(String key, String path, JsonCodec codec);
/**
* 根据 JSONPath 设置 JSON 局部内容。
*
* @param key Redis 键
* @param path JSONPath
* @param value 值
* @param codec JSON 编解码器
* @param <T> JSON 文档类型
*/
<T> void jsonSet(String key, String path, Object value, JsonCodec codec);
/**
* JSONPath 不存在时设置局部内容。
*
* @param key Redis 键
* @param path JSONPath
* @param value 值
* @param codec JSON 编解码器
* @param <T> JSON 文档类型
* @return 是否设置成功
*/
<T> boolean jsonSetIfAbsent(String key, String path, Object value, JsonCodec codec);
/**
* JSONPath 存在时设置局部内容。
*
* @param key Redis 键
* @param path JSONPath
* @param value 值
* @param codec JSON 编解码器
* @param <T> JSON 文档类型
* @return 是否设置成功
*/
<T> boolean jsonSetIfExists(String key, String path, Object value, JsonCodec codec);
/**
* 删除 JSONPath 对应内容。
*
* @param key Redis 键
* @param path JSONPath
* @param codec JSON 编解码器
* @param <T> JSON 文档类型
* @return 删除数量
*/
<T> long jsonDelete(String key, String path, JsonCodec codec);
/**
* 向 JSON 数组追加元素。
*
* @param key Redis 键
* @param path JSONPath
* @param codec JSON 编解码器
* @param values 追加值
* @param <T> JSON 文档类型
* @return 追加后的数组长度
*/
<T> long jsonArrayAppend(String key, String path, JsonCodec codec, Object... values);
/**
* 获取 JSON 对象字段名。
*
* @param key Redis 键
* @param codec JSON 编解码器
* @param <T> JSON 文档类型
* @return 字段名集合
*/
<T> List<String> jsonKeys(String key, JsonCodec codec);
/**
* 清空 JSON 文档。
*
* @param key Redis 键
* @param codec JSON 编解码器
* @param <T> JSON 文档类型
*/
<T> void jsonClear(String key, JsonCodec codec);
// -------------------------------------------------------------------------
// BinaryStream / 二进制流
// -------------------------------------------------------------------------
/**
* 获取二进制流对象。
*
* @param key Redis 键
* @return RBinaryStream
*/
RBinaryStream getBinaryStream(String key);
/**
* 获取二进制输入流。
*
* @param key Redis 键
* @return InputStream
*/
InputStream binaryInputStream(String key);
/**
* 获取二进制输出流。
*
* @param key Redis 键
* @return OutputStream
*/
OutputStream binaryOutputStream(String key);
/**
* 写入二进制数据。
*
* @param key Redis 键
* @param data 字节数组
*/
void binaryWrite(String key, byte[] data);
/**
* 读取全部二进制数据。
*
* @param key Redis 键
* @return 字节数组
*/
byte[] binaryReadAll(String key);
/**
* 获取二进制数据大小。
*
* @param key Redis 键
* @return 字节大小
*/
long binarySize(String key);
/**
* 删除二进制数据。
*
* @param key Redis 键
* @return 是否删除成功
*/
boolean binaryDelete(String key);
// -------------------------------------------------------------------------
// Multimap / 一键多值
// -------------------------------------------------------------------------
/**
* 获取 SetMultimap。
*
* @param key Redis 键
* @param <K> 字段类型
* @param <V> 值类型
* @return RSetMultimap
*/
<K, V> RSetMultimap<K, V> getSetMultimap(String key);
/**
* 获取 ListMultimap。
*
* @param key Redis 键
* @param <K> 字段类型
* @param <V> 值类型
* @return RListMultimap
*/
<K, V> RListMultimap<K, V> getListMultimap(String key);
/**
* 向 SetMultimap 添加值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 是否新增
*/
boolean smmPut(String key, Object mapKey, Object mapValue);
/**
* 获取 SetMultimap 指定字段的值集合。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 值集合
*/
Set<Object> smmGet(String key, Object mapKey);
/**
* 删除 SetMultimap 指定字段的指定值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 是否删除成功
*/
boolean smmRemove(String key, Object mapKey, Object mapValue);
/**
* 删除 SetMultimap 指定字段的全部值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 删除的值集合
*/
Set<Object> smmRemoveAll(String key, Object mapKey);
/**
* 判断 SetMultimap 是否包含指定字段。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 存在返回 true
*/
boolean smmContainsKey(String key, Object mapKey);
/**
* 判断 SetMultimap 是否包含指定字段和值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 存在返回 true
*/
boolean smmContainsEntry(String key, Object mapKey, Object mapValue);
/**
* 获取 SetMultimap 总值数量。
*
* @param key Redis 键
* @return 总值数量
*/
int smmSize(String key);
/**
* 清空 SetMultimap。
*
* @param key Redis 键
*/
void smmClear(String key);
/**
* 向 ListMultimap 添加值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 是否新增
*/
boolean lmmPut(String key, Object mapKey, Object mapValue);
/**
* 获取 ListMultimap 指定字段的值列表。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 值列表
*/
List<Object> lmmGet(String key, Object mapKey);
/**
* 删除 ListMultimap 指定字段的指定值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 是否删除成功
*/
boolean lmmRemove(String key, Object mapKey, Object mapValue);
/**
* 删除 ListMultimap 指定字段的全部值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 删除的值列表
*/
List<Object> lmmRemoveAll(String key, Object mapKey);
/**
* 判断 ListMultimap 是否包含指定字段。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 存在返回 true
*/
boolean lmmContainsKey(String key, Object mapKey);
/**
* 判断 ListMultimap 是否包含指定字段和值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 存在返回 true
*/
boolean lmmContainsEntry(String key, Object mapKey, Object mapValue);
/**
* 获取 ListMultimap 总值数量。
*
* @param key Redis 键
* @return 总值数量
*/
int lmmSize(String key);
/**
* 清空 ListMultimap。
*
* @param key Redis 键
*/
void lmmClear(String key);
// -------------------------------------------------------------------------
// SortedSet / LexSortedSet
// -------------------------------------------------------------------------
/**
* 获取自然排序集合。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RSortedSet
*/
<T> RSortedSet<T> getSortedSet(String key);
/**
* 获取字典序排序集合。
*
* @param key Redis 键
* @return RLexSortedSet
*/
RLexSortedSet getLexSortedSet(String key);
/**
* 添加自然排序集合元素。
*
* @param key Redis 键
* @param value 元素
* @return 是否新增
*/
boolean sortedSetAdd(String key, Object value);
/**
* 批量添加自然排序集合元素。
*
* @param key Redis 键
* @param values 元素集合
* @return 是否有新增
*/
boolean sortedSetAddAll(String key, Collection<?> values);
/**
* 获取自然排序集合全部元素。
*
* @param key Redis 键
* @return 元素集合
*/
Collection<Object> sortedSetReadAll(String key);
/**
* 删除自然排序集合元素。
*
* @param key Redis 键
* @param values 元素
* @return 是否删除成功
*/
boolean sortedSetRemove(String key, Object... values);
/**
* 获取自然排序集合大小。
*
* @param key Redis 键
* @return 大小
*/
int sortedSetSize(String key);
/**
* 清空自然排序集合。
*
* @param key Redis 键
*/
void sortedSetClear(String key);
/**
* 添加字典序排序集合元素。
*
* @param key Redis 键
* @param value 元素
* @return 是否新增
*/
boolean lexAdd(String key, String value);
/**
* 批量添加字典序排序集合元素。
*
* @param key Redis 键
* @param values 元素集合
* @return 是否有新增
*/
boolean lexAddAll(String key, Collection<String> values);
/**
* 获取字典序排序集合全部元素。
*
* @param key Redis 键
* @return 元素集合
*/
Collection<String> lexReadAll(String key);
/**
* 获取大于等于指定元素的字典序集合。
*
* @param key Redis 键
* @param from 开始元素
* @return 元素集合
*/
Collection<String> lexRangeTail(String key, String from);
/**
* 获取小于等于指定元素的字典序集合。
*
* @param key Redis 键
* @param to 结束元素
* @return 元素集合
*/
Collection<String> lexRangeHead(String key, String to);
/**
* 删除字典序排序集合元素。
*
* @param key Redis 键
* @param values 元素
* @return 是否删除成功
*/
boolean lexRemove(String key, String... values);
/**
* 获取字典序排序集合大小。
*
* @param key Redis 键
* @return 大小
*/
int lexSize(String key);
/**
* 清空字典序排序集合。
*
* @param key Redis 键
*/
void lexClear(String key);
// -------------------------------------------------------------------------
// LongAdder / DoubleAdder
// -------------------------------------------------------------------------
/**
* 获取分布式 LongAdder。
*
* @param key Redis 键
* @return RLongAdder
*/
RLongAdder getLongAdder(String key);
/**
* 获取分布式 DoubleAdder。
*
* @param key Redis 键
* @return RDoubleAdder
*/
RDoubleAdder getDoubleAdder(String key);
/**
* LongAdder 增加。
*
* @param key Redis 键
* @param delta 增量
*/
void longAdderAdd(String key, long delta);
/**
* LongAdder 求和。
*
* @param key Redis 键
* @return 当前总和
*/
long longAdderSum(String key);
/**
* LongAdder 重置。
*
* @param key Redis 键
*/
void longAdderReset(String key);
/**
* LongAdder 求和后重置。
*
* @param key Redis 键
* @return 重置前总和
*/
long longAdderSumThenReset(String key);
/**
* DoubleAdder 增加。
*
* @param key Redis 键
* @param delta 增量
*/
void doubleAdderAdd(String key, double delta);
/**
* DoubleAdder 求和。
*
* @param key Redis 键
* @return 当前总和
*/
double doubleAdderSum(String key);
/**
* DoubleAdder 重置。
*
* @param key Redis 键
*/
void doubleAdderReset(String key);
/**
* DoubleAdder 求和后重置。
*
* @param key Redis 键
* @return 重置前总和
*/
double doubleAdderSumThenReset(String key);
// -------------------------------------------------------------------------
// Bounded / Priority 队列增强
// -------------------------------------------------------------------------
/**
* 获取有界阻塞队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RBoundedBlockingQueue
*/
<T> RBoundedBlockingQueue<T> getBoundedBlockingQueue(String key);
/**
* 初始化有界阻塞队列容量。
*
* @param key Redis 键
* @param capacity 容量
* @return 是否初始化成功
*/
boolean boundedQueueTrySetCapacity(String key, int capacity);
/**
* 有界阻塞队列入队。
*
* @param key Redis 键
* @param value 元素
* @param <T> 元素类型
* @return 是否入队成功
*/
<T> boolean boundedQueueOffer(String key, T value);
/**
* 有界阻塞队列超时入队。
*
* @param key Redis 键
* @param value 元素
* @param timeout 等待时间
* @param unit 时间单位
* @param <T> 元素类型
* @return 是否入队成功
* @throws InterruptedException 线程中断时抛出
*/
<T> boolean boundedQueueOffer(String key, T value, long timeout, TimeUnit unit) throws InterruptedException;
/**
* 有界阻塞队列出队。
*
* @param key Redis 键
* @param <T> 元素类型
* @return 元素
*/
<T> T boundedQueuePoll(String key);
/**
* 有界阻塞队列超时出队。
*
* @param key Redis 键
* @param timeout 等待时间
* @param unit 时间单位
* @param <T> 元素类型
* @return 元素
* @throws InterruptedException 线程中断时抛出
*/
<T> T boundedQueuePoll(String key, long timeout, TimeUnit unit) throws InterruptedException;
/**
* 获取有界阻塞队列大小。
*
* @param key Redis 键
* @return 队列大小
*/
int boundedQueueSize(String key);
/**
* 获取有界阻塞队列剩余容量。
*
* @param key Redis 键
* @return 剩余容量
*/
int boundedQueueRemainingCapacity(String key);
/**
* 获取优先级双端队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RPriorityDeque
*/
<T> RPriorityDeque<T> getPriorityDeque(String key);
/**
* 获取优先级阻塞队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RPriorityBlockingQueue
*/
<T> RPriorityBlockingQueue<T> getPriorityBlockingQueue(String key);
/**
* 获取优先级阻塞双端队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RPriorityBlockingDeque
*/
<T> RPriorityBlockingDeque<T> getPriorityBlockingDeque(String key);
/**
* 优先级队列入队。
*
* @param key Redis 键
* @param value 元素,建议实现 Comparable
* @param <T> 元素类型
* @return 是否入队成功
*/
<T> boolean priorityQueueOffer(String key, T value);
/**
* 优先级队列出队。
*
* @param key Redis 键
* @param <T> 元素类型
* @return 元素
*/
<T> T priorityQueuePoll(String key);
/**
* 优先级双端队列从头部入队。
*
* @param key Redis 键
* @param value 元素,建议实现 Comparable
* @param <T> 元素类型
* @return 是否入队成功
*/
<T> boolean priorityDequeOfferFirst(String key, T value);
/**
* 优先级双端队列从尾部入队。
*
* @param key Redis 键
* @param value 元素,建议实现 Comparable
* @param <T> 元素类型
* @return 是否入队成功
*/
<T> boolean priorityDequeOfferLast(String key, T value);
/**
* 优先级双端队列从头部出队。
*
* @param key Redis 键
* @param <T> 元素类型
* @return 元素
*/
<T> T priorityDequePollFirst(String key);
/**
* 优先级双端队列从尾部出队。
*
* @param key Redis 键
* @param <T> 元素类型
* @return 元素
*/
<T> T priorityDequePollLast(String key);
// -------------------------------------------------------------------------
// ReliableQueue / 可靠队列
// -------------------------------------------------------------------------
/**
* 设置可靠队列配置。
*
* @param key 队列 key
* @param config 队列配置
*/
void reliableQueueSetConfig(String key, QueueConfig config);
/**
* 队列配置不存在时设置可靠队列配置。
*
* @param key 队列 key
* @param config 队列配置
* @return 是否设置成功
*/
boolean reliableQueueSetConfigIfAbsent(String key, QueueConfig config);
/**
* 可靠队列添加消息。
*
* @param key 队列 key
* @param args 添加参数
* @param <T> 消息类型
* @return 添加后的消息
*/
<T> Message<T> reliableQueueAdd(String key, QueueAddArgs<T> args);
/**
* 可靠队列批量添加消息。
*
* @param key 队列 key
* @param args 添加参数
* @param <T> 消息类型
* @return 添加后的消息集合
*/
<T> List<Message<T>> reliableQueueAddMany(String key, QueueAddArgs<T> args);
/**
* 可靠队列拉取一条消息。
*
* @param key 队列 key
* @param <T> 消息类型
* @return 消息
*/
<T> Message<T> reliableQueuePoll(String key);
/**
* 可靠队列按参数拉取一条消息。
*
* @param key 队列 key
* @param args 拉取参数
* @param <T> 消息类型
* @return 消息
*/
<T> Message<T> reliableQueuePoll(String key, QueuePollArgs args);
/**
* 可靠队列批量拉取消息。
*
* @param key 队列 key
* @param args 拉取参数
* @param <T> 消息类型
* @return 消息集合
*/
<T> List<Message<T>> reliableQueuePollMany(String key, QueuePollArgs args);
/**
* 确认可靠队列消息处理成功。
*
* @param key 队列 key
* @param args ACK 参数
*/
void reliableQueueAck(String key, QueueAckArgs args);
/**
* 标记可靠队列消息处理失败。
*
* @param key 队列 key
* @param args NACK 参数
*/
void reliableQueueNack(String key, QueueNegativeAckArgs args);
/**
* 根据消息 ID 获取可靠队列消息。
*
* @param key 队列 key
* @param id 消息 ID
* @param <T> 消息类型
* @return 消息
*/
<T> Message<T> reliableQueueGet(String key, String id);
/**
* 根据消息 ID 批量获取可靠队列消息。
*
* @param key 队列 key
* @param ids 消息 ID
* @param <T> 消息类型
* @return 消息集合
*/
<T> List<Message<T>> reliableQueueGetAll(String key, String... ids);
/**
* 获取可靠队列所有可拉取消息。
*
* @param key 队列 key
* @param <T> 消息类型
* @return 消息集合
*/
<T> List<Message<T>> reliableQueueListAll(String key);
/**
* 判断可靠队列是否包含指定消息 ID。
*
* @param key 队列 key
* @param id 消息 ID
* @return 包含返回 true
*/
boolean reliableQueueContains(String key, String id);
/**
* 判断可靠队列包含的消息 ID 数量。
*
* @param key 队列 key
* @param ids 消息 ID
* @return 匹配数量
*/
int reliableQueueContainsMany(String key, String... ids);
/**
* 删除可靠队列消息。
*
* @param key 队列 key
* @param args 删除参数
* @return 是否删除成功
*/
boolean reliableQueueRemove(String key, QueueRemoveArgs args);
/**
* 批量删除可靠队列消息。
*
* @param key 队列 key
* @param args 删除参数
* @return 删除数量
*/
int reliableQueueRemoveMany(String key, QueueRemoveArgs args);
/**
* 移动可靠队列消息。
*
* @param key 队列 key
* @param args 移动参数
* @return 移动数量
*/
int reliableQueueMove(String key, QueueMoveArgs args);
/**
* 获取可靠队列消息数量。
*
* @param key 队列 key
* @return 消息数量
*/
int reliableQueueSize(String key);
/**
* 获取可靠队列延迟消息数量。
*
* @param key 队列 key
* @return 延迟消息数量
*/
int reliableQueueDelayedSize(String key);
/**
* 获取可靠队列未确认消息数量。
*
* @param key 队列 key
* @return 未确认消息数量
*/
int reliableQueueUnacknowledgedSize(String key);
/**
* 清空可靠队列全部状态消息。
*
* @param key 队列 key
* @return 是否清空成功
*/
boolean reliableQueueClear(String key);
/**
* 获取将当前队列作为死信队列的源队列名称。
*
* @param key 队列 key
* @return 源队列名称集合
*/
Set<String> reliableQueueDeadLetterSources(String key);
/**
* 添加可靠队列事件监听器。
*
* @param key 队列 key
* @param listener 监听器
* @return 监听器 ID
*/
String reliableQueueAddListener(String key, QueueEventListener listener);
/**
* 移除可靠队列事件监听器。
*
* @param key 队列 key
* @param listenerId 监听器 ID
*/
void reliableQueueRemoveListener(String key, String listenerId);
/**
* 启用可靠队列指定操作。
*
* @param key 队列 key
* @param operation 队列操作
*/
void reliableQueueEnableOperation(String key, QueueOperation operation);
/**
* 禁用可靠队列指定操作。
*
* @param key 队列 key
* @param operation 队列操作
*/
void reliableQueueDisableOperation(String key, QueueOperation operation);
// -------------------------------------------------------------------------
// Stream / 高级消费治理
// -------------------------------------------------------------------------
/**
* 获取 Stream 详细信息。
*
* @param streamKey Stream key
* @return Stream 信息
*/
StreamInfo<Object, Object> streamInfo(String streamKey);
/**
* 获取 Stream 消费组列表。
*
* @param streamKey Stream key
* @return 消费组列表
*/
List<StreamGroup> streamListGroups(String streamKey);
/**
* 获取 Stream 指定消费组的消费者列表。
*
* @param streamKey Stream key
* @param groupName 消费组
* @return 消费者列表
*/
List<StreamConsumer> streamListConsumers(String streamKey, String groupName);
/**
* 创建 Stream 消费者。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 消费者
*/
void streamCreateConsumer(String streamKey, String groupName, String consumerName);
/**
* 删除 Stream 消费者。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 消费者
* @return 该消费者名下的待处理消息数量
*/
long streamRemoveConsumer(String streamKey, String groupName, String consumerName);
/**
* 删除 Stream 消费组。
*
* @param streamKey Stream key
* @param groupName 消费组
*/
void streamRemoveGroup(String streamKey, String groupName);
/**
* 更新 Stream 消费组读取起始 ID。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param id 消息 ID
*/
void streamUpdateGroupMessageId(String streamKey, String groupName, StreamMessageId id);
/**
* 获取 Stream 消费组待处理消息概要。
*
* @param streamKey Stream key
* @param groupName 消费组
* @return 待处理概要
*/
PendingResult streamPendingInfo(String streamKey, String groupName);
/**
* 获取 Stream 消费组待处理消息列表。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param startId 开始 ID
* @param endId 结束 ID
* @param count 数量
* @return 待处理消息列表
*/
List<PendingEntry> streamListPending(String streamKey, String groupName, StreamMessageId startId, StreamMessageId endId, int count);
/**
* 获取 Stream 指定消费者的待处理消息列表。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 消费者
* @param startId 开始 ID
* @param endId 结束 ID
* @param count 数量
* @return 待处理消息列表
*/
List<PendingEntry> streamListPending(String streamKey, String groupName, String consumerName, StreamMessageId startId, StreamMessageId endId, int count);
/**
* 获取 Stream 指定消费者满足最小空闲时间的待处理消息列表。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 消费者
* @param startId 开始 ID
* @param endId 结束 ID
* @param idleTime 最小空闲时间
* @param unit 时间单位
* @param count 数量
* @return 待处理消息列表
*/
List<PendingEntry> streamListPending(String streamKey, String groupName, String consumerName,
StreamMessageId startId, StreamMessageId endId,
long idleTime, TimeUnit unit, int count);
/**
* 按 ID 范围读取 Stream 消息。
*
* @param streamKey Stream key
* @param startId 开始 ID
* @param endId 结束 ID
* @return 消息 Map
*/
Map<StreamMessageId, Map<Object, Object>> streamRange(String streamKey, StreamMessageId startId, StreamMessageId endId);
/**
* 按 ID 范围读取 Stream 消息并限制数量。
*
* @param streamKey Stream key
* @param startId 开始 ID
* @param endId 结束 ID
* @param count 数量
* @return 消息 Map
*/
Map<StreamMessageId, Map<Object, Object>> streamRange(String streamKey, StreamMessageId startId, StreamMessageId endId, int count);
/**
* 按 ID 范围倒序读取 Stream 消息。
*
* @param streamKey Stream key
* @param startId 开始 ID
* @param endId 结束 ID
* @return 消息 Map
*/
Map<StreamMessageId, Map<Object, Object>> streamRangeReversed(String streamKey, StreamMessageId startId, StreamMessageId endId);
/**
* 按 ID 范围倒序读取 Stream 消息并限制数量。
*
* @param streamKey Stream key
* @param startId 开始 ID
* @param endId 结束 ID
* @param count 数量
* @return 消息 Map
*/
Map<StreamMessageId, Map<Object, Object>> streamRangeReversed(String streamKey, StreamMessageId startId, StreamMessageId endId, int count);
/**
* 转移待处理 Stream 消息所有权。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 新消费者
* @param idleTime 最小空闲时间
* @param unit 时间单位
* @param ids 消息 ID
* @return 转移后的消息 Map
*/
Map<StreamMessageId, Map<Object, Object>> streamClaim(String streamKey, String groupName, String consumerName,
long idleTime, TimeUnit unit, StreamMessageId... ids);
/**
* 自动转移待处理 Stream 消息所有权。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 新消费者
* @param idleTime 最小空闲时间
* @param unit 时间单位
* @param startId 起始 ID
* @param count 数量
* @return 自动转移结果
*/
AutoClaimResult<Object, Object> streamAutoClaim(String streamKey, String groupName, String consumerName,
long idleTime, TimeUnit unit, StreamMessageId startId, int count);
/**
* 裁剪 Stream。
*
* @param streamKey Stream key
* @param args 裁剪参数
* @return 裁剪数量
*/
long streamTrim(String streamKey, StreamTrimArgs args);
/**
* 添加 Stream 对象监听器。
*
* @param streamKey Stream key
* @param listener 对象监听器
* @return 监听器 ID
*/
int streamAddListener(String streamKey, ObjectListener listener);
/**
* 移除 Stream 对象监听器。
*
* @param streamKey Stream key
* @param listenerId 监听器 ID
*/
void streamRemoveListener(String streamKey, int listenerId);
// -------------------------------------------------------------------------
// Executor / Scheduler 分布式任务
// -------------------------------------------------------------------------
/**
* 获取分布式执行器。
*
* @param name 执行器名称
* @return RExecutorService
*/
RExecutorService getExecutorService(String name);
/**
* 获取分布式定时执行器。
*
* @param name 执行器名称
* @return RScheduledExecutorService
*/
RScheduledExecutorService getScheduledExecutorService(String name);
/**
* 执行 Runnable 分布式任务。
*
* @param name 执行器名称
* @param task 任务
*/
void executorExecute(String name, Runnable task);
/**
* 提交 Runnable 分布式任务。
*
* @param name 执行器名称
* @param task 任务
* @return Future
*/
Future<?> executorSubmit(String name, Runnable task);
/**
* 提交 Callable 分布式任务。
*
* @param name 执行器名称
* @param task 任务
* @param <T> 返回类型
* @return Future
*/
<T> Future<T> executorSubmit(String name, Callable<T> task);
/**
* 关闭分布式执行器。
*
* @param name 执行器名称
*/
void executorShutdown(String name);
/**
* 立即关闭分布式执行器。
*
* @param name 执行器名称
* @return 未执行任务集合
*/
List<Runnable> executorShutdownNow(String name);
/**
* 调度 Runnable 分布式任务。
*
* @param name 执行器名称
* @param task 任务
* @param delay 延迟时间
* @param unit 时间单位
* @return ScheduledFuture
*/
ScheduledFuture<?> schedule(String name, Runnable task, long delay, TimeUnit unit);
/**
* 调度 Callable 分布式任务。
*
* @param name 执行器名称
* @param task 任务
* @param delay 延迟时间
* @param unit 时间单位
* @param <T> 返回类型
* @return ScheduledFuture
*/
<T> ScheduledFuture<T> schedule(String name, Callable<T> task, long delay, TimeUnit unit);
/**
* 固定频率调度 Runnable 分布式任务。
*
* @param name 执行器名称
* @param task 任务
* @param initialDelay 初始延迟
* @param period 执行周期
* @param unit 时间单位
* @return ScheduledFuture
*/
ScheduledFuture<?> scheduleAtFixedRate(String name, Runnable task, long initialDelay, long period, TimeUnit unit);
/**
* 固定延迟调度 Runnable 分布式任务。
*
* @param name 执行器名称
* @param task 任务
* @param initialDelay 初始延迟
* @param delay 执行间隔
* @param unit 时间单位
* @return ScheduledFuture
*/
ScheduledFuture<?> scheduleWithFixedDelay(String name, Runnable task, long initialDelay, long delay, TimeUnit unit);
// -------------------------------------------------------------------------
// RemoteService / LiveObject
// -------------------------------------------------------------------------
/**
* 获取远程服务。
*
* @return RRemoteService
*/
RRemoteService getRemoteService();
/**
* 获取指定名称的远程服务。
*
* @param name 服务名称
* @return RRemoteService
*/
RRemoteService getRemoteService(String name);
/**
* 注册远程服务实现。
*
* @param remoteInterface 远程服务接口
* @param implementation 远程服务实现
* @param <T> 服务类型
*/
<T> void remoteRegister(Class<T> remoteInterface, T implementation);
/**
* 注册远程服务实现并指定工作线程数量。
*
* @param remoteInterface 远程服务接口
* @param implementation 远程服务实现
* @param workers 工作线程数量
* @param <T> 服务类型
*/
<T> void remoteRegister(Class<T> remoteInterface, T implementation, int workers);
/**
* 获取远程服务代理。
*
* @param remoteInterface 远程服务接口
* @param <T> 服务类型
* @return 服务代理
*/
<T> T remoteGet(Class<T> remoteInterface);
/**
* 获取 LiveObject 服务。
*
* @return RLiveObjectService
*/
RLiveObjectService getLiveObjectService();
/**
* 附加 LiveObject。
*
* @param detachedObject 游离对象
* @param <T> 对象类型
* @return LiveObject
*/
<T> T liveObjectAttach(T detachedObject);
/**
* 合并 LiveObject。
*
* @param detachedObject 游离对象
* @param <T> 对象类型
* @return LiveObject
*/
<T> T liveObjectMerge(T detachedObject);
/**
* 获取 LiveObject。
*
* @param entityClass 实体类型
* @param id 实体 ID
* @param <T> 对象类型
* @return LiveObject
*/
<T> T liveObjectGet(Class<T> entityClass, Object id);
/**
* 删除 LiveObject。
*
* @param attachedObject 已附加对象
*/
void liveObjectDelete(Object attachedObject);
/**
* 根据类型和 ID 删除 LiveObject。
*
* @param entityClass 实体类型
* @param id 实体 ID
*/
void liveObjectDelete(Class<?> entityClass, Object id);
// -------------------------------------------------------------------------
// Object Listener / 对象监听
// -------------------------------------------------------------------------
/**
* 添加全局对象监听器。
*
* @param listener 对象监听器
* @return 监听器 ID
*/
int addGlobalObjectListener(ObjectListener listener);
/**
* 移除全局对象监听器。
*
* @param listenerId 监听器 ID
*/
void removeGlobalObjectListener(int listenerId);
/**
* 添加 Bucket 对象监听器。
*
* @param key Redis 键
* @param listener 对象监听器
* @return 监听器 ID
*/
int addBucketListener(String key, ObjectListener listener);
/**
* 移除 Bucket 对象监听器。
*
* @param key Redis 键
* @param listenerId 监听器 ID
*/
void removeBucketListener(String key, int listenerId);
/**
* 添加 Map 对象监听器。
*
* @param key Redis 键
* @param listener 对象监听器
* @return 监听器 ID
*/
int addMapListener(String key, ObjectListener listener);
/**
* 添加 Map Entry 监听器。
*
* @param key Redis 键
* @param listener Entry 监听器
* @return 监听器 ID
*/
int addMapEntryListener(String key, ObjectListener listener);
/**
* 移除 Map 监听器。
*
* @param key Redis 键
* @param listenerId 监听器 ID
*/
void removeMapListener(String key, int listenerId);
/**
* 添加 Queue 对象监听器。
*
* @param key Redis 键
* @param listener 对象监听器
* @return 监听器 ID
*/
int addQueueListener(String key, ObjectListener listener);
/**
* 移除 Queue 对象监听器。
*
* @param key Redis 键
* @param listenerId 监听器 ID
*/
void removeQueueListener(String key, int listenerId);
/**
* 添加 Set 对象监听器。
*
* @param key Redis 键
* @param listener 对象监听器
* @return 监听器 ID
*/
int addSetListener(String key, ObjectListener listener);
/**
* 移除 Set 对象监听器。
*
* @param key Redis 键
* @param listenerId 监听器 ID
*/
void removeSetListener(String key, int listenerId);
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
创建Service实现
package local.ateng.java.redis.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import local.ateng.java.redis.service.RedissonService;
import org.redisson.api.*;
import org.redisson.api.geo.GeoSearchArgs;
import org.redisson.api.listener.MessageListener;
import org.redisson.api.queue.*;
import org.redisson.api.queue.event.QueueEventListener;
import org.redisson.api.stream.*;
import org.redisson.client.protocol.ScoredEntry;
import org.redisson.codec.JsonCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* Redis 服务实现类
* 基于 Spring Boot 3 + Redisson 封装 Redis 常用能力。
*
* @author Ateng
* @since 2026-04-26
*/
@Service
public class RedissonServiceImpl implements RedissonService {
private static final Logger log = LoggerFactory.getLogger(RedissonServiceImpl.class);
private final RedissonClient redissonClient;
private final ObjectMapper objectMapper;
public RedissonServiceImpl(RedissonClient redissonClient, ObjectMapper objectMapper) {
this.redissonClient = redissonClient;
this.objectMapper = objectMapper;
}
// -------------------------------------------------------------------------
// Redisson 原生对象访问
// -------------------------------------------------------------------------
/**
* 获取 RedissonClient 实例。
*
* @return RedissonClient 实例
*/
@Override
public RedissonClient getClient() {
return redissonClient;
}
/**
* 获取对象桶。
*
* @param key Redis 键
* @param <T> 值类型
* @return RBucket
*/
@Override
public <T> RBucket<T> getBucket(String key) {
checkKey(key);
return redissonClient.getBucket(key);
}
/**
* 获取哈希 Map。
*
* @param key Redis 键
* @param <K> 字段类型
* @param <V> 值类型
* @return RMap
*/
@Override
public <K, V> RMap<K, V> getMap(String key) {
checkKey(key);
return redissonClient.getMap(key);
}
/**
* 获取带 TTL 能力的 MapCache。
*
* @param key Redis 键
* @param <K> 字段类型
* @param <V> 值类型
* @return RMapCache
*/
@Override
public <K, V> RMapCache<K, V> getMapCache(String key) {
checkKey(key);
return redissonClient.getMapCache(key);
}
/**
* 获取列表。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RList
*/
@Override
public <T> RList<T> getList(String key) {
checkKey(key);
return redissonClient.getList(key);
}
/**
* 获取双端队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RDeque
*/
@Override
public <T> RDeque<T> getDeque(String key) {
checkKey(key);
return redissonClient.getDeque(key);
}
/**
* 获取集合。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RSet
*/
@Override
public <T> RSet<T> getSet(String key) {
checkKey(key);
return redissonClient.getSet(key);
}
/**
* 获取带元素 TTL 能力的 SetCache。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RSetCache
*/
@Override
public <T> RSetCache<T> getSetCache(String key) {
checkKey(key);
return redissonClient.getSetCache(key);
}
/**
* 获取有序集合。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RScoredSortedSet
*/
@Override
public <T> RScoredSortedSet<T> getScoredSortedSet(String key) {
checkKey(key);
return redissonClient.getScoredSortedSet(key);
}
/**
* 获取 BitSet。
*
* @param key Redis 键
* @return RBitSet
*/
@Override
public RBitSet getBitSet(String key) {
checkKey(key);
return redissonClient.getBitSet(key);
}
/**
* 获取 HyperLogLog。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RHyperLogLog
*/
@Override
public <T> RHyperLogLog<T> getHyperLogLog(String key) {
checkKey(key);
return redissonClient.getHyperLogLog(key);
}
/**
* 获取 Geo。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RGeo
*/
@Override
public <T> RGeo<T> getGeo(String key) {
checkKey(key);
return redissonClient.getGeo(key);
}
/**
* 获取普通队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RQueue
*/
@Override
public <T> RQueue<T> getQueue(String key) {
checkKey(key);
return redissonClient.getQueue(key);
}
/**
* 获取阻塞队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RBlockingQueue
*/
@Override
public <T> RBlockingQueue<T> getBlockingQueue(String key) {
checkKey(key);
return redissonClient.getBlockingQueue(key);
}
/**
* 获取可靠队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RReliableQueue
*/
@Override
public <T> RReliableQueue<T> getReliableQueue(String key) {
checkKey(key);
return redissonClient.getReliableQueue(key);
}
/**
* 获取消息主题。
*
* @param topic 主题名称
* @return RTopic
*/
@Override
public RTopic getTopic(String topic) {
checkKey(topic);
return redissonClient.getTopic(topic);
}
/**
* 获取 Stream。
*
* @param key Redis 键
* @param <K> 字段类型
* @param <V> 值类型
* @return RStream
*/
@Override
public <K, V> RStream<K, V> getStream(String key) {
checkKey(key);
return redissonClient.getStream(key);
}
/**
* 获取脚本执行对象。
*
* @return RScript
*/
@Override
public RScript getScript() {
return redissonClient.getScript();
}
// -------------------------------------------------------------------------
// 通用 Key 管理
// -------------------------------------------------------------------------
/**
* 判断指定 key 是否存在。
*
* @param key Redis 键
* @return 存在返回 true
*/
@Override
public boolean hasKey(String key) {
if (StrUtil.isBlank(key)) {
return false;
}
return redissonClient.getKeys().countExists(key) > 0;
}
/**
* 统计多个 key 中实际存在的数量。
*
* @param keys Redis 键集合
* @return 存在数量
*/
@Override
public long countExists(String... keys) {
if (ArrayUtil.isEmpty(keys)) {
return 0L;
}
String[] keyArray = filterKeys(keys);
if (ArrayUtil.isEmpty(keyArray)) {
return 0L;
}
return redissonClient.getKeys().countExists(keyArray);
}
/**
* 删除指定 key。
*
* @param key Redis 键
* @return 是否删除成功
*/
@Override
public boolean deleteKey(String key) {
if (StrUtil.isBlank(key)) {
return false;
}
return redissonClient.getKeys().delete(key) > 0;
}
/**
* 批量删除 key。
*
* @param keys Redis 键集合
* @return 删除数量
*/
@Override
public long deleteKeys(Collection<String> keys) {
if (CollUtil.isEmpty(keys)) {
return 0L;
}
String[] keyArray = keys.stream()
.filter(StrUtil::isNotBlank)
.distinct()
.toArray(String[]::new);
if (ArrayUtil.isEmpty(keyArray)) {
return 0L;
}
return redissonClient.getKeys().delete(keyArray);
}
/**
* 根据通配符删除 key。
*
* @param pattern 通配符表达式
* @return 删除数量
*/
@Override
public long deleteByPattern(String pattern) {
if (StrUtil.isBlank(pattern)) {
return 0L;
}
return redissonClient.getKeys().deleteByPattern(pattern);
}
/**
* 设置 key 过期时间。
*
* @param key Redis 键
* @param timeout 超时时间
* @param unit 时间单位
* @return 是否设置成功
*/
@Override
public boolean expire(String key, long timeout, TimeUnit unit) {
checkKey(key);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(timeout > 0, "过期时间必须大于 0");
return redissonClient.getBucket(key).expire(timeout, unit);
}
/**
* 设置 key 过期时间。
*
* @param key Redis 键
* @param ttl 过期时间
* @return 是否设置成功
*/
@Override
public boolean expire(String key, Duration ttl) {
checkKey(key);
checkPositiveDuration(ttl, "过期时间不能为空且必须大于 0");
return redissonClient.getBucket(key).expire(ttl);
}
/**
* 获取 key 剩余过期时间。
*
* @param key Redis 键
* @param unit 时间单位
* @return 剩余时间,-1 表示永久,-2 表示不存在
*/
@Override
public long getTtl(String key, TimeUnit unit) {
checkKey(key);
Assert.notNull(unit, "时间单位不能为空");
long ttlMillis = redissonClient.getBucket(key).remainTimeToLive();
if (ttlMillis < 0) {
return ttlMillis;
}
return unit.convert(ttlMillis, TimeUnit.MILLISECONDS);
}
/**
* 移除 key 过期时间。
*
* @param key Redis 键
* @return 是否成功
*/
@Override
public boolean persist(String key) {
checkKey(key);
return redissonClient.getBucket(key).clearExpire();
}
/**
* 修改 key 名称。
*
* @param oldKey 旧 key
* @param newKey 新 key
* @return 是否成功
*/
@Override
public boolean renameKey(String oldKey, String newKey) {
checkKey(oldKey);
checkKey(newKey);
try {
redissonClient.getKeys().rename(oldKey, newKey);
return true;
} catch (Exception e) {
log.warn("重命名 Redis Key 失败,oldKey={},newKey={}", oldKey, newKey, e);
return false;
}
}
/**
* 新 key 不存在时修改 key 名称。
*
* @param oldKey 旧 key
* @param newKey 新 key
* @return 是否成功
*/
@Override
public boolean renameKeyIfAbsent(String oldKey, String newKey) {
checkKey(oldKey);
checkKey(newKey);
return redissonClient.getKeys().renamenx(oldKey, newKey);
}
/**
* 查询匹配通配符的 key。
*
* @param pattern 通配符表达式
* @return key 集合
*/
@Override
public Set<String> keys(String pattern) {
if (StrUtil.isBlank(pattern)) {
return Collections.emptySet();
}
Set<String> result = new LinkedHashSet<>();
Iterable<String> iterable = redissonClient.getKeys().getKeysByPattern(pattern);
for (String key : iterable) {
result.add(key);
}
return result;
}
/**
* 查询匹配通配符的 key,并限制返回数量。
*
* @param pattern 通配符表达式
* @param count 最大数量
* @return key 集合
*/
@Override
public Set<String> scanKeys(String pattern, int count) {
if (StrUtil.isBlank(pattern) || count <= 0) {
return Collections.emptySet();
}
Set<String> result = new LinkedHashSet<>();
Iterable<String> iterable = redissonClient.getKeys().getKeysByPattern(pattern);
for (String key : iterable) {
result.add(key);
if (result.size() >= count) {
break;
}
}
return result;
}
/**
* 判断 key 是否已过期或不存在。
*
* @param key Redis 键
* @return 已过期或不存在返回 true
*/
@Override
public boolean isExpired(String key) {
if (StrUtil.isBlank(key)) {
return true;
}
long ttlMillis = redissonClient.getBucket(key).remainTimeToLive();
return ttlMillis == -2 || ttlMillis == 0;
}
/**
* 获取 key 类型。
*
* @param key Redis 键
* @return 类型名称
*/
@Override
public String getKeyType(String key) {
if (StrUtil.isBlank(key)) {
return null;
}
RType type = redissonClient.getKeys().getType(key);
return ObjectUtil.isNull(type) ? null : type.name().toLowerCase();
}
// -------------------------------------------------------------------------
// 类型转换
// -------------------------------------------------------------------------
/**
* 将对象转换为指定类型。
*
* @param value 原始值
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 转换后的值
*/
@Override
public <T> T convertValue(Object value, Class<T> clazz) {
// 原始值或目标类型为空时,无法进行有效转换,直接返回 null
if (ObjectUtil.isNull(value) || ObjectUtil.isNull(clazz)) {
return null;
}
// 如果原始值本身已经是目标类型,直接强转返回,避免重复序列化和反序列化
if (clazz.isInstance(value)) {
return clazz.cast(value);
}
try {
// Redis 中常见的值可能是 JSON 字符串,这里优先按 JSON 字符串处理
if (value instanceof String text) {
// 目标类型就是 String 时,直接返回原字符串
if (String.class.equals(clazz)) {
return clazz.cast(text);
}
// 目标类型不是 String 时,尝试将字符串按 JSON 反序列化为目标类型
return objectMapper.readValue(text, clazz);
}
// 非字符串对象使用 Jackson 的 convertValue 进行类型转换
// 适用于 LinkedHashMap -> JavaBean、Map -> DTO、基础类型转换等场景
return objectMapper.convertValue(value, clazz);
} catch (IllegalArgumentException | JsonProcessingException e) {
// 转换失败时不向外抛出异常,避免缓存值格式异常影响主流程
// 这里记录目标类型和原始值类型,便于排查 Redis 中的数据结构问题
log.warn("Redis 值类型转换失败,targetType={},valueType={}",
clazz.getName(), value.getClass().getName(), e);
return null;
}
}
/**
* 将对象转换为指定泛型类型。
*
* @param value 原始值
* @param typeReference 目标类型引用
* @param <T> 泛型类型
* @return 转换后的值
*/
@Override
public <T> T convertValue(Object value, TypeReference<T> typeReference) {
// 原始值或泛型类型引用为空时,无法进行有效转换,直接返回 null
if (ObjectUtil.isNull(value) || ObjectUtil.isNull(typeReference)) {
return null;
}
try {
// Redis 中存储的泛型数据可能是 JSON 字符串,例如 List<User>、Map<String, User>
if (value instanceof String text) {
try {
// 优先将字符串按 JSON 反序列化为指定泛型类型
return objectMapper.readValue(text, typeReference);
} catch (JsonProcessingException ignored) {
// 如果字符串不是合法 JSON,则退回到 convertValue
// 例如目标泛型实际可以接收 String、Object 等简单类型时,仍有机会转换成功
return objectMapper.convertValue(value, typeReference);
}
}
// 非字符串对象使用 Jackson 的 convertValue 进行泛型转换
// 适用于 LinkedHashMap -> 泛型 DTO、List<LinkedHashMap> -> List<DTO> 等场景
return objectMapper.convertValue(value, typeReference);
} catch (IllegalArgumentException e) {
// 泛型转换失败通常是结构不匹配,例如字段类型不兼容、集合元素类型不一致等
// 记录目标泛型类型和原始值类型,便于定位缓存数据问题
log.warn("Redis 值泛型转换失败,targetType={},valueType={}",
typeReference.getType(), value.getClass().getName(), e);
return null;
}
}
// -------------------------------------------------------------------------
// 字符串 / Bucket 操作
// -------------------------------------------------------------------------
/**
* 设置缓存值。
*
* @param key Redis 键
* @param value 缓存值
*/
@Override
public void set(String key, Object value) {
checkKey(key);
redissonClient.getBucket(key).set(value);
}
/**
* 设置缓存值并指定过期时间。
*
* @param key Redis 键
* @param value 缓存值
* @param timeout 超时时间
* @param unit 时间单位
*/
@Override
public void set(String key, Object value, long timeout, TimeUnit unit) {
checkKey(key);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(timeout > 0, "过期时间必须大于 0");
redissonClient.getBucket(key).set(value, timeout, unit);
}
/**
* 设置缓存值并指定过期时间。
*
* @param key Redis 键
* @param value 缓存值
* @param ttl 过期时间
*/
@Override
public void set(String key, Object value, Duration ttl) {
checkKey(key);
checkPositiveDuration(ttl, "过期时间不能为空且必须大于 0");
redissonClient.getBucket(key).set(value, ttl.toMillis(), TimeUnit.MILLISECONDS);
}
/**
* 获取缓存值。
*
* @param key Redis 键
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 缓存值
*/
@Override
public <T> T get(String key, Class<T> clazz) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(clazz)) {
return null;
}
Object value = redissonClient.getBucket(key).get();
return convertValue(value, clazz);
}
/**
* 获取缓存值。
*
* @param key Redis 键
* @param typeReference 目标泛型类型
* @param <T> 泛型类型
* @return 缓存值
*/
@Override
public <T> T get(String key, TypeReference<T> typeReference) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(typeReference)) {
return null;
}
Object value = redissonClient.getBucket(key).get();
return convertValue(value, typeReference);
}
/**
* key 不存在时设置缓存值。
*
* @param key Redis 键
* @param value 缓存值
* @param timeout 超时时间
* @param unit 时间单位
* @return 是否设置成功
*/
@Override
public boolean setIfAbsent(String key, Object value, long timeout, TimeUnit unit) {
checkKey(key);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(timeout > 0, "过期时间必须大于 0");
return redissonClient.getBucket(key).setIfAbsent(value, Duration.ofMillis(unit.toMillis(timeout)));
}
/**
* key 不存在时设置缓存值。
*
* @param key Redis 键
* @param value 缓存值
* @param ttl 过期时间
* @return 是否设置成功
*/
@Override
public boolean setIfAbsent(String key, Object value, Duration ttl) {
checkKey(key);
checkPositiveDuration(ttl, "过期时间不能为空且必须大于 0");
return redissonClient.getBucket(key).setIfAbsent(value, ttl);
}
/**
* key 存在时设置缓存值。
*
* @param key Redis 键
* @param value 缓存值
* @return 是否设置成功
*/
@Override
public boolean setIfExists(String key, Object value) {
checkKey(key);
return redissonClient.getBucket(key).setIfExists(value);
}
/**
* key 存在时设置缓存值并指定过期时间。
*
* @param key Redis 键
* @param value 缓存值
* @param ttl 过期时间
* @return 是否设置成功
*/
@Override
public boolean setIfExists(String key, Object value, Duration ttl) {
checkKey(key);
checkPositiveDuration(ttl, "过期时间不能为空且必须大于 0");
return redissonClient.getBucket(key).setIfExists(value, ttl.toMillis(), TimeUnit.MILLISECONDS);
}
/**
* 原子替换并返回旧值。
*
* @param key Redis 键
* @param value 新值
* @param clazz 旧值类型
* @param <T> 泛型类型
* @return 旧值
*/
@Override
public <T> T getAndSet(String key, Object value, Class<T> clazz) {
checkKey(key);
Object oldValue = redissonClient.getBucket(key).getAndSet(value);
return convertValue(oldValue, clazz);
}
/**
* 原子替换并返回旧值。
*
* @param key Redis 键
* @param value 新值
* @param typeReference 旧值类型
* @param <T> 泛型类型
* @return 旧值
*/
@Override
public <T> T getAndSet(String key, Object value, TypeReference<T> typeReference) {
checkKey(key);
Object oldValue = redissonClient.getBucket(key).getAndSet(value);
return convertValue(oldValue, typeReference);
}
/**
* 获取并删除缓存值。
*
* @param key Redis 键
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 删除前的值
*/
@Override
public <T> T getAndDelete(String key, Class<T> clazz) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(clazz)) {
return null;
}
Object value = redissonClient.getBucket(key).getAndDelete();
return convertValue(value, clazz);
}
/**
* 批量获取缓存值。
*
* @param keys Redis 键集合
* @return 键值 Map
*/
@Override
public Map<String, Object> entries(Collection<String> keys) {
if (CollUtil.isEmpty(keys)) {
return Collections.emptyMap();
}
String[] keyArray = keys.stream()
.filter(StrUtil::isNotBlank)
.distinct()
.toArray(String[]::new);
if (ArrayUtil.isEmpty(keyArray)) {
return Collections.emptyMap();
}
return redissonClient.getBuckets().get(keyArray);
}
/**
* 批量获取缓存值并转换类型。
*
* @param keys Redis 键集合
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 键值 Map
*/
@Override
public <T> Map<String, T> entries(Collection<String> keys, Class<T> clazz) {
if (CollUtil.isEmpty(keys) || ObjectUtil.isNull(clazz)) {
return Collections.emptyMap();
}
Map<String, Object> rawMap = entries(keys);
if (CollUtil.isEmpty(rawMap)) {
return Collections.emptyMap();
}
Map<String, T> result = new LinkedHashMap<>(rawMap.size());
rawMap.forEach((key, value) -> {
T converted = convertValue(value, clazz);
if (ObjectUtil.isNotNull(converted)) {
result.put(key, converted);
}
});
return result;
}
/**
* 批量获取缓存值并转换泛型类型。
*
* @param keys Redis 键集合
* @param typeReference 目标类型
* @param <T> 泛型类型
* @return 键值 Map
*/
@Override
public <T> Map<String, T> entries(Collection<String> keys, TypeReference<T> typeReference) {
if (CollUtil.isEmpty(keys) || ObjectUtil.isNull(typeReference)) {
return Collections.emptyMap();
}
Map<String, Object> rawMap = entries(keys);
if (CollUtil.isEmpty(rawMap)) {
return Collections.emptyMap();
}
Map<String, T> result = new LinkedHashMap<>(rawMap.size());
rawMap.forEach((key, value) -> {
T converted = convertValue(value, typeReference);
if (ObjectUtil.isNotNull(converted)) {
result.put(key, converted);
}
});
return result;
}
/**
* 获取序列化后的字节大小。
*
* @param key Redis 键
* @return 字节大小
*/
@Override
public long size(String key) {
if (StrUtil.isBlank(key)) {
return 0L;
}
return redissonClient.getBucket(key).size();
}
// -------------------------------------------------------------------------
// 原子数值 / 计数器 / ID
// -------------------------------------------------------------------------
/**
* 获取长整型原子对象。
*
* @param key Redis 键
* @return RAtomicLong
*/
@Override
public RAtomicLong getAtomicLong(String key) {
checkKey(key);
return redissonClient.getAtomicLong(key);
}
/**
* 获取浮点型原子对象。
*
* @param key Redis 键
* @return RAtomicDouble
*/
@Override
public RAtomicDouble getAtomicDouble(String key) {
checkKey(key);
return redissonClient.getAtomicDouble(key);
}
/**
* 整数自增。
*
* @param key Redis 键
* @param delta 增量
* @return 最新值
*/
@Override
public long increment(String key, long delta) {
checkKey(key);
return redissonClient.getAtomicLong(key).addAndGet(delta);
}
/**
* 整数自减。
*
* @param key Redis 键
* @param delta 减量
* @return 最新值
*/
@Override
public long decrement(String key, long delta) {
checkKey(key);
Assert.isTrue(delta >= 0, "递减值不能小于 0");
return redissonClient.getAtomicLong(key).addAndGet(-delta);
}
/**
* 浮点数自增。
*
* @param key Redis 键
* @param delta 增量
* @return 最新值
*/
@Override
public double incrementDouble(String key, double delta) {
checkKey(key);
return redissonClient.getAtomicDouble(key).addAndGet(delta);
}
/**
* 浮点数自减。
*
* @param key Redis 键
* @param delta 减量
* @return 最新值
*/
@Override
public double decrementDouble(String key, double delta) {
checkKey(key);
Assert.isTrue(delta >= 0, "递减值不能小于 0");
return redissonClient.getAtomicDouble(key).addAndGet(-delta);
}
/**
* 设置整数计数器值。
*
* @param key Redis 键
* @param value 值
*/
@Override
public void setAtomicLong(String key, long value) {
checkKey(key);
redissonClient.getAtomicLong(key).set(value);
}
/**
* 获取整数计数器值。
*
* @param key Redis 键
* @return 当前值
*/
@Override
public long getAtomicLongValue(String key) {
checkKey(key);
return redissonClient.getAtomicLong(key).get();
}
/**
* 重置整数计数器。
*
* @param key Redis 键
*/
@Override
public void resetAtomicLong(String key) {
checkKey(key);
redissonClient.getAtomicLong(key).set(0L);
log.info("Redis 计数器已重置,key={}", key);
}
/**
* 获取分布式 ID 生成器。
*
* @param key Redis 键
* @return RIdGenerator
*/
@Override
public RIdGenerator getIdGenerator(String key) {
checkKey(key);
return redissonClient.getIdGenerator(key);
}
/**
* 初始化分布式 ID 生成器。
*
* @param key Redis 键
* @param initialValue 初始值
* @param allocationSize 每次分配步长
* @return 是否初始化成功
*/
@Override
public boolean idGeneratorInit(String key, long initialValue, long allocationSize) {
checkKey(key);
Assert.isTrue(allocationSize > 0, "ID 分配步长必须大于 0");
boolean initialized = redissonClient.getIdGenerator(key).tryInit(initialValue, allocationSize);
if (initialized) {
log.info("Redis 分布式 ID 生成器初始化成功,key={},initialValue={},allocationSize={}",
key, initialValue, allocationSize);
} else {
log.debug("Redis 分布式 ID 生成器已存在,跳过初始化,key={}", key);
}
return initialized;
}
/**
* 获取下一个分布式 ID。
*
* @param key Redis 键
* @return ID
*/
@Override
public long nextId(String key) {
checkKey(key);
return redissonClient.getIdGenerator(key).nextId();
}
// -------------------------------------------------------------------------
// Hash / MapCache 操作
// -------------------------------------------------------------------------
/**
* 设置哈希字段值。
*
* @param key Redis 键
* @param field 字段名
* @param value 字段值
*/
@Override
public void hPut(String key, String field, Object value) {
checkKey(key);
checkField(field);
redissonClient.<String, Object>getMap(key).fastPut(field, value);
}
/**
* 批量设置哈希字段值。
*
* @param key Redis 键
* @param map 字段 Map
*/
@Override
public void hPutAll(String key, Map<String, ?> map) {
checkKey(key);
if (CollUtil.isEmpty(map)) {
return;
}
Map<String, Object> valueMap = new LinkedHashMap<>(map.size());
map.forEach((field, value) -> {
if (StrUtil.isNotBlank(field)) {
valueMap.put(field, value);
}
});
if (CollUtil.isNotEmpty(valueMap)) {
redissonClient.<String, Object>getMap(key).putAll(valueMap);
}
}
/**
* 字段不存在时设置哈希字段值。
*
* @param key Redis 键
* @param field 字段名
* @param value 字段值
* @return 是否设置成功
*/
@Override
public boolean hPutIfAbsent(String key, String field, Object value) {
checkKey(key);
checkField(field);
return redissonClient.<String, Object>getMap(key).fastPutIfAbsent(field, value);
}
/**
* 获取哈希字段值。
*
* @param key Redis 键
* @param field 字段名
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 字段值
*/
@Override
public <T> T hGet(String key, String field, Class<T> clazz) {
if (StrUtil.hasBlank(key, field) || ObjectUtil.isNull(clazz)) {
return null;
}
Object value = redissonClient.<String, Object>getMap(key).get(field);
return convertValue(value, clazz);
}
/**
* 获取哈希字段值。
*
* @param key Redis 键
* @param field 字段名
* @param typeReference 目标类型
* @param <T> 泛型类型
* @return 字段值
*/
@Override
public <T> T hGet(String key, String field, TypeReference<T> typeReference) {
if (StrUtil.hasBlank(key, field) || ObjectUtil.isNull(typeReference)) {
return null;
}
Object value = redissonClient.<String, Object>getMap(key).get(field);
return convertValue(value, typeReference);
}
/**
* 批量获取哈希字段值。
*
* @param key Redis 键
* @param fields 字段集合
* @return 字段值 Map
*/
@Override
public Map<String, Object> hMultiGet(String key, Collection<String> fields) {
if (StrUtil.isBlank(key) || CollUtil.isEmpty(fields)) {
return Collections.emptyMap();
}
Set<String> fieldSet = fields.stream()
.filter(StrUtil::isNotBlank)
.collect(LinkedHashSet::new, LinkedHashSet::add, LinkedHashSet::addAll);
if (CollUtil.isEmpty(fieldSet)) {
return Collections.emptyMap();
}
return redissonClient.<String, Object>getMap(key).getAll(fieldSet);
}
/**
* 删除哈希字段。
*
* @param key Redis 键
* @param fields 字段名
* @return 删除数量
*/
@Override
public long hDelete(String key, String... fields) {
if (StrUtil.isBlank(key) || ArrayUtil.isEmpty(fields)) {
return 0L;
}
String[] fieldArray = Arrays.stream(fields)
.filter(StrUtil::isNotBlank)
.distinct()
.toArray(String[]::new);
if (ArrayUtil.isEmpty(fieldArray)) {
return 0L;
}
return redissonClient.<String, Object>getMap(key).fastRemove(fieldArray);
}
/**
* 判断哈希字段是否存在。
*
* @param key Redis 键
* @param field 字段名
* @return 存在返回 true
*/
@Override
public boolean hHasKey(String key, String field) {
if (StrUtil.hasBlank(key, field)) {
return false;
}
return redissonClient.<String, Object>getMap(key).containsKey(field);
}
/**
* 获取哈希全部字段和值。
*
* @param key Redis 键
* @return 字段值 Map
*/
@Override
public Map<String, Object> hEntries(String key) {
if (StrUtil.isBlank(key)) {
return Collections.emptyMap();
}
return redissonClient.<String, Object>getMap(key).readAllMap();
}
/**
* 获取哈希全部字段和值并转换类型。
*
* @param key Redis 键
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 字段值 Map
*/
@Override
public <T> Map<String, T> hEntries(String key, Class<T> clazz) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(clazz)) {
return Collections.emptyMap();
}
Map<String, Object> rawMap = hEntries(key);
if (CollUtil.isEmpty(rawMap)) {
return Collections.emptyMap();
}
Map<String, T> result = new LinkedHashMap<>(rawMap.size());
rawMap.forEach((field, value) -> {
T converted = convertValue(value, clazz);
if (ObjectUtil.isNotNull(converted)) {
result.put(field, converted);
}
});
return result;
}
/**
* 获取哈希全部字段和值并转换泛型类型。
*
* @param key Redis 键
* @param typeReference 目标类型
* @param <T> 泛型类型
* @return 字段值 Map
*/
@Override
public <T> Map<String, T> hEntries(String key, TypeReference<T> typeReference) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(typeReference)) {
return Collections.emptyMap();
}
Map<String, Object> rawMap = hEntries(key);
if (CollUtil.isEmpty(rawMap)) {
return Collections.emptyMap();
}
Map<String, T> result = new LinkedHashMap<>(rawMap.size());
rawMap.forEach((field, value) -> {
T converted = convertValue(value, typeReference);
if (ObjectUtil.isNotNull(converted)) {
result.put(field, converted);
}
});
return result;
}
/**
* 获取哈希字段名集合。
*
* @param key Redis 键
* @return 字段集合
*/
@Override
public Set<String> hKeys(String key) {
if (StrUtil.isBlank(key)) {
return Collections.emptySet();
}
return redissonClient.<String, Object>getMap(key).readAllKeySet();
}
/**
* 获取哈希字段值集合。
*
* @param key Redis 键
* @return 字段值集合
*/
@Override
public Collection<Object> hValues(String key) {
if (StrUtil.isBlank(key)) {
return Collections.emptyList();
}
return redissonClient.<String, Object>getMap(key).readAllValues();
}
/**
* 获取哈希字段数量。
*
* @param key Redis 键
* @return 字段数量
*/
@Override
public int hSize(String key) {
if (StrUtil.isBlank(key)) {
return 0;
}
return redissonClient.<String, Object>getMap(key).size();
}
/**
* 哈希字段整数自增。
*
* @param key Redis 键
* @param field 字段名
* @param delta 增量
* @return 最新值
*/
@Override
public long hIncrement(String key, String field, long delta) {
checkKey(key);
checkField(field);
Number value = redissonClient.<String, Number>getMap(key).addAndGet(field, delta);
return ObjectUtil.isNull(value) ? 0L : value.longValue();
}
/**
* 哈希字段浮点数自增。
*
* @param key Redis 键
* @param field 字段名
* @param delta 增量
* @return 最新值
*/
@Override
public double hIncrementDouble(String key, String field, double delta) {
checkKey(key);
checkField(field);
Number value = redissonClient.<String, Number>getMap(key).addAndGet(field, delta);
return ObjectUtil.isNull(value) ? 0D : value.doubleValue();
}
/**
* 清空哈希。
*
* @param key Redis 键
*/
@Override
public void hClear(String key) {
if (StrUtil.isBlank(key)) {
return;
}
redissonClient.<String, Object>getMap(key).clear();
}
/**
* 设置 MapCache 字段值并指定字段级 TTL。
*
* @param key Redis 键
* @param field 字段名
* @param value 字段值
* @param ttl 字段 TTL
*/
@Override
public void hcPut(String key, String field, Object value, Duration ttl) {
checkKey(key);
checkField(field);
checkPositiveDuration(ttl, "字段过期时间不能为空且必须大于 0");
redissonClient.<String, Object>getMapCache(key)
.fastPut(field, value, ttl.toMillis(), TimeUnit.MILLISECONDS);
}
/**
* 设置 MapCache 字段值并指定字段级 TTL 与最大空闲时间。
*
* @param key Redis 键
* @param field 字段名
* @param value 字段值
* @param ttl 字段 TTL
* @param maxIdle 最大空闲时间
*/
@Override
public void hcPut(String key, String field, Object value, Duration ttl, Duration maxIdle) {
checkKey(key);
checkField(field);
checkPositiveDuration(ttl, "字段过期时间不能为空且必须大于 0");
checkPositiveDuration(maxIdle, "字段最大空闲时间不能为空且必须大于 0");
redissonClient.<String, Object>getMapCache(key)
.fastPut(
field,
value,
ttl.toMillis(),
TimeUnit.MILLISECONDS,
maxIdle.toMillis(),
TimeUnit.MILLISECONDS
);
}
/**
* 获取 MapCache 字段值。
*
* @param key Redis 键
* @param field 字段名
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 字段值
*/
@Override
public <T> T hcGet(String key, String field, Class<T> clazz) {
if (StrUtil.hasBlank(key, field) || ObjectUtil.isNull(clazz)) {
return null;
}
Object value = redissonClient.<String, Object>getMapCache(key).get(field);
return convertValue(value, clazz);
}
/**
* 删除 MapCache 字段。
*
* @param key Redis 键
* @param fields 字段名
* @return 删除数量
*/
@Override
public long hcDelete(String key, String... fields) {
if (StrUtil.isBlank(key) || ArrayUtil.isEmpty(fields)) {
return 0L;
}
String[] fieldArray = Arrays.stream(fields)
.filter(StrUtil::isNotBlank)
.distinct()
.toArray(String[]::new);
if (ArrayUtil.isEmpty(fieldArray)) {
return 0L;
}
return redissonClient.<String, Object>getMapCache(key).fastRemove(fieldArray);
}
// -------------------------------------------------------------------------
// List / Deque 操作
// -------------------------------------------------------------------------
/**
* 左侧压入列表。
*
* @param key Redis 键
* @param value 元素
*/
@Override
public void lLeftPush(String key, Object value) {
checkKey(key);
redissonClient.<Object>getDeque(key).addFirst(value);
}
/**
* 右侧压入列表。
*
* @param key Redis 键
* @param value 元素
*/
@Override
public void lRightPush(String key, Object value) {
checkKey(key);
redissonClient.<Object>getDeque(key).addLast(value);
}
/**
* 批量右侧压入列表。
*
* @param key Redis 键
* @param values 元素集合
*/
@Override
public void lRightPushAll(String key, Collection<?> values) {
checkKey(key);
if (CollUtil.isEmpty(values)) {
return;
}
redissonClient.<Object>getList(key).addAll(values);
}
/**
* 左侧弹出列表元素。
*
* @param key Redis 键
* @return 元素
*/
@Override
public Object lLeftPop(String key) {
if (StrUtil.isBlank(key)) {
return null;
}
return redissonClient.<Object>getDeque(key).pollFirst();
}
/**
* 左侧弹出列表元素并转换类型。
*
* @param key Redis 键
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 元素
*/
@Override
public <T> T lLeftPop(String key, Class<T> clazz) {
Object value = lLeftPop(key);
return convertValue(value, clazz);
}
/**
* 右侧弹出列表元素。
*
* @param key Redis 键
* @return 元素
*/
@Override
public Object lRightPop(String key) {
if (StrUtil.isBlank(key)) {
return null;
}
return redissonClient.<Object>getDeque(key).pollLast();
}
/**
* 阻塞式左侧弹出列表元素。
*
* @param key Redis 键
* @param timeout 超时时间
* @param unit 时间单位
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 元素
* @throws InterruptedException 线程中断时抛出
*/
@Override
public <T> T lLeftPop(String key, long timeout, TimeUnit unit, Class<T> clazz) throws InterruptedException {
checkKey(key);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(timeout >= 0, "等待时间不能小于 0");
Object value = redissonClient.<Object>getBlockingDeque(key).pollFirst(timeout, unit);
return convertValue(value, clazz);
}
/**
* 获取列表范围。
*
* @param key Redis 键
* @param start 开始索引
* @param end 结束索引
* @return 元素集合
*/
@Override
public List<Object> lRange(String key, long start, long end) {
if (StrUtil.isBlank(key)) {
return Collections.emptyList();
}
RList<Object> list = redissonClient.getList(key);
int size = list.size();
if (size <= 0) {
return Collections.emptyList();
}
int fromIndex = normalizeIndex(start, size);
int toIndex = normalizeIndex(end, size);
fromIndex = Math.max(fromIndex, 0);
toIndex = Math.min(toIndex, size - 1);
if (fromIndex > toIndex) {
return Collections.emptyList();
}
return new ArrayList<>(list.subList(fromIndex, toIndex + 1));
}
/**
* 获取列表范围并转换类型。
*
* @param key Redis 键
* @param start 开始索引
* @param end 结束索引
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 元素集合
*/
@Override
public <T> List<T> lRange(String key, long start, long end, Class<T> clazz) {
if (ObjectUtil.isNull(clazz)) {
return Collections.emptyList();
}
List<Object> rawList = lRange(key, start, end);
if (CollUtil.isEmpty(rawList)) {
return Collections.emptyList();
}
List<T> result = new ArrayList<>(rawList.size());
for (Object item : rawList) {
T converted = convertValue(item, clazz);
if (ObjectUtil.isNotNull(converted)) {
result.add(converted);
}
}
return result;
}
/**
* 获取列表长度。
*
* @param key Redis 键
* @return 长度
*/
@Override
public long lSize(String key) {
if (StrUtil.isBlank(key)) {
return 0L;
}
return redissonClient.<Object>getList(key).size();
}
/**
* 删除列表元素。
*
* @param key Redis 键
* @param count 删除数量规则
* @param value 元素
* @return 删除数量
*/
@Override
public long lRemove(String key, long count, Object value) {
if (StrUtil.isBlank(key)) {
return 0L;
}
RList<Object> list = redissonClient.getList(key);
if (list.isEmpty()) {
return 0L;
}
if (count == 0) {
long removed = 0L;
while (list.remove(value)) {
removed++;
}
return removed;
}
List<Object> copy = new ArrayList<>(list);
long target = Math.abs(count);
long removed = 0L;
if (count > 0) {
for (int i = 0; i < copy.size() && removed < target; i++) {
if (ObjectUtil.equal(copy.get(i), value)) {
copy.remove(i--);
removed++;
}
}
} else {
for (int i = copy.size() - 1; i >= 0 && removed < target; i--) {
if (ObjectUtil.equal(copy.get(i), value)) {
copy.remove(i);
removed++;
}
}
}
if (removed > 0) {
list.clear();
list.addAll(copy);
}
return removed;
}
/**
* 获取列表指定索引元素。
*
* @param key Redis 键
* @param index 索引
* @return 元素
*/
@Override
public Object lIndex(String key, long index) {
if (StrUtil.isBlank(key)) {
return null;
}
RList<Object> list = redissonClient.getList(key);
int size = list.size();
if (size <= 0) {
return null;
}
int realIndex = normalizeIndex(index, size);
if (realIndex < 0 || realIndex >= size) {
return null;
}
return list.get(realIndex);
}
/**
* 获取列表指定索引元素并转换类型。
*
* @param key Redis 键
* @param index 索引
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 元素
*/
@Override
public <T> T lIndex(String key, long index, Class<T> clazz) {
Object value = lIndex(key, index);
return convertValue(value, clazz);
}
/**
* 设置列表指定索引元素。
*
* @param key Redis 键
* @param index 索引
* @param value 元素
*/
@Override
public void lSet(String key, long index, Object value) {
checkKey(key);
RList<Object> list = redissonClient.getList(key);
int size = list.size();
int realIndex = normalizeIndex(index, size);
Assert.isTrue(realIndex >= 0 && realIndex < size, "列表索引超出范围");
list.set(realIndex, value);
}
/**
* 裁剪列表范围。
*
* @param key Redis 键
* @param start 开始索引
* @param end 结束索引
*/
@Override
public void lTrim(String key, int start, int end) {
checkKey(key);
List<Object> range = lRange(key, start, end);
RList<Object> list = redissonClient.getList(key);
list.clear();
if (CollUtil.isNotEmpty(range)) {
list.addAll(range);
}
}
/**
* 清空列表。
*
* @param key Redis 键
*/
@Override
public void lClear(String key) {
if (StrUtil.isBlank(key)) {
return;
}
redissonClient.<Object>getList(key).clear();
}
// -------------------------------------------------------------------------
// Set / SetCache 操作
// -------------------------------------------------------------------------
/**
* 添加集合元素。
*
* @param key Redis 键
* @param values 元素
* @return 是否有新增
*/
@Override
public boolean sAdd(String key, Object... values) {
if (StrUtil.isBlank(key) || ArrayUtil.isEmpty(values)) {
return false;
}
return redissonClient.<Object>getSet(key).addAll(Arrays.asList(values));
}
/**
* 添加集合元素。
*
* @param key Redis 键
* @param values 元素集合
* @return 是否有新增
*/
@Override
public boolean sAdd(String key, Collection<?> values) {
if (StrUtil.isBlank(key) || CollUtil.isEmpty(values)) {
return false;
}
return redissonClient.<Object>getSet(key).addAll(values);
}
/**
* 添加 SetCache 元素并指定元素 TTL。
*
* @param key Redis 键
* @param value 元素
* @param ttl TTL
* @return 是否添加成功
*/
@Override
public boolean scAdd(String key, Object value, Duration ttl) {
checkKey(key);
checkPositiveDuration(ttl, "元素过期时间不能为空且必须大于 0");
return redissonClient.<Object>getSetCache(key).add(value, ttl.toMillis(), TimeUnit.MILLISECONDS);
}
/**
* 判断集合中是否存在元素。
*
* @param key Redis 键
* @param value 元素
* @return 存在返回 true
*/
@Override
public boolean sIsMember(String key, Object value) {
if (StrUtil.isBlank(key)) {
return false;
}
return redissonClient.<Object>getSet(key).contains(value);
}
/**
* 获取集合所有元素。
*
* @param key Redis 键
* @return 元素集合
*/
@Override
public Set<Object> sMembers(String key) {
if (StrUtil.isBlank(key)) {
return Collections.emptySet();
}
return redissonClient.<Object>getSet(key).readAll();
}
/**
* 获取集合所有元素并转换类型。
*
* @param key Redis 键
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 元素集合
*/
@Override
public <T> Set<T> sMembers(String key, Class<T> clazz) {
if (ObjectUtil.isNull(clazz)) {
return Collections.emptySet();
}
return convertSet(sMembers(key), clazz);
}
/**
* 获取集合大小。
*
* @param key Redis 键
* @return 大小
*/
@Override
public long sSize(String key) {
if (StrUtil.isBlank(key)) {
return 0L;
}
return redissonClient.<Object>getSet(key).size();
}
/**
* 随机弹出集合元素。
*
* @param key Redis 键
* @return 元素
*/
@Override
public Object sPop(String key) {
if (StrUtil.isBlank(key)) {
return null;
}
return redissonClient.<Object>getSet(key).removeRandom();
}
/**
* 删除集合元素。
*
* @param key Redis 键
* @param values 元素
* @return 是否删除成功
*/
@Override
public boolean sRemove(String key, Object... values) {
if (StrUtil.isBlank(key) || ArrayUtil.isEmpty(values)) {
return false;
}
return redissonClient.<Object>getSet(key).removeAll(Arrays.asList(values));
}
/**
* 随机获取集合元素但不删除。
*
* @param key Redis 键
* @return 元素
*/
@Override
public Object sRandomMember(String key) {
if (StrUtil.isBlank(key)) {
return null;
}
return redissonClient.<Object>getSet(key).random();
}
/**
* 随机获取多个集合元素。
*
* @param key Redis 键
* @param count 数量
* @return 元素集合
*/
@Override
public Set<Object> sRandomMembers(String key, int count) {
if (StrUtil.isBlank(key) || count <= 0) {
return Collections.emptySet();
}
return redissonClient.<Object>getSet(key).random(count);
}
/**
* 获取并集。
*
* @param key1 第一个 key
* @param key2 第二个 key
* @return 并集
*/
@Override
public Set<Object> sUnion(String key1, String key2) {
if (StrUtil.hasBlank(key1, key2)) {
return Collections.emptySet();
}
return redissonClient.<Object>getSet(key1).readUnion(key2);
}
/**
* 获取交集。
*
* @param key1 第一个 key
* @param key2 第二个 key
* @return 交集
*/
@Override
public Set<Object> sIntersect(String key1, String key2) {
if (StrUtil.hasBlank(key1, key2)) {
return Collections.emptySet();
}
return redissonClient.<Object>getSet(key1).readIntersection(key2);
}
/**
* 获取差集。
*
* @param key1 第一个 key
* @param key2 第二个 key
* @return 差集
*/
@Override
public Set<Object> sDifference(String key1, String key2) {
if (StrUtil.hasBlank(key1, key2)) {
return Collections.emptySet();
}
return redissonClient.<Object>getSet(key1).readDiff(key2);
}
/**
* 将集合并集存储到目标 key。
*
* @param destKey 目标 key
* @param keys 源 key 集合
* @return 存储数量
*/
@Override
public long sUnionStore(String destKey, String... keys) {
checkKey(destKey);
String[] keyArray = filterKeys(keys);
if (ArrayUtil.isEmpty(keyArray)) {
return 0L;
}
Set<Object> result = redissonClient.<Object>getSet(keyArray[0])
.readUnion(Arrays.copyOfRange(keyArray, 1, keyArray.length));
RSet<Object> destSet = redissonClient.getSet(destKey);
destSet.clear();
if (CollUtil.isNotEmpty(result)) {
destSet.addAll(result);
}
return result.size();
}
/**
* 将集合交集存储到目标 key。
*
* @param destKey 目标 key
* @param keys 源 key 集合
* @return 存储数量
*/
@Override
public long sIntersectStore(String destKey, String... keys) {
checkKey(destKey);
String[] keyArray = filterKeys(keys);
if (ArrayUtil.isEmpty(keyArray)) {
return 0L;
}
Set<Object> result = redissonClient.<Object>getSet(keyArray[0])
.readIntersection(Arrays.copyOfRange(keyArray, 1, keyArray.length));
RSet<Object> destSet = redissonClient.getSet(destKey);
destSet.clear();
if (CollUtil.isNotEmpty(result)) {
destSet.addAll(result);
}
return result.size();
}
/**
* 将集合差集存储到目标 key。
*
* @param destKey 目标 key
* @param keys 源 key 集合
* @return 存储数量
*/
@Override
public long sDifferenceStore(String destKey, String... keys) {
checkKey(destKey);
String[] keyArray = filterKeys(keys);
if (ArrayUtil.isEmpty(keyArray)) {
return 0L;
}
Set<Object> result = redissonClient.<Object>getSet(keyArray[0])
.readDiff(Arrays.copyOfRange(keyArray, 1, keyArray.length));
RSet<Object> destSet = redissonClient.getSet(destKey);
destSet.clear();
if (CollUtil.isNotEmpty(result)) {
destSet.addAll(result);
}
return result.size();
}
// -------------------------------------------------------------------------
// ZSet / 排行榜操作
// -------------------------------------------------------------------------
/**
* 添加有序集合元素。
*
* @param key Redis 键
* @param value 元素
* @param score 分数
* @return 是否新增
*/
@Override
public boolean zAdd(String key, Object value, double score) {
checkKey(key);
return redissonClient.<Object>getScoredSortedSet(key).add(score, value);
}
/**
* 批量添加有序集合元素。
*
* @param key Redis 键
* @param scoreMap 元素分数 Map
* @return 新增数量
*/
@Override
public int zAddAll(String key, Map<Object, Double> scoreMap) {
checkKey(key);
if (CollUtil.isEmpty(scoreMap)) {
return 0;
}
return redissonClient.<Object>getScoredSortedSet(key).addAll(scoreMap);
}
/**
* 删除有序集合元素。
*
* @param key Redis 键
* @param values 元素
* @return 是否删除成功
*/
@Override
public boolean zRemove(String key, Object... values) {
if (StrUtil.isBlank(key) || ArrayUtil.isEmpty(values)) {
return false;
}
return redissonClient.<Object>getScoredSortedSet(key).removeAll(Arrays.asList(values));
}
/**
* 获取元素分数。
*
* @param key Redis 键
* @param value 元素
* @return 分数
*/
@Override
public Double zScore(String key, Object value) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(value)) {
return null;
}
return redissonClient.<Object>getScoredSortedSet(key).getScore(value);
}
/**
* 获取升序排名,从 0 开始。
*
* @param key Redis 键
* @param value 元素
* @return 排名
*/
@Override
public Integer zRank(String key, Object value) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(value)) {
return null;
}
return redissonClient.<Object>getScoredSortedSet(key).rank(value);
}
/**
* 获取降序排名,从 0 开始。
*
* @param key Redis 键
* @param value 元素
* @return 排名
*/
@Override
public Integer zRevRank(String key, Object value) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(value)) {
return null;
}
return redissonClient.<Object>getScoredSortedSet(key).revRank(value);
}
/**
* 获取分数区间元素。
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 元素集合
*/
@Override
public Set<Object> zRangeByScore(String key, double min, double max) {
if (StrUtil.isBlank(key)) {
return Collections.emptySet();
}
Collection<Object> values = redissonClient.<Object>getScoredSortedSet(key)
.valueRange(min, true, max, true);
return new LinkedHashSet<>(values);
}
/**
* 获取分数区间元素并分页。
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @param offset 偏移量
* @param count 数量
* @return 元素集合
*/
@Override
public Collection<Object> zRangeByScore(String key, double min, double max, int offset, int count) {
if (StrUtil.isBlank(key) || offset < 0 || count <= 0) {
return Collections.emptyList();
}
return redissonClient.<Object>getScoredSortedSet(key)
.valueRange(min, true, max, true, offset, count);
}
/**
* 获取分数区间元素及分数。
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 元素分数 Map
*/
@Override
public Map<Object, Double> zRangeByScoreWithScores(String key, double min, double max) {
if (StrUtil.isBlank(key)) {
return Collections.emptyMap();
}
Collection<ScoredEntry<Object>> entries = redissonClient.<Object>getScoredSortedSet(key)
.entryRange(min, true, max, true);
return scoredEntriesToMap(entries);
}
/**
* 获取降序分数区间元素及分数。
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 元素分数 Map
*/
@Override
public Map<Object, Double> zRevRangeByScoreWithScores(String key, double min, double max) {
if (StrUtil.isBlank(key)) {
return Collections.emptyMap();
}
List<ScoredEntry<Object>> entries = new ArrayList<>(
redissonClient.<Object>getScoredSortedSet(key).entryRange(min, true, max, true)
);
Collections.reverse(entries);
return scoredEntriesToMap(entries);
}
/**
* 获取排名区间元素。
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 元素集合
*/
@Override
public Set<Object> zRange(String key, int start, int end) {
if (StrUtil.isBlank(key)) {
return Collections.emptySet();
}
Collection<Object> values = redissonClient.<Object>getScoredSortedSet(key).valueRange(start, end);
return new LinkedHashSet<>(values);
}
/**
* 获取排名区间元素及分数。
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 元素分数 Map
*/
@Override
public Map<Object, Double> zRangeWithScores(String key, int start, int end) {
if (StrUtil.isBlank(key)) {
return Collections.emptyMap();
}
Collection<ScoredEntry<Object>> entries = redissonClient.<Object>getScoredSortedSet(key)
.entryRange(start, end);
return scoredEntriesToMap(entries);
}
/**
* 获取降序排名区间元素。
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 元素集合
*/
@Override
public Set<Object> zRevRange(String key, int start, int end) {
if (StrUtil.isBlank(key)) {
return Collections.emptySet();
}
Collection<Object> values = redissonClient.<Object>getScoredSortedSet(key)
.valueRangeReversed(start, end);
return new LinkedHashSet<>(values);
}
/**
* 获取降序排名区间元素及分数。
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 元素分数 Map
*/
@Override
public Map<Object, Double> zRevRangeWithScores(String key, int start, int end) {
if (StrUtil.isBlank(key)) {
return Collections.emptyMap();
}
Collection<ScoredEntry<Object>> entries = redissonClient.<Object>getScoredSortedSet(key)
.entryRangeReversed(start, end);
return scoredEntriesToMap(entries);
}
/**
* 增加元素分数。
*
* @param key Redis 键
* @param value 元素
* @param delta 分数增量
* @return 最新分数
*/
@Override
public Double zIncrBy(String key, Object value, double delta) {
checkKey(key);
Assert.notNull(value, "有序集合元素不能为空");
return redissonClient.<Object>getScoredSortedSet(key).addScore(value, delta);
}
/**
* 获取有序集合元素数量。
*
* @param key Redis 键
* @return 数量
*/
@Override
public int zCard(String key) {
if (StrUtil.isBlank(key)) {
return 0;
}
return redissonClient.<Object>getScoredSortedSet(key).size();
}
/**
* 获取分数区间元素数量。
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 数量
*/
@Override
public long zCount(String key, double min, double max) {
if (StrUtil.isBlank(key)) {
return 0L;
}
return redissonClient.<Object>getScoredSortedSet(key).count(min, true, max, true);
}
/**
* 删除分数区间元素。
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 删除数量
*/
@Override
public long zRemoveRangeByScore(String key, double min, double max) {
if (StrUtil.isBlank(key)) {
return 0L;
}
return redissonClient.<Object>getScoredSortedSet(key)
.removeRangeByScore(min, true, max, true);
}
/**
* 删除排名区间元素。
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 删除数量
*/
@Override
public long zRemoveRangeByRank(String key, int start, int end) {
if (StrUtil.isBlank(key)) {
return 0L;
}
return redissonClient.<Object>getScoredSortedSet(key).removeRangeByRank(start, end);
}
/**
* 弹出分数最小的元素。
* 注意:当前实现是先查再删,不是严格原子弹出;如需高并发严格原子语义,后续可改为 Lua 或 Redisson 原生弹出 API。
*
* @param key Redis 键
* @return 元素及分数
*/
@Override
public ScoredEntry<Object> zPopFirst(String key) {
if (StrUtil.isBlank(key)) {
return null;
}
RScoredSortedSet<Object> zSet = redissonClient.getScoredSortedSet(key);
Collection<ScoredEntry<Object>> entries = zSet.entryRange(0, 0);
if (CollUtil.isEmpty(entries)) {
return null;
}
ScoredEntry<Object> entry = entries.iterator().next();
zSet.remove(entry.getValue());
return entry;
}
/**
* 弹出分数最大的元素。
* 注意:当前实现是先查再删,不是严格原子弹出;如需高并发严格原子语义,后续可改为 Lua 或 Redisson 原生弹出 API。
*
* @param key Redis 键
* @return 元素及分数
*/
@Override
public ScoredEntry<Object> zPopLast(String key) {
if (StrUtil.isBlank(key)) {
return null;
}
RScoredSortedSet<Object> zSet = redissonClient.getScoredSortedSet(key);
Collection<ScoredEntry<Object>> entries = zSet.entryRangeReversed(0, 0);
if (CollUtil.isEmpty(entries)) {
return null;
}
ScoredEntry<Object> entry = entries.iterator().next();
zSet.remove(entry.getValue());
return entry;
}
// -------------------------------------------------------------------------
// 分布式锁与同步器
// -------------------------------------------------------------------------
/**
* 获取可重入锁。
*
* @param lockKey 锁 key
* @return RLock
*/
@Override
public RLock getLock(String lockKey) {
checkKey(lockKey);
return redissonClient.getLock(lockKey);
}
/**
* 获取公平锁。
*
* @param lockKey 锁 key
* @return RLock
*/
@Override
public RLock getFairLock(String lockKey) {
checkKey(lockKey);
return redissonClient.getFairLock(lockKey);
}
/**
* 获取自旋锁。
*
* @param lockKey 锁 key
* @return RLock
*/
@Override
public RLock getSpinLock(String lockKey) {
checkKey(lockKey);
return redissonClient.getSpinLock(lockKey);
}
/**
* 获取联锁。
*
* @param locks 多个锁
* @return RLock
*/
@Override
public RLock getMultiLock(RLock... locks) {
Assert.isTrue(ArrayUtil.isNotEmpty(locks), "联锁对象不能为空");
for (RLock lock : locks) {
Assert.notNull(lock, "联锁对象不能包含空锁");
}
return redissonClient.getMultiLock(locks);
}
/**
* 阻塞加锁。
*
* @param lockKey 锁 key
*/
@Override
public void lock(String lockKey) {
RLock lock = getLock(lockKey);
lock.lock();
}
/**
* 阻塞加锁并指定自动释放时间。
*
* @param lockKey 锁 key
* @param leaseTime 持有时间
* @param unit 时间单位
*/
@Override
public void lock(String lockKey, long leaseTime, TimeUnit unit) {
RLock lock = getLock(lockKey);
Assert.notNull(unit, "时间单位不能为空");
if (leaseTime <= 0) {
lock.lock();
return;
}
lock.lock(leaseTime, unit);
}
/**
* 尝试获取锁。
*
* @param lockKey 锁 key
* @param waitTime 等待时间
* @param leaseTime 持有时间
* @param unit 时间单位
* @return 是否获取成功
* @throws InterruptedException 线程中断时抛出
*/
@Override
public boolean tryLock(String lockKey, long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {
RLock lock = getLock(lockKey);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(waitTime >= 0, "等待时间不能小于 0");
if (leaseTime <= 0) {
return lock.tryLock(waitTime, unit);
}
return lock.tryLock(waitTime, leaseTime, unit);
}
/**
* 释放锁。
*
* @param lockKey 锁 key
*/
@Override
public void unlock(String lockKey) {
if (StrUtil.isBlank(lockKey)) {
return;
}
RLock lock = redissonClient.getLock(lockKey);
if (!lock.isHeldByCurrentThread()) {
log.warn("当前线程未持有分布式锁,跳过释放,lockKey={}", lockKey);
return;
}
try {
lock.unlock();
} catch (Exception e) {
log.error("释放分布式锁异常,lockKey={}", lockKey, e);
}
}
/**
* 执行带锁任务。
*
* @param lockKey 锁 key
* @param task 任务
*/
@Override
public void executeWithLock(String lockKey, Runnable task) {
Assert.notNull(task, "锁内任务不能为空");
RLock lock = getLock(lockKey);
boolean locked = false;
try {
lock.lock();
locked = true;
task.run();
} finally {
unlockSafely(lockKey, lock, locked);
}
}
/**
* 执行带锁任务并返回结果。
*
* @param lockKey 锁 key
* @param supplier 任务
* @param <T> 返回类型
* @return 任务结果
*/
@Override
public <T> T executeWithLock(String lockKey, Supplier<T> supplier) {
Assert.notNull(supplier, "锁内任务不能为空");
RLock lock = getLock(lockKey);
boolean locked = false;
try {
lock.lock();
locked = true;
return supplier.get();
} finally {
unlockSafely(lockKey, lock, locked);
}
}
/**
* 尝试执行带锁任务。
*
* @param lockKey 锁 key
* @param waitTime 等待时间
* @param leaseTime 持有时间,-1 表示使用 watchdog
* @param unit 时间单位
* @param task 任务
* @return 是否执行成功
*/
@Override
public boolean tryExecuteWithLock(String lockKey, long waitTime, long leaseTime, TimeUnit unit, Runnable task) {
Assert.notNull(task, "锁内任务不能为空");
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(waitTime >= 0, "等待时间不能小于 0");
RLock lock = getLock(lockKey);
boolean locked = false;
try {
locked = leaseTime <= 0
? lock.tryLock(waitTime, unit)
: lock.tryLock(waitTime, leaseTime, unit);
if (!locked) {
log.warn("获取分布式锁失败,lockKey={}", lockKey);
return false;
}
task.run();
return true;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.error("获取分布式锁被中断,lockKey={}", lockKey, e);
return false;
} finally {
unlockSafely(lockKey, lock, locked);
}
}
/**
* 判断当前线程是否持有锁。
*
* @param lockKey 锁 key
* @return 当前线程持有返回 true
*/
@Override
public boolean isHeldByCurrentThread(String lockKey) {
if (StrUtil.isBlank(lockKey)) {
return false;
}
return redissonClient.getLock(lockKey).isHeldByCurrentThread();
}
/**
* 判断锁是否被任意线程持有。
*
* @param lockKey 锁 key
* @return 已加锁返回 true
*/
@Override
public boolean isLocked(String lockKey) {
if (StrUtil.isBlank(lockKey)) {
return false;
}
return redissonClient.getLock(lockKey).isLocked();
}
/**
* 获取读写锁。
*
* @param lockKey 锁 key
* @return RReadWriteLock
*/
@Override
public RReadWriteLock getReadWriteLock(String lockKey) {
checkKey(lockKey);
return redissonClient.getReadWriteLock(lockKey);
}
/**
* 获取读锁。
*
* @param lockKey 锁 key
*/
@Override
public void readLock(String lockKey) {
getReadWriteLock(lockKey).readLock().lock();
}
/**
* 获取写锁。
*
* @param lockKey 锁 key
*/
@Override
public void writeLock(String lockKey) {
getReadWriteLock(lockKey).writeLock().lock();
}
/**
* 尝试获取读锁。
*
* @param lockKey 锁 key
* @param waitTime 等待时间
* @param leaseTime 持有时间
* @param unit 时间单位
* @return 是否成功
* @throws InterruptedException 线程中断时抛出
*/
@Override
public boolean tryReadLock(String lockKey, long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {
RLock lock = getReadWriteLock(lockKey).readLock();
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(waitTime >= 0, "等待时间不能小于 0");
if (leaseTime <= 0) {
return lock.tryLock(waitTime, unit);
}
return lock.tryLock(waitTime, leaseTime, unit);
}
/**
* 尝试获取写锁。
*
* @param lockKey 锁 key
* @param waitTime 等待时间
* @param leaseTime 持有时间
* @param unit 时间单位
* @return 是否成功
* @throws InterruptedException 线程中断时抛出
*/
@Override
public boolean tryWriteLock(String lockKey, long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {
RLock lock = getReadWriteLock(lockKey).writeLock();
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(waitTime >= 0, "等待时间不能小于 0");
if (leaseTime <= 0) {
return lock.tryLock(waitTime, unit);
}
return lock.tryLock(waitTime, leaseTime, unit);
}
/**
* 释放读锁。
*
* @param lockKey 锁 key
*/
@Override
public void unlockRead(String lockKey) {
if (StrUtil.isBlank(lockKey)) {
return;
}
RLock lock = redissonClient.getReadWriteLock(lockKey).readLock();
unlockSafely(lockKey + ":read", lock, true);
}
/**
* 释放写锁。
*
* @param lockKey 锁 key
*/
@Override
public void unlockWrite(String lockKey) {
if (StrUtil.isBlank(lockKey)) {
return;
}
RLock lock = redissonClient.getReadWriteLock(lockKey).writeLock();
unlockSafely(lockKey + ":write", lock, true);
}
/**
* 获取闭锁。
*
* @param latchKey 闭锁 key
* @return RCountDownLatch
*/
@Override
public RCountDownLatch getCountDownLatch(String latchKey) {
checkKey(latchKey);
return redissonClient.getCountDownLatch(latchKey);
}
/**
* 设置闭锁计数。
*
* @param latchKey 闭锁 key
* @param count 计数
*/
@Override
public void setCount(String latchKey, int count) {
checkKey(latchKey);
Assert.isTrue(count > 0, "闭锁计数必须大于 0");
boolean success = redissonClient.getCountDownLatch(latchKey).trySetCount(count);
if (!success) {
log.warn("设置闭锁计数失败,可能闭锁已存在,latchKey={},count={}", latchKey, count);
}
}
/**
* 闭锁计数减一。
*
* @param latchKey 闭锁 key
*/
@Override
public void countDown(String latchKey) {
checkKey(latchKey);
redissonClient.getCountDownLatch(latchKey).countDown();
}
/**
* 等待闭锁完成。
*
* @param latchKey 闭锁 key
* @throws InterruptedException 线程中断时抛出
*/
@Override
public void await(String latchKey) throws InterruptedException {
checkKey(latchKey);
redissonClient.getCountDownLatch(latchKey).await();
}
/**
* 等待闭锁完成。
*
* @param latchKey 闭锁 key
* @param timeout 超时时间
* @param unit 时间单位
* @return 是否完成
* @throws InterruptedException 线程中断时抛出
*/
@Override
public boolean await(String latchKey, long timeout, TimeUnit unit) throws InterruptedException {
checkKey(latchKey);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(timeout >= 0, "等待时间不能小于 0");
return redissonClient.getCountDownLatch(latchKey).await(timeout, unit);
}
/**
* 获取信号量。
*
* @param semaphoreKey 信号量 key
* @return RSemaphore
*/
@Override
public RSemaphore getSemaphore(String semaphoreKey) {
checkKey(semaphoreKey);
return redissonClient.getSemaphore(semaphoreKey);
}
/**
* 初始化信号量许可数量。
*
* @param semaphoreKey 信号量 key
* @param permits 许可数量
*/
@Override
public void trySetPermits(String semaphoreKey, int permits) {
checkKey(semaphoreKey);
Assert.isTrue(permits > 0, "信号量许可数量必须大于 0");
boolean success = redissonClient.getSemaphore(semaphoreKey).trySetPermits(permits);
if (!success) {
log.warn("初始化信号量许可失败,可能信号量已存在,semaphoreKey={},permits={}", semaphoreKey, permits);
}
}
/**
* 获取一个信号量许可。
*
* @param semaphoreKey 信号量 key
* @throws InterruptedException 线程中断时抛出
*/
@Override
public void acquire(String semaphoreKey) throws InterruptedException {
checkKey(semaphoreKey);
redissonClient.getSemaphore(semaphoreKey).acquire();
}
/**
* 尝试获取信号量许可。
*
* @param semaphoreKey 信号量 key
* @param permits 许可数量
* @param waitTime 等待时间
* @param unit 时间单位
* @return 是否获取成功
* @throws InterruptedException 线程中断时抛出
*/
@Override
public boolean tryAcquire(String semaphoreKey, int permits, long waitTime, TimeUnit unit) throws InterruptedException {
checkKey(semaphoreKey);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(permits > 0, "信号量许可数量必须大于 0");
Assert.isTrue(waitTime >= 0, "等待时间不能小于 0");
return redissonClient.getSemaphore(semaphoreKey).tryAcquire(permits, waitTime, unit);
}
/**
* 释放信号量许可。
*
* @param semaphoreKey 信号量 key
*/
@Override
public void release(String semaphoreKey) {
checkKey(semaphoreKey);
redissonClient.getSemaphore(semaphoreKey).release();
}
/**
* 获取可用许可数量。
*
* @param semaphoreKey 信号量 key
* @return 可用许可数量
*/
@Override
public int availablePermits(String semaphoreKey) {
checkKey(semaphoreKey);
return redissonClient.getSemaphore(semaphoreKey).availablePermits();
}
/**
* 获取可过期信号量。
*
* @param semaphoreKey 信号量 key
* @return RPermitExpirableSemaphore
*/
@Override
public RPermitExpirableSemaphore getPermitExpirableSemaphore(String semaphoreKey) {
checkKey(semaphoreKey);
return redissonClient.getPermitExpirableSemaphore(semaphoreKey);
}
// -------------------------------------------------------------------------
// 限流器
// -------------------------------------------------------------------------
/**
* 初始化限流器。
*
* @param key 限流器 key
* @param rateType 限流类型
* @param rate 令牌数
* @param interval 间隔
* @param unit 间隔单位
* @return 是否初始化成功
*/
@Override
public boolean rateLimiterInit(String key, RateType rateType, long rate, long interval, RateIntervalUnit unit) {
checkKey(key);
Assert.notNull(rateType, "限流类型不能为空");
Assert.notNull(unit, "限流时间单位不能为空");
Assert.isTrue(rate > 0, "令牌数必须大于 0");
Assert.isTrue(interval > 0, "限流间隔必须大于 0");
boolean initialized = redissonClient.getRateLimiter(key)
.trySetRate(rateType, rate, interval, unit);
if (initialized) {
log.info("Redis 限流器初始化成功,key={},rate={},interval={},unit={}", key, rate, interval, unit);
} else {
log.debug("Redis 限流器已存在,跳过初始化,key={}", key);
}
return initialized;
}
/**
* 更新限流器速率。
*
* @param key 限流器 key
* @param rateType 限流类型
* @param rate 令牌数
* @param interval 间隔
* @param unit 间隔单位
*/
@Override
public void rateLimiterSetRate(String key, RateType rateType, long rate, long interval, RateIntervalUnit unit) {
checkKey(key);
Assert.notNull(rateType, "限流类型不能为空");
Assert.notNull(unit, "限流时间单位不能为空");
Assert.isTrue(rate > 0, "令牌数必须大于 0");
Assert.isTrue(interval > 0, "限流间隔必须大于 0");
redissonClient.getRateLimiter(key).setRate(rateType, rate, interval, unit);
log.info("Redis 限流器速率已更新,key={},rate={},interval={},unit={}", key, rate, interval, unit);
}
/**
* 尝试获取一个令牌。
*
* @param key 限流器 key
* @return 是否获取成功
*/
@Override
public boolean rateLimiterTryAcquire(String key) {
checkKey(key);
return redissonClient.getRateLimiter(key).tryAcquire();
}
/**
* 尝试获取指定数量令牌。
*
* @param key 限流器 key
* @param permits 令牌数量
* @return 是否获取成功
*/
@Override
public boolean rateLimiterTryAcquire(String key, long permits) {
checkKey(key);
Assert.isTrue(permits > 0, "令牌数量必须大于 0");
return redissonClient.getRateLimiter(key).tryAcquire(permits);
}
/**
* 阻塞获取指定数量令牌。
*
* @param key 限流器 key
* @param permits 令牌数量
*/
@Override
public void rateLimiterAcquire(String key, long permits) {
checkKey(key);
Assert.isTrue(permits > 0, "令牌数量必须大于 0");
redissonClient.getRateLimiter(key).acquire(permits);
}
/**
* 尝试等待获取令牌。
*
* @param key 限流器 key
* @param timeout 等待时间
* @param unit 时间单位
* @return 是否获取成功
*/
@Override
public boolean rateLimiterTryAcquire(String key, long timeout, TimeUnit unit) {
checkKey(key);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(timeout >= 0, "等待时间不能小于 0");
return redissonClient.getRateLimiter(key).tryAcquire(timeout, unit);
}
/**
* 获取限流器对象。
*
* @param key 限流器 key
* @return RRateLimiter
*/
@Override
public RRateLimiter rateLimiterGet(String key) {
checkKey(key);
return redissonClient.getRateLimiter(key);
}
/**
* 删除限流器。
*
* @param key 限流器 key
* @return 是否删除成功
*/
@Override
public boolean rateLimiterDelete(String key) {
if (StrUtil.isBlank(key)) {
return false;
}
return redissonClient.getRateLimiter(key).delete();
}
// -------------------------------------------------------------------------
// 布隆过滤器
// -------------------------------------------------------------------------
/**
* 获取布隆过滤器。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RBloomFilter
*/
@Override
public <T> RBloomFilter<T> getBloomFilter(String key) {
checkKey(key);
return redissonClient.getBloomFilter(key);
}
/**
* 初始化布隆过滤器。
*
* @param key Redis 键
* @param expectedInsertions 预计插入量
* @param falseProbability 误判率
*/
@Override
public void bloomInit(String key, long expectedInsertions, double falseProbability) {
checkKey(key);
Assert.isTrue(expectedInsertions > 0, "预计插入量必须大于 0");
Assert.isTrue(falseProbability > 0 && falseProbability < 1, "误判率必须在 0 到 1 之间");
boolean initialized = redissonClient.getBloomFilter(key)
.tryInit(expectedInsertions, falseProbability);
if (initialized) {
log.info("Redis 布隆过滤器初始化成功,key={},expectedInsertions={},falseProbability={}",
key, expectedInsertions, falseProbability);
} else {
log.debug("Redis 布隆过滤器已存在,跳过初始化,key={}", key);
}
}
/**
* 判断布隆过滤器是否可能包含元素。
*
* @param key Redis 键
* @param value 元素
* @return 可能包含返回 true
*/
@Override
public boolean bloomContains(String key, Object value) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(value)) {
return false;
}
return redissonClient.getBloomFilter(key).contains(value);
}
/**
* 添加布隆过滤器元素。
*
* @param key Redis 键
* @param value 元素
* @return 是否新增
*/
@Override
public boolean bloomAdd(String key, Object value) {
checkKey(key);
Assert.notNull(value, "布隆过滤器元素不能为空");
return redissonClient.getBloomFilter(key).add(value);
}
/**
* 批量添加布隆过滤器元素。
*
* @param key Redis 键
* @param values 元素集合
* @return 新增数量
*/
@Override
public long bloomAddAll(String key, Collection<?> values) {
checkKey(key);
if (CollUtil.isEmpty(values)) {
return 0L;
}
RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(key);
long added = 0L;
for (Object value : values) {
if (ObjectUtil.isNotNull(value) && bloomFilter.add(value)) {
added++;
}
}
return added;
}
/**
* 删除布隆过滤器。
*
* @param key Redis 键
* @return 是否删除成功
*/
@Override
public boolean bloomDelete(String key) {
if (StrUtil.isBlank(key)) {
return false;
}
return redissonClient.getBloomFilter(key).delete();
}
/**
* 判断布隆过滤器是否存在。
*
* @param key Redis 键
* @return 存在返回 true
*/
@Override
public boolean bloomExists(String key) {
if (StrUtil.isBlank(key)) {
return false;
}
return redissonClient.getBloomFilter(key).isExists();
}
/**
* 获取预计插入量。
*
* @param key Redis 键
* @return 预计插入量
*/
@Override
public long bloomGetExpectedInsertions(String key) {
if (StrUtil.isBlank(key)) {
return 0L;
}
RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(key);
if (!bloomFilter.isExists()) {
return 0L;
}
return bloomFilter.getExpectedInsertions();
}
/**
* 获取误判率。
*
* @param key Redis 键
* @return 误判率
*/
@Override
public double bloomGetFalseProbability(String key) {
if (StrUtil.isBlank(key)) {
return 0D;
}
RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(key);
if (!bloomFilter.isExists()) {
return 0D;
}
return bloomFilter.getFalseProbability();
}
// -------------------------------------------------------------------------
// BitSet / 签到 / 位图统计
// -------------------------------------------------------------------------
/**
* 设置位图指定位置。
*
* @param key Redis 键
* @param index 位索引
* @param value 位值
*/
@Override
public void bitSet(String key, long index, boolean value) {
checkKey(key);
Assert.isTrue(index >= 0, "位图索引不能小于 0");
redissonClient.getBitSet(key).set(index, value);
}
/**
* 获取位图指定位置。
*
* @param key Redis 键
* @param index 位索引
* @return 位值
*/
@Override
public boolean bitGet(String key, long index) {
if (StrUtil.isBlank(key) || index < 0) {
return false;
}
return redissonClient.getBitSet(key).get(index);
}
/**
* 获取位图中 true 的数量。
*
* @param key Redis 键
* @return 数量
*/
@Override
public long bitCount(String key) {
if (StrUtil.isBlank(key)) {
return 0L;
}
return redissonClient.getBitSet(key).cardinality();
}
/**
* 清空位图。
*
* @param key Redis 键
*/
@Override
public void bitClear(String key) {
if (StrUtil.isBlank(key)) {
return;
}
redissonClient.getBitSet(key).clear();
}
/**
* 用户指定日期签到。
*
* @param keyPrefix 业务 key 前缀
* @param userId 用户 ID
* @param date 日期
*/
@Override
public void sign(String keyPrefix, Object userId, LocalDate date) {
checkKey(keyPrefix);
Assert.notNull(userId, "用户 ID 不能为空");
Assert.notNull(date, "签到日期不能为空");
String key = buildSignKey(keyPrefix, userId, date.getYear());
int bitIndex = date.getDayOfYear() - 1;
redissonClient.getBitSet(key).set(bitIndex, true);
log.info("用户签到成功,key={},userId={},date={}", key, userId, date);
}
/**
* 判断用户指定日期是否签到。
*
* @param keyPrefix 业务 key 前缀
* @param userId 用户 ID
* @param date 日期
* @return 已签到返回 true
*/
@Override
public boolean isSigned(String keyPrefix, Object userId, LocalDate date) {
if (StrUtil.isBlank(keyPrefix) || ObjectUtil.isNull(userId) || ObjectUtil.isNull(date)) {
return false;
}
String key = buildSignKey(keyPrefix, userId, date.getYear());
int bitIndex = date.getDayOfYear() - 1;
return redissonClient.getBitSet(key).get(bitIndex);
}
/**
* 获取用户指定年份签到天数。
*
* @param keyPrefix 业务 key 前缀
* @param userId 用户 ID
* @param year 年份
* @return 签到天数
*/
@Override
public long getSignCount(String keyPrefix, Object userId, int year) {
if (StrUtil.isBlank(keyPrefix) || ObjectUtil.isNull(userId) || year <= 0) {
return 0L;
}
String key = buildSignKey(keyPrefix, userId, year);
return redissonClient.getBitSet(key).cardinality();
}
/**
* 获取从指定日期向前连续签到天数。
*
* @param keyPrefix 业务 key 前缀
* @param userId 用户 ID
* @param date 日期
* @return 连续签到天数
*/
@Override
public int getContinuousSignCount(String keyPrefix, Object userId, LocalDate date) {
if (StrUtil.isBlank(keyPrefix) || ObjectUtil.isNull(userId) || ObjectUtil.isNull(date)) {
return 0;
}
String key = buildSignKey(keyPrefix, userId, date.getYear());
RBitSet bitSet = redissonClient.getBitSet(key);
int count = 0;
int bitIndex = date.getDayOfYear() - 1;
for (int i = bitIndex; i >= 0; i--) {
if (bitSet.get(i)) {
count++;
} else {
break;
}
}
return count;
}
// -------------------------------------------------------------------------
// HyperLogLog / UV 统计
// -------------------------------------------------------------------------
/**
* 添加 HyperLogLog 元素。
*
* @param key Redis 键
* @param value 元素
* @return 是否改变基数估算
*/
@Override
public boolean hllAdd(String key, Object value) {
checkKey(key);
Assert.notNull(value, "HyperLogLog 元素不能为空");
return redissonClient.getHyperLogLog(key).add(value);
}
/**
* 批量添加 HyperLogLog 元素。
*
* @param key Redis 键
* @param values 元素集合
* @return 是否改变基数估算
*/
/**
* 批量添加 HyperLogLog 元素。
*
* @param key Redis 键
* @param values 元素集合
* @return 是否改变基数估算
*/
@Override
public boolean hllAddAll(String key, Collection<?> values) {
checkKey(key);
if (CollUtil.isEmpty(values)) {
return false;
}
Collection<Object> valueList = new ArrayList<>(values.size());
for (Object value : values) {
if (ObjectUtil.isNotNull(value)) {
valueList.add(value);
}
}
if (CollUtil.isEmpty(valueList)) {
return false;
}
return redissonClient.<Object>getHyperLogLog(key).addAll(valueList);
}
/**
* 获取 HyperLogLog 基数估算。
*
* @param key Redis 键
* @return 基数估算
*/
@Override
public long hllCount(String key) {
if (StrUtil.isBlank(key)) {
return 0L;
}
return redissonClient.getHyperLogLog(key).count();
}
/**
* 合并 HyperLogLog。
*
* @param destKey 目标 key
* @param keys 源 key
* @return 合并后基数估算
*/
@Override
public long hllMerge(String destKey, String... keys) {
checkKey(destKey);
String[] keyArray = filterKeys(keys);
if (ArrayUtil.isEmpty(keyArray)) {
return hllCount(destKey);
}
RHyperLogLog<Object> dest = redissonClient.getHyperLogLog(destKey);
dest.mergeWith(keyArray);
return dest.count();
}
/**
* 按日期记录 UV。
*
* @param keyPrefix 业务 key 前缀
* @param bizKey 业务标识
* @param userFlag 用户唯一标识
* @param date 日期
* @return 是否改变基数估算
*/
@Override
public boolean uvRecord(String keyPrefix, String bizKey, Object userFlag, LocalDate date) {
checkKey(keyPrefix);
checkKey(bizKey);
Assert.notNull(userFlag, "用户唯一标识不能为空");
Assert.notNull(date, "UV 日期不能为空");
String key = buildUvKey(keyPrefix, bizKey, date);
return hllAdd(key, userFlag);
}
/**
* 获取指定日期 UV。
*
* @param keyPrefix 业务 key 前缀
* @param bizKey 业务标识
* @param date 日期
* @return UV 数量
*/
@Override
public long uvCount(String keyPrefix, String bizKey, LocalDate date) {
if (StrUtil.hasBlank(keyPrefix, bizKey) || ObjectUtil.isNull(date)) {
return 0L;
}
String key = buildUvKey(keyPrefix, bizKey, date);
return hllCount(key);
}
// -------------------------------------------------------------------------
// Geo / LBS 地理位置
// -------------------------------------------------------------------------
/**
* 添加地理位置。
*
* @param key Redis 键
* @param longitude 经度
* @param latitude 纬度
* @param member 成员
* @return 添加数量
*/
@Override
public long geoAdd(String key, double longitude, double latitude, Object member) {
checkKey(key);
checkGeoCoordinate(longitude, latitude);
Assert.notNull(member, "Geo 成员不能为空");
return redissonClient.<Object>getGeo(key).add(longitude, latitude, member);
}
/**
* 批量添加地理位置。
*
* @param key Redis 键
* @param entries 坐标集合
* @return 添加数量
*/
@Override
public long geoAdd(String key, GeoEntry... entries) {
checkKey(key);
if (ArrayUtil.isEmpty(entries)) {
return 0L;
}
return redissonClient.getGeo(key).add(entries);
}
/**
* 仅成员不存在时添加地理位置。
*
* @param key Redis 键
* @param longitude 经度
* @param latitude 纬度
* @param member 成员
* @return 是否添加成功
*/
@Override
public boolean geoTryAdd(String key, double longitude, double latitude, Object member) {
checkKey(key);
checkGeoCoordinate(longitude, latitude);
Assert.notNull(member, "Geo 成员不能为空");
return redissonClient.<Object>getGeo(key).tryAdd(longitude, latitude, member);
}
/**
* 计算两个成员距离。
*
* @param key Redis 键
* @param member1 成员 1
* @param member2 成员 2
* @param unit 单位
* @return 距离
*/
@Override
public Double geoDistance(String key, Object member1, Object member2, GeoUnit unit) {
if (StrUtil.isBlank(key) || ObjectUtil.hasEmpty(member1, member2, unit)) {
return null;
}
return redissonClient.<Object>getGeo(key).dist(member1, member2, unit);
}
/**
* 查询成员 GeoHash。
*
* @param key Redis 键
* @param members 成员
* @return GeoHash Map
*/
@Override
public Map<Object, String> geoHash(String key, Object... members) {
if (StrUtil.isBlank(key) || ArrayUtil.isEmpty(members)) {
return Collections.emptyMap();
}
return redissonClient.<Object>getGeo(key).hash(members);
}
/**
* 查询成员坐标。
*
* @param key Redis 键
* @param members 成员
* @return 坐标 Map
*/
@Override
public Map<Object, GeoPosition> geoPosition(String key, Object... members) {
if (StrUtil.isBlank(key) || ArrayUtil.isEmpty(members)) {
return Collections.emptyMap();
}
return redissonClient.<Object>getGeo(key).pos(members);
}
/**
* 根据条件搜索地理位置成员。
*
* @param key Redis 键
* @param args 搜索参数
* @return 成员列表
*/
@Override
public List<Object> geoSearch(String key, GeoSearchArgs args) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(args)) {
return Collections.emptyList();
}
return redissonClient.<Object>getGeo(key).search(args);
}
/**
* 根据条件搜索地理位置成员和距离。
*
* @param key Redis 键
* @param args 搜索参数
* @return 成员距离 Map
*/
@Override
public Map<Object, Double> geoSearchWithDistance(String key, GeoSearchArgs args) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(args)) {
return Collections.emptyMap();
}
return redissonClient.<Object>getGeo(key).searchWithDistance(args);
}
/**
* 根据条件搜索地理位置成员和坐标。
*
* @param key Redis 键
* @param args 搜索参数
* @return 成员坐标 Map
*/
@Override
public Map<Object, GeoPosition> geoSearchWithPosition(String key, GeoSearchArgs args) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(args)) {
return Collections.emptyMap();
}
return redissonClient.<Object>getGeo(key).searchWithPosition(args);
}
/**
* 删除地理位置成员。
*
* @param key Redis 键
* @param members 成员
* @return 删除数量
*/
@Override
public long geoRemove(String key, Object... members) {
if (StrUtil.isBlank(key) || ArrayUtil.isEmpty(members)) {
return 0L;
}
return redissonClient.<Object>getGeo(key).removeAll(Arrays.asList(members)) ? members.length : 0L;
}
// -------------------------------------------------------------------------
// 分布式会话
// -------------------------------------------------------------------------
/**
* 创建或覆盖会话。
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @param session 会话对象
* @param ttl 过期时间
*/
@Override
public void sessionSet(String keyPrefix, String token, Object session, Duration ttl) {
checkKey(keyPrefix);
checkKey(token);
Assert.notNull(session, "会话对象不能为空");
checkPositiveDuration(ttl, "会话过期时间不能为空且必须大于 0");
String key = buildSessionKey(keyPrefix, token);
redissonClient.getBucket(key).set(session, ttl.toMillis(), TimeUnit.MILLISECONDS);
log.info("Redis 会话写入成功,key={}", key);
}
/**
* 获取会话。
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 会话对象
*/
@Override
public <T> T sessionGet(String keyPrefix, String token, Class<T> clazz) {
if (StrUtil.hasBlank(keyPrefix, token) || ObjectUtil.isNull(clazz)) {
return null;
}
String key = buildSessionKey(keyPrefix, token);
Object value = redissonClient.getBucket(key).get();
return convertValue(value, clazz);
}
/**
* 获取会话。
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @param typeReference 目标类型
* @param <T> 泛型类型
* @return 会话对象
*/
@Override
public <T> T sessionGet(String keyPrefix, String token, TypeReference<T> typeReference) {
if (StrUtil.hasBlank(keyPrefix, token) || ObjectUtil.isNull(typeReference)) {
return null;
}
String key = buildSessionKey(keyPrefix, token);
Object value = redissonClient.getBucket(key).get();
return convertValue(value, typeReference);
}
/**
* 刷新会话过期时间。
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @param ttl 过期时间
* @return 是否刷新成功
*/
@Override
public boolean sessionRefresh(String keyPrefix, String token, Duration ttl) {
if (StrUtil.hasBlank(keyPrefix, token)) {
return false;
}
checkPositiveDuration(ttl, "会话过期时间不能为空且必须大于 0");
String key = buildSessionKey(keyPrefix, token);
boolean success = redissonClient.getBucket(key).expire(ttl);
if (success) {
log.info("Redis 会话续期成功,key={}", key);
} else {
log.warn("Redis 会话续期失败,可能会话不存在,key={}", key);
}
return success;
}
/**
* 删除会话。
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @return 是否删除成功
*/
@Override
public boolean sessionDelete(String keyPrefix, String token) {
if (StrUtil.hasBlank(keyPrefix, token)) {
return false;
}
String key = buildSessionKey(keyPrefix, token);
boolean deleted = redissonClient.getBucket(key).delete();
if (deleted) {
log.info("Redis 会话删除成功,key={}", key);
}
return deleted;
}
// -------------------------------------------------------------------------
// Queue / BlockingQueue / DelayedQueue / ReliableQueue
// -------------------------------------------------------------------------
/**
* 入普通队列。
*
* @param queueKey 队列 key
* @param value 元素
* @param <T> 元素类型
* @return 是否入队成功
*/
@Override
public <T> boolean enqueue(String queueKey, T value) {
checkKey(queueKey);
return redissonClient.<T>getQueue(queueKey).offer(value);
}
/**
* 出普通队列。
*
* @param queueKey 队列 key
* @param <T> 元素类型
* @return 元素
*/
@Override
public <T> T dequeue(String queueKey) {
if (StrUtil.isBlank(queueKey)) {
return null;
}
return redissonClient.<T>getQueue(queueKey).poll();
}
/**
* 阻塞入队。
*
* @param queueKey 队列 key
* @param value 元素
* @param <T> 元素类型
* @throws InterruptedException 线程中断时抛出
*/
@Override
public <T> void enqueueBlocking(String queueKey, T value) throws InterruptedException {
checkKey(queueKey);
redissonClient.<T>getBlockingQueue(queueKey).put(value);
}
/**
* 超时阻塞入队。
*
* @param queueKey 队列 key
* @param value 元素
* @param timeout 超时时间
* @param unit 时间单位
* @param <T> 元素类型
* @return 是否入队成功
* @throws InterruptedException 线程中断时抛出
*/
@Override
public <T> boolean enqueueBlocking(String queueKey, T value, long timeout, TimeUnit unit) throws InterruptedException {
checkKey(queueKey);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(timeout >= 0, "等待时间不能小于 0");
return redissonClient.<T>getBlockingQueue(queueKey).offer(value, timeout, unit);
}
/**
* 超时阻塞出队。
*
* @param queueKey 队列 key
* @param timeout 超时时间
* @param unit 时间单位
* @param <T> 元素类型
* @return 元素
* @throws InterruptedException 线程中断时抛出
*/
@Override
public <T> T dequeueBlocking(String queueKey, long timeout, TimeUnit unit) throws InterruptedException {
checkKey(queueKey);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(timeout >= 0, "等待时间不能小于 0");
return redissonClient.<T>getBlockingQueue(queueKey).poll(timeout, unit);
}
/**
* 获取队列长度。
*
* @param queueKey 队列 key
* @return 长度
*/
@Override
public long queueSize(String queueKey) {
if (StrUtil.isBlank(queueKey)) {
return 0L;
}
return redissonClient.getQueue(queueKey).size();
}
/**
* 添加延迟队列任务。
*
* @param queueKey 队列 key
* @param value 元素
* @param delay 延迟时间
* @param unit 时间单位
* @param <T> 元素类型
*/
@Override
public <T> void enqueueDelayed(String queueKey, T value, long delay, TimeUnit unit) {
checkKey(queueKey);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(delay >= 0, "延迟时间不能小于 0");
RBlockingQueue<T> blockingQueue = redissonClient.getBlockingQueue(queueKey);
RDelayedQueue<T> delayedQueue = redissonClient.getDelayedQueue(blockingQueue);
delayedQueue.offer(value, delay, unit);
log.info("Redis 延迟任务入队成功,queueKey={},delay={},unit={}", queueKey, delay, unit);
}
/**
* 获取延迟队列对象。
*
* @param queueKey 队列 key
* @param <T> 元素类型
* @return RDelayedQueue
*/
@Override
public <T> RDelayedQueue<T> getDelayedQueue(String queueKey) {
checkKey(queueKey);
RBlockingQueue<T> blockingQueue = redissonClient.getBlockingQueue(queueKey);
return redissonClient.getDelayedQueue(blockingQueue);
}
/**
* 清空队列。
*
* @param queueKey 队列 key
*/
@Override
public void clearQueue(String queueKey) {
if (StrUtil.isBlank(queueKey)) {
return;
}
redissonClient.getQueue(queueKey).clear();
}
/**
* 判断队列是否为空。
*
* @param queueKey 队列 key
* @return 为空返回 true
*/
@Override
public boolean isQueueEmpty(String queueKey) {
if (StrUtil.isBlank(queueKey)) {
return true;
}
return redissonClient.getQueue(queueKey).isEmpty();
}
/**
* 删除队列元素。
*
* @param queueKey 队列 key
* @param value 元素
* @return 是否删除成功
*/
@Override
public boolean removeFromQueue(String queueKey, Object value) {
if (StrUtil.isBlank(queueKey)) {
return false;
}
return redissonClient.getQueue(queueKey).remove(value);
}
/**
* 获取环形缓冲队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RRingBuffer
*/
@Override
public <T> RRingBuffer<T> getRingBuffer(String key) {
checkKey(key);
return redissonClient.getRingBuffer(key);
}
/**
* 获取优先级队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RPriorityQueue
*/
@Override
public <T> RPriorityQueue<T> getPriorityQueue(String key) {
checkKey(key);
return redissonClient.getPriorityQueue(key);
}
/**
* 获取阻塞双端队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RBlockingDeque
*/
@Override
public <T> RBlockingDeque<T> getBlockingDeque(String key) {
checkKey(key);
return redissonClient.getBlockingDeque(key);
}
// -------------------------------------------------------------------------
// 发布订阅 / Topic
// -------------------------------------------------------------------------
/**
* 发布消息。
*
* @param channel 频道
* @param message 消息
* @return 接收客户端数量
*/
@Override
public long publish(String channel, Object message) {
checkKey(channel);
return redissonClient.getTopic(channel).publish(message);
}
/**
* 订阅频道。
*
* @param channel 频道
* @param messageConsumer 消费回调
* @return 监听器 ID
*/
@Override
public int subscribe(String channel, Consumer<Object> messageConsumer) {
checkKey(channel);
Assert.notNull(messageConsumer, "消息消费回调不能为空");
MessageListener<Object> listener = (topic, message) -> {
try {
messageConsumer.accept(message);
} catch (Exception e) {
log.error("Redis 订阅消息消费异常,channel={},message={}", channel, message, e);
}
};
int listenerId = redissonClient.getTopic(channel).addListener(Object.class, listener);
log.info("Redis 频道订阅成功,channel={},listenerId={}", channel, listenerId);
return listenerId;
}
/**
* 取消订阅频道。
*
* @param channel 频道
* @param listenerId 监听器 ID
*/
@Override
public void unsubscribe(String channel, int listenerId) {
if (StrUtil.isBlank(channel)) {
return;
}
redissonClient.getTopic(channel).removeListener(listenerId);
log.info("Redis 频道取消订阅成功,channel={},listenerId={}", channel, listenerId);
}
/**
* 取消订阅频道全部监听器。
*
* @param channel 频道
*/
@Override
public void unsubscribe(String channel) {
if (StrUtil.isBlank(channel)) {
return;
}
redissonClient.getTopic(channel).removeAllListeners();
log.info("Redis 频道全部监听器已移除,channel={}", channel);
}
/**
* 获取模式主题。
*
* @param pattern 频道通配符
* @return RPatternTopic
*/
@Override
public RPatternTopic getPatternTopic(String pattern) {
checkKey(pattern);
return redissonClient.getPatternTopic(pattern);
}
/**
* 获取可靠主题。
*
* @param topic 主题名
* @return RReliableTopic
*/
@Override
public RReliableTopic getReliableTopic(String topic) {
checkKey(topic);
return redissonClient.getReliableTopic(topic);
}
// -------------------------------------------------------------------------
// Stream 消息流
// -------------------------------------------------------------------------
/**
* 添加 Stream 消息。
*
* @param streamKey Stream key
* @param entries 消息字段
* @return 消息 ID
*/
@Override
public StreamMessageId streamAdd(String streamKey, Map<Object, Object> entries) {
checkKey(streamKey);
Assert.isTrue(CollUtil.isNotEmpty(entries), "Stream 消息字段不能为空");
return redissonClient.<Object, Object>getStream(streamKey)
.add(StreamAddArgs.entries(entries));
}
/**
* 添加 Stream 消息。
*
* @param streamKey Stream key
* @param args 添加参数
* @param <K> 字段类型
* @param <V> 值类型
* @return 消息 ID
*/
@Override
public <K, V> StreamMessageId streamAdd(String streamKey, StreamAddArgs<K, V> args) {
checkKey(streamKey);
Assert.notNull(args, "Stream 添加参数不能为空");
return redissonClient.<K, V>getStream(streamKey).add(args);
}
/**
* 读取 Stream 消息。
*
* @param streamKey Stream key
* @param args 读取参数
* @return 消息 Map
*/
@Override
public Map<StreamMessageId, Map<Object, Object>> streamRead(String streamKey, StreamReadArgs args) {
checkKey(streamKey);
Assert.notNull(args, "Stream 读取参数不能为空");
return redissonClient.<Object, Object>getStream(streamKey).read(args);
}
/**
* 创建消费组。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param id 起始消息 ID
*/
@Override
public void streamCreateGroup(String streamKey, String groupName, StreamMessageId id) {
checkKey(streamKey);
checkKey(groupName);
Assert.notNull(id, "Stream 起始消息 ID 不能为空");
try {
StreamCreateGroupArgs args = StreamCreateGroupArgs.name(groupName)
.id(id)
.makeStream();
redissonClient.<Object, Object>getStream(streamKey).createGroup(args);
log.info("Redis Stream 消费组创建成功,streamKey={},groupName={},id={}", streamKey, groupName, id);
} catch (Exception e) {
log.warn("Redis Stream 消费组创建失败,可能消费组已存在,streamKey={},groupName={}", streamKey, groupName, e);
}
}
/**
* 读取消费组消息。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 消费者
* @param args 读取参数
* @return 消息 Map
*/
@Override
public Map<StreamMessageId, Map<Object, Object>> streamReadGroup(
String streamKey,
String groupName,
String consumerName,
StreamReadGroupArgs args
) {
checkKey(streamKey);
checkKey(groupName);
checkKey(consumerName);
Assert.notNull(args, "Stream 消费组读取参数不能为空");
return redissonClient.<Object, Object>getStream(streamKey)
.readGroup(groupName, consumerName, args);
}
/**
* 确认 Stream 消息。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param ids 消息 ID
* @return 确认数量
*/
@Override
public long streamAck(String streamKey, String groupName, StreamMessageId... ids) {
checkKey(streamKey);
checkKey(groupName);
if (ArrayUtil.isEmpty(ids)) {
return 0L;
}
return redissonClient.<Object, Object>getStream(streamKey).ack(groupName, ids);
}
/**
* 删除 Stream 消息。
*
* @param streamKey Stream key
* @param ids 消息 ID
* @return 删除数量
*/
@Override
public long streamRemove(String streamKey, StreamMessageId... ids) {
checkKey(streamKey);
if (ArrayUtil.isEmpty(ids)) {
return 0L;
}
return redissonClient.<Object, Object>getStream(streamKey).remove(ids);
}
/**
* 获取 Stream 长度。
*
* @param streamKey Stream key
* @return 长度
*/
@Override
public long streamSize(String streamKey) {
if (StrUtil.isBlank(streamKey)) {
return 0L;
}
return redissonClient.getStream(streamKey).size();
}
// -------------------------------------------------------------------------
// Lua / 脚本 / 批处理 / 事务
// -------------------------------------------------------------------------
/**
* 执行 Lua 脚本。
*
* @param script Lua 脚本
* @param mode 执行模式
* @param returnType 返回类型
* @param keys KEYS
* @param values ARGV
* @param <T> 返回泛型
* @return 执行结果
*/
@Override
public <T> T eval(String script, RScript.Mode mode, RScript.ReturnType returnType, List<Object> keys, Object... values) {
Assert.isTrue(StrUtil.isNotBlank(script), "Lua 脚本不能为空");
Assert.notNull(mode, "Lua 执行模式不能为空");
Assert.notNull(returnType, "Lua 返回类型不能为空");
List<Object> safeKeys = safeScriptKeys(keys);
return redissonClient.getScript().eval(mode, script, returnType, safeKeys, values);
}
/**
* 执行 Lua 脚本并返回指定类型。
*
* @param script Lua 脚本
* @param returnType 目标类型
* @param keys KEYS
* @param args ARGV
* @param <T> 返回泛型
* @return 执行结果
*/
@Override
public <T> T eval(String script, Class<T> returnType, List<Object> keys, Object... args) {
Assert.isTrue(StrUtil.isNotBlank(script), "Lua 脚本不能为空");
Assert.notNull(returnType, "Lua 返回类型不能为空");
Object result = redissonClient.getScript().eval(
RScript.Mode.READ_WRITE,
script,
RScript.ReturnType.VALUE,
safeScriptKeys(keys),
args
);
return convertValue(result, returnType);
}
/**
* 执行 Lua 脚本但不关心结果。
*
* @param script Lua 脚本
* @param keys KEYS
* @param args ARGV
*/
@Override
public void evalNoResult(String script, List<Object> keys, Object... args) {
Assert.isTrue(StrUtil.isNotBlank(script), "Lua 脚本不能为空");
redissonClient.getScript().eval(
RScript.Mode.READ_WRITE,
script,
RScript.ReturnType.VALUE,
safeScriptKeys(keys),
args
);
}
/**
* 根据 SHA1 执行 Lua 脚本。
*
* @param sha1 脚本 SHA1
* @param returnType 目标类型
* @param keys KEYS
* @param values ARGV
* @param <T> 返回泛型
* @return 执行结果
*/
@Override
public <T> T evalBySha(String sha1, Class<T> returnType, List<Object> keys, Object... values) {
Assert.isTrue(StrUtil.isNotBlank(sha1), "Lua 脚本 SHA1 不能为空");
Assert.notNull(returnType, "Lua 返回类型不能为空");
Object result = redissonClient.getScript().evalSha(
RScript.Mode.READ_WRITE,
sha1,
RScript.ReturnType.VALUE,
safeScriptKeys(keys),
values
);
return convertValue(result, returnType);
}
/**
* 加载 Lua 脚本。
*
* @param script Lua 脚本
* @return SHA1
*/
@Override
public String loadScript(String script) {
Assert.isTrue(StrUtil.isNotBlank(script), "Lua 脚本不能为空");
String sha1 = redissonClient.getScript().scriptLoad(script);
log.info("Redis Lua 脚本加载成功,sha1={}", sha1);
return sha1;
}
/**
* 创建批处理对象。
*
* @return RBatch
*/
@Override
public RBatch createBatch() {
return redissonClient.createBatch();
}
/**
* 创建事务对象。
*
* @return RTransaction
*/
@Override
public RTransaction createTransaction() {
return redissonClient.createTransaction(TransactionOptions.defaults());
}
// -------------------------------------------------------------------------
// LocalCachedMap / 本地缓存 Map
// -------------------------------------------------------------------------
/**
* 获取本地缓存 Map。
*
* @param key Redis 键
* @param options 本地缓存配置
* @param <K> 字段类型
* @param <V> 值类型
* @return RLocalCachedMap
*/
@Override
public <K, V> RLocalCachedMap<K, V> getLocalCachedMap(String key, LocalCachedMapOptions<K, V> options) {
checkKey(key);
Assert.notNull(options, "本地缓存配置不能为空");
return redissonClient.getLocalCachedMap(key, options);
}
/**
* 设置本地缓存 Map 字段值。
*
* @param key Redis 键
* @param field 字段
* @param value 字段值
* @param options 本地缓存配置
*/
@Override
public void lcPut(String key, Object field, Object value, LocalCachedMapOptions<Object, Object> options) {
checkKey(key);
Assert.notNull(field, "本地缓存 Map 字段不能为空");
getLocalCachedMap(key, options).fastPut(field, value);
}
/**
* 字段不存在时设置本地缓存 Map 字段值。
*
* @param key Redis 键
* @param field 字段
* @param value 字段值
* @param options 本地缓存配置
* @return 是否设置成功
*/
@Override
public boolean lcPutIfAbsent(String key, Object field, Object value, LocalCachedMapOptions<Object, Object> options) {
checkKey(key);
Assert.notNull(field, "本地缓存 Map 字段不能为空");
return getLocalCachedMap(key, options).fastPutIfAbsent(field, value);
}
/**
* 获取本地缓存 Map 字段值。
*
* @param key Redis 键
* @param field 字段
* @param clazz 目标类型
* @param options 本地缓存配置
* @param <T> 泛型类型
* @return 字段值
*/
@Override
public <T> T lcGet(String key, Object field, Class<T> clazz, LocalCachedMapOptions<Object, Object> options) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(field) || ObjectUtil.isNull(clazz) || ObjectUtil.isNull(options)) {
return null;
}
Object value = getLocalCachedMap(key, options).get(field);
return convertValue(value, clazz);
}
/**
* 获取本地缓存 Map 字段值。
*
* @param key Redis 键
* @param field 字段
* @param typeReference 目标类型
* @param options 本地缓存配置
* @param <T> 泛型类型
* @return 字段值
*/
@Override
public <T> T lcGet(String key, Object field, TypeReference<T> typeReference, LocalCachedMapOptions<Object, Object> options) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(field) || ObjectUtil.isNull(typeReference) || ObjectUtil.isNull(options)) {
return null;
}
Object value = getLocalCachedMap(key, options).get(field);
return convertValue(value, typeReference);
}
/**
* 删除本地缓存 Map 字段。
*
* @param key Redis 键
* @param field 字段
* @param options 本地缓存配置
* @return 删除前的值
*/
@Override
public Object lcRemove(String key, Object field, LocalCachedMapOptions<Object, Object> options) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(field) || ObjectUtil.isNull(options)) {
return null;
}
return getLocalCachedMap(key, options).remove(field);
}
/**
* 判断本地缓存 Map 字段是否存在。
*
* @param key Redis 键
* @param field 字段
* @param options 本地缓存配置
* @return 存在返回 true
*/
@Override
public boolean lcContainsKey(String key, Object field, LocalCachedMapOptions<Object, Object> options) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(field) || ObjectUtil.isNull(options)) {
return false;
}
return getLocalCachedMap(key, options).containsKey(field);
}
/**
* 获取本地缓存 Map 大小。
*
* @param key Redis 键
* @param options 本地缓存配置
* @return 大小
*/
@Override
public int lcSize(String key, LocalCachedMapOptions<Object, Object> options) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(options)) {
return 0;
}
return getLocalCachedMap(key, options).size();
}
/**
* 清空本地缓存 Map。
*
* @param key Redis 键
* @param options 本地缓存配置
*/
@Override
public void lcClear(String key, LocalCachedMapOptions<Object, Object> options) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(options)) {
return;
}
getLocalCachedMap(key, options).clear();
}
/**
* 仅清空当前 JVM 内的本地缓存,不清空 Redis 远端数据。
*
* @param key Redis 键
* @param options 本地缓存配置
*/
@Override
public void lcClearLocalCache(String key, LocalCachedMapOptions<Object, Object> options) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(options)) {
return;
}
getLocalCachedMap(key, options).clearLocalCache();
}
// -------------------------------------------------------------------------
// JsonBucket / RedisJSON
// -------------------------------------------------------------------------
/**
* 获取 JSON Bucket。
*
* @param key Redis 键
* @param codec JSON 编解码器
* @param <T> 值类型
* @return RJsonBucket
*/
@Override
public <T> RJsonBucket<T> getJsonBucket(String key, JsonCodec codec) {
checkKey(key);
Assert.notNull(codec, "JSON编解码器不能为空");
return redissonClient.getJsonBucket(key, codec);
}
/**
* 设置 JSON 文档。
*
* @param key Redis 键
* @param value JSON 对象
* @param codec JSON 编解码器
* @param <T> 值类型
*/
@Override
public <T> void jsonSet(String key, T value, JsonCodec codec) {
checkKey(key);
Assert.notNull(codec, "JSON编解码器不能为空");
getJsonBucket(key, codec).set(value);
}
/**
* 设置 JSON 文档并指定过期时间。
*
* @param key Redis 键
* @param value JSON 对象
* @param ttl 过期时间
* @param codec JSON 编解码器
* @param <T> 值类型
*/
@Override
public <T> void jsonSet(String key, T value, Duration ttl, JsonCodec codec) {
checkKey(key);
checkPositiveDuration(ttl, "JSON过期时间不能为空且必须大于0");
Assert.notNull(codec, "JSON编解码器不能为空");
getJsonBucket(key, codec).set(value, ttl.toMillis(), TimeUnit.MILLISECONDS);
}
/**
* 获取完整 JSON 文档。
*
* @param key Redis 键
* @param codec JSON 编解码器
* @param <T> 值类型
* @return JSON 对象
*/
@Override
@SuppressWarnings("unchecked")
public <T> T jsonGet(String key, JsonCodec codec) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(codec)) {
return null;
}
return (T) getJsonBucket(key, codec).get();
}
/**
* 根据 JSONPath 获取 JSON 局部内容。
*
* @param key Redis 键
* @param path JSONPath
* @param codec JSON 编解码器
* @param <T> 返回类型
* @return JSONPath 对应的值
*/
@Override
public <T> T jsonGet(String key, String path, JsonCodec codec) {
if (StrUtil.hasBlank(key, path) || ObjectUtil.isNull(codec)) {
return null;
}
return getJsonBucket(key, codec).get(codec, path);
}
/**
* 根据 JSONPath 设置 JSON 局部内容。
*
* @param key Redis 键
* @param path JSONPath
* @param value 值
* @param codec JSON 编解码器
* @param <T> JSON 文档类型
*/
@Override
public <T> void jsonSet(String key, String path, Object value, JsonCodec codec) {
checkKey(key);
Assert.isTrue(StrUtil.isNotBlank(path), "JSONPath不能为空");
Assert.notNull(codec, "JSON编解码器不能为空");
getJsonBucket(key, codec).set(path, value);
}
/**
* JSONPath 不存在时设置局部内容。
*
* @param key Redis 键
* @param path JSONPath
* @param value 值
* @param codec JSON 编解码器
* @param <T> JSON 文档类型
* @return 是否设置成功
*/
@Override
public <T> boolean jsonSetIfAbsent(String key, String path, Object value, JsonCodec codec) {
checkKey(key);
Assert.isTrue(StrUtil.isNotBlank(path), "JSONPath不能为空");
Assert.notNull(codec, "JSON编解码器不能为空");
return getJsonBucket(key, codec).setIfAbsent(path, value);
}
/**
* JSONPath 存在时设置局部内容。
*
* @param key Redis 键
* @param path JSONPath
* @param value 值
* @param codec JSON 编解码器
* @param <T> JSON 文档类型
* @return 是否设置成功
*/
@Override
public <T> boolean jsonSetIfExists(String key, String path, Object value, JsonCodec codec) {
checkKey(key);
Assert.isTrue(StrUtil.isNotBlank(path), "JSONPath不能为空");
Assert.notNull(codec, "JSON编解码器不能为空");
return getJsonBucket(key, codec).setIfExists(path, value);
}
/**
* 删除 JSONPath 对应内容。
*
* @param key Redis 键
* @param path JSONPath
* @param codec JSON 编解码器
* @param <T> JSON 文档类型
* @return 删除数量
*/
@Override
public <T> long jsonDelete(String key, String path, JsonCodec codec) {
if (StrUtil.hasBlank(key, path) || ObjectUtil.isNull(codec)) {
return 0L;
}
return getJsonBucket(key, codec).delete(path);
}
/**
* 向 JSON 数组追加元素。
*
* @param key Redis 键
* @param path JSONPath
* @param codec JSON 编解码器
* @param values 追加值
* @param <T> JSON 文档类型
* @return 追加后的数组长度
*/
@Override
public <T> long jsonArrayAppend(String key, String path, JsonCodec codec, Object... values) {
checkKey(key);
Assert.isTrue(StrUtil.isNotBlank(path), "JSONPath不能为空");
Assert.notNull(codec, "JSON编解码器不能为空");
if (ArrayUtil.isEmpty(values)) {
return 0L;
}
return getJsonBucket(key, codec).arrayAppend(path, values);
}
/**
* 获取 JSON 对象字段名。
*
* @param key Redis 键
* @param codec JSON 编解码器
* @param <T> JSON 文档类型
* @return 字段名集合
*/
@Override
public <T> List<String> jsonKeys(String key, JsonCodec codec) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(codec)) {
return Collections.emptyList();
}
return getJsonBucket(key, codec).getKeys();
}
/**
* 清空 JSON 文档。
*
* @param key Redis 键
* @param codec JSON 编解码器
* @param <T> JSON 文档类型
*/
@Override
public <T> void jsonClear(String key, JsonCodec codec) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(codec)) {
return;
}
getJsonBucket(key, codec).clear();
}
// -------------------------------------------------------------------------
// BinaryStream / 二进制流
// -------------------------------------------------------------------------
/**
* 获取二进制流对象。
*
* @param key Redis 键
* @return RBinaryStream
*/
@Override
public RBinaryStream getBinaryStream(String key) {
checkKey(key);
return redissonClient.getBinaryStream(key);
}
/**
* 获取二进制输入流。
*
* @param key Redis 键
* @return InputStream
*/
@Override
public InputStream binaryInputStream(String key) {
return getBinaryStream(key).getInputStream();
}
/**
* 获取二进制输出流。
*
* @param key Redis 键
* @return OutputStream
*/
@Override
public OutputStream binaryOutputStream(String key) {
return getBinaryStream(key).getOutputStream();
}
/**
* 写入二进制数据。
*
* @param key Redis 键
* @param data 字节数组
*/
@Override
public void binaryWrite(String key, byte[] data) {
checkKey(key);
Assert.notNull(data, "二进制数据不能为空");
getBinaryStream(key).set(data);
}
/**
* 读取全部二进制数据。
*
* @param key Redis 键
* @return 字节数组
*/
@Override
public byte[] binaryReadAll(String key) {
if (StrUtil.isBlank(key)) {
return new byte[0];
}
byte[] data = getBinaryStream(key).get();
return ObjectUtil.defaultIfNull(data, new byte[0]);
}
/**
* 获取二进制数据大小。
*
* @param key Redis 键
* @return 字节大小
*/
@Override
public long binarySize(String key) {
if (StrUtil.isBlank(key)) {
return 0L;
}
return getBinaryStream(key).size();
}
/**
* 删除二进制数据。
*
* @param key Redis 键
* @return 是否删除成功
*/
@Override
public boolean binaryDelete(String key) {
if (StrUtil.isBlank(key)) {
return false;
}
return getBinaryStream(key).delete();
}
// -------------------------------------------------------------------------
// Multimap / 一键多值
// -------------------------------------------------------------------------
/**
* 获取 SetMultimap。
*
* @param key Redis 键
* @param <K> 字段类型
* @param <V> 值类型
* @return RSetMultimap
*/
@Override
public <K, V> RSetMultimap<K, V> getSetMultimap(String key) {
checkKey(key);
return redissonClient.getSetMultimap(key);
}
/**
* 获取 ListMultimap。
*
* @param key Redis 键
* @param <K> 字段类型
* @param <V> 值类型
* @return RListMultimap
*/
@Override
public <K, V> RListMultimap<K, V> getListMultimap(String key) {
checkKey(key);
return redissonClient.getListMultimap(key);
}
/**
* 向 SetMultimap 添加值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 是否新增
*/
@Override
public boolean smmPut(String key, Object mapKey, Object mapValue) {
checkKey(key);
Assert.notNull(mapKey, "Multimap字段不能为空");
return redissonClient.<Object, Object>getSetMultimap(key).put(mapKey, mapValue);
}
/**
* 获取 SetMultimap 指定字段的值集合。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 值集合
*/
@Override
public Set<Object> smmGet(String key, Object mapKey) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(mapKey)) {
return Collections.emptySet();
}
return redissonClient.<Object, Object>getSetMultimap(key).getAll(mapKey);
}
/**
* 删除 SetMultimap 指定字段的指定值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 是否删除成功
*/
@Override
public boolean smmRemove(String key, Object mapKey, Object mapValue) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(mapKey)) {
return false;
}
return redissonClient.<Object, Object>getSetMultimap(key).remove(mapKey, mapValue);
}
/**
* 删除 SetMultimap 指定字段的全部值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 删除的值集合
*/
@Override
public Set<Object> smmRemoveAll(String key, Object mapKey) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(mapKey)) {
return Collections.emptySet();
}
return redissonClient.<Object, Object>getSetMultimap(key).removeAll(mapKey);
}
/**
* 判断 SetMultimap 是否包含指定字段。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 存在返回 true
*/
@Override
public boolean smmContainsKey(String key, Object mapKey) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(mapKey)) {
return false;
}
return redissonClient.<Object, Object>getSetMultimap(key).containsKey(mapKey);
}
/**
* 判断 SetMultimap 是否包含指定字段和值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 存在返回 true
*/
@Override
public boolean smmContainsEntry(String key, Object mapKey, Object mapValue) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(mapKey)) {
return false;
}
return redissonClient.<Object, Object>getSetMultimap(key).containsEntry(mapKey, mapValue);
}
/**
* 获取 SetMultimap 总值数量。
*
* @param key Redis 键
* @return 总值数量
*/
@Override
public int smmSize(String key) {
if (StrUtil.isBlank(key)) {
return 0;
}
return redissonClient.<Object, Object>getSetMultimap(key).size();
}
/**
* 清空 SetMultimap。
*
* @param key Redis 键
*/
@Override
public void smmClear(String key) {
if (StrUtil.isBlank(key)) {
return;
}
redissonClient.<Object, Object>getSetMultimap(key).clear();
}
/**
* 向 ListMultimap 添加值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 是否新增
*/
@Override
public boolean lmmPut(String key, Object mapKey, Object mapValue) {
checkKey(key);
Assert.notNull(mapKey, "Multimap字段不能为空");
return redissonClient.<Object, Object>getListMultimap(key).put(mapKey, mapValue);
}
/**
* 获取 ListMultimap 指定字段的值列表。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 值列表
*/
@Override
public List<Object> lmmGet(String key, Object mapKey) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(mapKey)) {
return Collections.emptyList();
}
return redissonClient.<Object, Object>getListMultimap(key).getAll(mapKey);
}
/**
* 删除 ListMultimap 指定字段的指定值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 是否删除成功
*/
@Override
public boolean lmmRemove(String key, Object mapKey, Object mapValue) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(mapKey)) {
return false;
}
return redissonClient.<Object, Object>getListMultimap(key).remove(mapKey, mapValue);
}
/**
* 删除 ListMultimap 指定字段的全部值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 删除的值列表
*/
@Override
public List<Object> lmmRemoveAll(String key, Object mapKey) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(mapKey)) {
return Collections.emptyList();
}
return redissonClient.<Object, Object>getListMultimap(key).removeAll(mapKey);
}
/**
* 判断 ListMultimap 是否包含指定字段。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 存在返回 true
*/
@Override
public boolean lmmContainsKey(String key, Object mapKey) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(mapKey)) {
return false;
}
return redissonClient.<Object, Object>getListMultimap(key).containsKey(mapKey);
}
/**
* 判断 ListMultimap 是否包含指定字段和值。
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 存在返回 true
*/
@Override
public boolean lmmContainsEntry(String key, Object mapKey, Object mapValue) {
if (StrUtil.isBlank(key) || ObjectUtil.isNull(mapKey)) {
return false;
}
return redissonClient.<Object, Object>getListMultimap(key).containsEntry(mapKey, mapValue);
}
/**
* 获取 ListMultimap 总值数量。
*
* @param key Redis 键
* @return 总值数量
*/
@Override
public int lmmSize(String key) {
if (StrUtil.isBlank(key)) {
return 0;
}
return redissonClient.<Object, Object>getListMultimap(key).size();
}
/**
* 清空 ListMultimap。
*
* @param key Redis 键
*/
@Override
public void lmmClear(String key) {
if (StrUtil.isBlank(key)) {
return;
}
redissonClient.<Object, Object>getListMultimap(key).clear();
}
// -------------------------------------------------------------------------
// SortedSet / LexSortedSet
// -------------------------------------------------------------------------
/**
* 获取自然排序集合。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RSortedSet
*/
@Override
public <T> RSortedSet<T> getSortedSet(String key) {
checkKey(key);
return redissonClient.getSortedSet(key);
}
/**
* 获取字典序排序集合。
*
* @param key Redis 键
* @return RLexSortedSet
*/
@Override
public RLexSortedSet getLexSortedSet(String key) {
checkKey(key);
return redissonClient.getLexSortedSet(key);
}
/**
* 添加自然排序集合元素。
*
* @param key Redis 键
* @param value 元素
* @return 是否新增
*/
@Override
public boolean sortedSetAdd(String key, Object value) {
checkKey(key);
Assert.notNull(value, "自然排序集合元素不能为空");
return redissonClient.<Object>getSortedSet(key).add(value);
}
/**
* 批量添加自然排序集合元素。
*
* @param key Redis 键
* @param values 元素集合
* @return 是否有新增
*/
@Override
public boolean sortedSetAddAll(String key, Collection<?> values) {
checkKey(key);
if (CollUtil.isEmpty(values)) {
return false;
}
Collection<Object> valueList = new ArrayList<>(values.size());
for (Object value : values) {
if (ObjectUtil.isNotNull(value)) {
valueList.add(value);
}
}
if (CollUtil.isEmpty(valueList)) {
return false;
}
return redissonClient.<Object>getSortedSet(key).addAll(valueList);
}
/**
* 获取自然排序集合全部元素。
*
* @param key Redis 键
* @return 元素集合
*/
@Override
public Collection<Object> sortedSetReadAll(String key) {
if (StrUtil.isBlank(key)) {
return Collections.emptyList();
}
return new ArrayList<>(redissonClient.<Object>getSortedSet(key));
}
/**
* 删除自然排序集合元素。
*
* @param key Redis 键
* @param values 元素
* @return 是否删除成功
*/
@Override
public boolean sortedSetRemove(String key, Object... values) {
if (StrUtil.isBlank(key) || ArrayUtil.isEmpty(values)) {
return false;
}
return redissonClient.<Object>getSortedSet(key).removeAll(Arrays.asList(values));
}
/**
* 获取自然排序集合大小。
*
* @param key Redis 键
* @return 大小
*/
@Override
public int sortedSetSize(String key) {
if (StrUtil.isBlank(key)) {
return 0;
}
return redissonClient.<Object>getSortedSet(key).size();
}
/**
* 清空自然排序集合。
*
* @param key Redis 键
*/
@Override
public void sortedSetClear(String key) {
if (StrUtil.isBlank(key)) {
return;
}
redissonClient.<Object>getSortedSet(key).clear();
}
/**
* 添加字典序排序集合元素。
*
* @param key Redis 键
* @param value 元素
* @return 是否新增
*/
@Override
public boolean lexAdd(String key, String value) {
checkKey(key);
Assert.isTrue(StrUtil.isNotBlank(value), "字典序集合元素不能为空");
return redissonClient.getLexSortedSet(key).add(value);
}
/**
* 批量添加字典序排序集合元素。
*
* @param key Redis 键
* @param values 元素集合
* @return 是否有新增
*/
@Override
public boolean lexAddAll(String key, Collection<String> values) {
checkKey(key);
if (CollUtil.isEmpty(values)) {
return false;
}
List<String> valueList = values.stream()
.filter(StrUtil::isNotBlank)
.distinct()
.toList();
if (CollUtil.isEmpty(valueList)) {
return false;
}
return redissonClient.getLexSortedSet(key).addAll(valueList);
}
/**
* 获取字典序排序集合全部元素。
*
* @param key Redis 键
* @return 元素集合
*/
@Override
public Collection<String> lexReadAll(String key) {
if (StrUtil.isBlank(key)) {
return Collections.emptyList();
}
return new ArrayList<>(redissonClient.getLexSortedSet(key));
}
/**
* 获取大于等于指定元素的字典序集合。
*
* @param key Redis 键
* @param from 开始元素
* @return 元素集合
*/
@Override
public Collection<String> lexRangeTail(String key, String from) {
if (StrUtil.hasBlank(key, from)) {
return Collections.emptyList();
}
return redissonClient.getLexSortedSet(key)
.rangeTail(from, true, 0, Integer.MAX_VALUE);
}
/**
* 获取小于等于指定元素的字典序集合。
*
* @param key Redis 键
* @param to 结束元素
* @return 元素集合
*/
@Override
public Collection<String> lexRangeHead(String key, String to) {
if (StrUtil.hasBlank(key, to)) {
return Collections.emptyList();
}
return redissonClient.getLexSortedSet(key)
.rangeHead(to, true, 0, Integer.MAX_VALUE);
}
/**
* 删除字典序排序集合元素。
*
* @param key Redis 键
* @param values 元素
* @return 是否删除成功
*/
@Override
public boolean lexRemove(String key, String... values) {
if (StrUtil.isBlank(key) || ArrayUtil.isEmpty(values)) {
return false;
}
List<String> valueList = Arrays.stream(values)
.filter(StrUtil::isNotBlank)
.distinct()
.toList();
if (CollUtil.isEmpty(valueList)) {
return false;
}
return redissonClient.getLexSortedSet(key).removeAll(valueList);
}
/**
* 获取字典序排序集合大小。
*
* @param key Redis 键
* @return 大小
*/
@Override
public int lexSize(String key) {
if (StrUtil.isBlank(key)) {
return 0;
}
return redissonClient.getLexSortedSet(key).size();
}
/**
* 清空字典序排序集合。
*
* @param key Redis 键
*/
@Override
public void lexClear(String key) {
if (StrUtil.isBlank(key)) {
return;
}
redissonClient.getLexSortedSet(key).clear();
}
// -------------------------------------------------------------------------
// LongAdder / DoubleAdder
// -------------------------------------------------------------------------
/**
* 获取分布式 LongAdder。
*
* @param key Redis 键
* @return RLongAdder
*/
@Override
public RLongAdder getLongAdder(String key) {
checkKey(key);
return redissonClient.getLongAdder(key);
}
/**
* 获取分布式 DoubleAdder。
*
* @param key Redis 键
* @return RDoubleAdder
*/
@Override
public RDoubleAdder getDoubleAdder(String key) {
checkKey(key);
return redissonClient.getDoubleAdder(key);
}
/**
* LongAdder 增加。
*
* @param key Redis 键
* @param delta 增量
*/
@Override
public void longAdderAdd(String key, long delta) {
checkKey(key);
redissonClient.getLongAdder(key).add(delta);
}
/**
* LongAdder 求和。
*
* @param key Redis 键
* @return 当前总和
*/
@Override
public long longAdderSum(String key) {
if (StrUtil.isBlank(key)) {
return 0L;
}
return redissonClient.getLongAdder(key).sum();
}
/**
* LongAdder 重置。
*
* @param key Redis 键
*/
@Override
public void longAdderReset(String key) {
if (StrUtil.isBlank(key)) {
return;
}
redissonClient.getLongAdder(key).reset();
}
/**
* LongAdder 求和后重置。
* 注意:Redisson RLongAdder 无原生 sumThenReset,同步语义为先 sum 再 reset。
*
* @param key Redis 键
* @return 重置前总和
*/
@Override
public long longAdderSumThenReset(String key) {
if (StrUtil.isBlank(key)) {
return 0L;
}
RLongAdder adder = redissonClient.getLongAdder(key);
long value = adder.sum();
adder.reset();
return value;
}
/**
* DoubleAdder 增加。
*
* @param key Redis 键
* @param delta 增量
*/
@Override
public void doubleAdderAdd(String key, double delta) {
checkKey(key);
redissonClient.getDoubleAdder(key).add(delta);
}
/**
* DoubleAdder 求和。
*
* @param key Redis 键
* @return 当前总和
*/
@Override
public double doubleAdderSum(String key) {
if (StrUtil.isBlank(key)) {
return 0D;
}
return redissonClient.getDoubleAdder(key).sum();
}
/**
* DoubleAdder 重置。
*
* @param key Redis 键
*/
@Override
public void doubleAdderReset(String key) {
if (StrUtil.isBlank(key)) {
return;
}
redissonClient.getDoubleAdder(key).reset();
}
/**
* DoubleAdder 求和后重置。
* 注意:Redisson RDoubleAdder 无原生 sumThenReset,同步语义为先 sum 再 reset。
*
* @param key Redis 键
* @return 重置前总和
*/
@Override
public double doubleAdderSumThenReset(String key) {
if (StrUtil.isBlank(key)) {
return 0D;
}
RDoubleAdder adder = redissonClient.getDoubleAdder(key);
double value = adder.sum();
adder.reset();
return value;
}
// -------------------------------------------------------------------------
// Bounded / Priority 队列增强
// -------------------------------------------------------------------------
/**
* 获取有界阻塞队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RBoundedBlockingQueue
*/
@Override
public <T> RBoundedBlockingQueue<T> getBoundedBlockingQueue(String key) {
checkKey(key);
return redissonClient.getBoundedBlockingQueue(key);
}
/**
* 初始化有界阻塞队列容量。
*
* @param key Redis 键
* @param capacity 容量
* @return 是否初始化成功
*/
@Override
public boolean boundedQueueTrySetCapacity(String key, int capacity) {
checkKey(key);
Assert.isTrue(capacity > 0, "有界阻塞队列容量必须大于0");
return redissonClient.getBoundedBlockingQueue(key).trySetCapacity(capacity);
}
/**
* 有界阻塞队列入队。
*
* @param key Redis 键
* @param value 元素
* @param <T> 元素类型
* @return 是否入队成功
*/
@Override
public <T> boolean boundedQueueOffer(String key, T value) {
checkKey(key);
return redissonClient.<T>getBoundedBlockingQueue(key).offer(value);
}
/**
* 有界阻塞队列超时入队。
*
* @param key Redis 键
* @param value 元素
* @param timeout 等待时间
* @param unit 时间单位
* @param <T> 元素类型
* @return 是否入队成功
* @throws InterruptedException 线程中断时抛出
*/
@Override
public <T> boolean boundedQueueOffer(String key, T value, long timeout, TimeUnit unit) throws InterruptedException {
checkKey(key);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(timeout >= 0, "等待时间不能小于0");
return redissonClient.<T>getBoundedBlockingQueue(key).offer(value, timeout, unit);
}
/**
* 有界阻塞队列出队。
*
* @param key Redis 键
* @param <T> 元素类型
* @return 元素
*/
@Override
public <T> T boundedQueuePoll(String key) {
if (StrUtil.isBlank(key)) {
return null;
}
return redissonClient.<T>getBoundedBlockingQueue(key).poll();
}
/**
* 有界阻塞队列超时出队。
*
* @param key Redis 键
* @param timeout 等待时间
* @param unit 时间单位
* @param <T> 元素类型
* @return 元素
* @throws InterruptedException 线程中断时抛出
*/
@Override
public <T> T boundedQueuePoll(String key, long timeout, TimeUnit unit) throws InterruptedException {
checkKey(key);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(timeout >= 0, "等待时间不能小于0");
return redissonClient.<T>getBoundedBlockingQueue(key).poll(timeout, unit);
}
/**
* 获取有界阻塞队列大小。
*
* @param key Redis 键
* @return 队列大小
*/
@Override
public int boundedQueueSize(String key) {
if (StrUtil.isBlank(key)) {
return 0;
}
return redissonClient.getBoundedBlockingQueue(key).size();
}
/**
* 获取有界阻塞队列剩余容量。
*
* @param key Redis 键
* @return 剩余容量
*/
@Override
public int boundedQueueRemainingCapacity(String key) {
if (StrUtil.isBlank(key)) {
return 0;
}
return redissonClient.getBoundedBlockingQueue(key).remainingCapacity();
}
/**
* 获取优先级双端队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RPriorityDeque
*/
@Override
public <T> RPriorityDeque<T> getPriorityDeque(String key) {
checkKey(key);
return redissonClient.getPriorityDeque(key);
}
/**
* 获取优先级阻塞队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RPriorityBlockingQueue
*/
@Override
public <T> RPriorityBlockingQueue<T> getPriorityBlockingQueue(String key) {
checkKey(key);
return redissonClient.getPriorityBlockingQueue(key);
}
/**
* 获取优先级阻塞双端队列。
*
* @param key Redis 键
* @param <T> 元素类型
* @return RPriorityBlockingDeque
*/
@Override
public <T> RPriorityBlockingDeque<T> getPriorityBlockingDeque(String key) {
checkKey(key);
return redissonClient.getPriorityBlockingDeque(key);
}
/**
* 优先级队列入队。
*
* @param key Redis 键
* @param value 元素,建议实现 Comparable
* @param <T> 元素类型
* @return 是否入队成功
*/
@Override
public <T> boolean priorityQueueOffer(String key, T value) {
checkKey(key);
return redissonClient.<T>getPriorityQueue(key).offer(value);
}
/**
* 优先级队列出队。
*
* @param key Redis 键
* @param <T> 元素类型
* @return 元素
*/
@Override
public <T> T priorityQueuePoll(String key) {
if (StrUtil.isBlank(key)) {
return null;
}
return redissonClient.<T>getPriorityQueue(key).poll();
}
/**
* 优先级双端队列从头部入队。
*
* @param key Redis 键
* @param value 元素,建议实现 Comparable
* @param <T> 元素类型
* @return 是否入队成功
*/
@Override
public <T> boolean priorityDequeOfferFirst(String key, T value) {
checkKey(key);
return redissonClient.<T>getPriorityDeque(key).offerFirst(value);
}
/**
* 优先级双端队列从尾部入队。
*
* @param key Redis 键
* @param value 元素,建议实现 Comparable
* @param <T> 元素类型
* @return 是否入队成功
*/
@Override
public <T> boolean priorityDequeOfferLast(String key, T value) {
checkKey(key);
return redissonClient.<T>getPriorityDeque(key).offerLast(value);
}
/**
* 优先级双端队列从头部出队。
*
* @param key Redis 键
* @param <T> 元素类型
* @return 元素
*/
@Override
public <T> T priorityDequePollFirst(String key) {
if (StrUtil.isBlank(key)) {
return null;
}
return redissonClient.<T>getPriorityDeque(key).pollFirst();
}
/**
* 优先级双端队列从尾部出队。
*
* @param key Redis 键
* @param <T> 元素类型
* @return 元素
*/
@Override
public <T> T priorityDequePollLast(String key) {
if (StrUtil.isBlank(key)) {
return null;
}
return redissonClient.<T>getPriorityDeque(key).pollLast();
}
// -------------------------------------------------------------------------
// ReliableQueue / 可靠队列
// -------------------------------------------------------------------------
/**
* 设置可靠队列配置。
*
* @param key 队列 key
* @param config 队列配置
*/
@Override
public void reliableQueueSetConfig(String key, QueueConfig config) {
checkKey(key);
Assert.notNull(config, "可靠队列配置不能为空");
redissonClient.getReliableQueue(key).setConfig(config);
log.info("Redis 可靠队列配置已设置,key={}", key);
}
/**
* 队列配置不存在时设置可靠队列配置。
*
* @param key 队列 key
* @param config 队列配置
* @return 是否设置成功
*/
@Override
public boolean reliableQueueSetConfigIfAbsent(String key, QueueConfig config) {
checkKey(key);
Assert.notNull(config, "可靠队列配置不能为空");
return redissonClient.getReliableQueue(key).setConfigIfAbsent(config);
}
/**
* 可靠队列添加消息。
*
* @param key 队列 key
* @param args 添加参数
* @param <T> 消息类型
* @return 添加后的消息
*/
@Override
public <T> Message<T> reliableQueueAdd(String key, QueueAddArgs<T> args) {
checkKey(key);
Assert.notNull(args, "可靠队列添加参数不能为空");
return redissonClient.<T>getReliableQueue(key).add(args);
}
/**
* 可靠队列批量添加消息。
*
* @param key 队列 key
* @param args 添加参数
* @param <T> 消息类型
* @return 添加后的消息集合
*/
@Override
public <T> List<Message<T>> reliableQueueAddMany(String key, QueueAddArgs<T> args) {
checkKey(key);
Assert.notNull(args, "可靠队列添加参数不能为空");
return redissonClient.<T>getReliableQueue(key).addMany(args);
}
/**
* 可靠队列拉取一条消息。
*
* @param key 队列 key
* @param <T> 消息类型
* @return 消息
*/
@Override
public <T> Message<T> reliableQueuePoll(String key) {
if (StrUtil.isBlank(key)) {
return null;
}
return redissonClient.<T>getReliableQueue(key).poll();
}
/**
* 可靠队列按参数拉取一条消息。
*
* @param key 队列 key
* @param args 拉取参数
* @param <T> 消息类型
* @return 消息
*/
@Override
public <T> Message<T> reliableQueuePoll(String key, QueuePollArgs args) {
checkKey(key);
Assert.notNull(args, "可靠队列拉取参数不能为空");
return redissonClient.<T>getReliableQueue(key).poll(args);
}
/**
* 可靠队列批量拉取消息。
*
* @param key 队列 key
* @param args 拉取参数
* @param <T> 消息类型
* @return 消息集合
*/
@Override
public <T> List<Message<T>> reliableQueuePollMany(String key, QueuePollArgs args) {
checkKey(key);
Assert.notNull(args, "可靠队列拉取参数不能为空");
return redissonClient.<T>getReliableQueue(key).pollMany(args);
}
/**
* 确认可靠队列消息处理成功。
*
* @param key 队列 key
* @param args ACK 参数
*/
@Override
public void reliableQueueAck(String key, QueueAckArgs args) {
checkKey(key);
Assert.notNull(args, "可靠队列ACK参数不能为空");
redissonClient.getReliableQueue(key).acknowledge(args);
}
/**
* 标记可靠队列消息处理失败。
*
* @param key 队列 key
* @param args NACK 参数
*/
@Override
public void reliableQueueNack(String key, QueueNegativeAckArgs args) {
checkKey(key);
Assert.notNull(args, "可靠队列NACK参数不能为空");
redissonClient.getReliableQueue(key).negativeAcknowledge(args);
}
/**
* 根据消息 ID 获取可靠队列消息。
*
* @param key 队列 key
* @param id 消息 ID
* @param <T> 消息类型
* @return 消息
*/
@Override
public <T> Message<T> reliableQueueGet(String key, String id) {
if (StrUtil.hasBlank(key, id)) {
return null;
}
return redissonClient.<T>getReliableQueue(key).get(id);
}
/**
* 根据消息 ID 批量获取可靠队列消息。
*
* @param key 队列 key
* @param ids 消息 ID
* @param <T> 消息类型
* @return 消息集合
*/
@Override
public <T> List<Message<T>> reliableQueueGetAll(String key, String... ids) {
if (StrUtil.isBlank(key) || ArrayUtil.isEmpty(ids)) {
return Collections.emptyList();
}
String[] idArray = Arrays.stream(ids)
.filter(StrUtil::isNotBlank)
.distinct()
.toArray(String[]::new);
if (ArrayUtil.isEmpty(idArray)) {
return Collections.emptyList();
}
return redissonClient.<T>getReliableQueue(key).getAll(idArray);
}
/**
* 获取可靠队列所有可拉取消息。
*
* @param key 队列 key
* @param <T> 消息类型
* @return 消息集合
*/
@Override
public <T> List<Message<T>> reliableQueueListAll(String key) {
if (StrUtil.isBlank(key)) {
return Collections.emptyList();
}
return redissonClient.<T>getReliableQueue(key).listAll();
}
/**
* 判断可靠队列是否包含指定消息 ID。
*
* @param key 队列 key
* @param id 消息 ID
* @return 包含返回 true
*/
@Override
public boolean reliableQueueContains(String key, String id) {
if (StrUtil.hasBlank(key, id)) {
return false;
}
return redissonClient.getReliableQueue(key).contains(id);
}
/**
* 判断可靠队列包含的消息 ID 数量。
*
* @param key 队列 key
* @param ids 消息 ID
* @return 匹配数量
*/
@Override
public int reliableQueueContainsMany(String key, String... ids) {
if (StrUtil.isBlank(key) || ArrayUtil.isEmpty(ids)) {
return 0;
}
String[] idArray = Arrays.stream(ids)
.filter(StrUtil::isNotBlank)
.distinct()
.toArray(String[]::new);
if (ArrayUtil.isEmpty(idArray)) {
return 0;
}
return redissonClient.getReliableQueue(key).containsMany(idArray);
}
/**
* 删除可靠队列消息。
*
* @param key 队列 key
* @param args 删除参数
* @return 是否删除成功
*/
@Override
public boolean reliableQueueRemove(String key, QueueRemoveArgs args) {
checkKey(key);
Assert.notNull(args, "可靠队列删除参数不能为空");
return redissonClient.getReliableQueue(key).remove(args);
}
/**
* 批量删除可靠队列消息。
*
* @param key 队列 key
* @param args 删除参数
* @return 删除数量
*/
@Override
public int reliableQueueRemoveMany(String key, QueueRemoveArgs args) {
checkKey(key);
Assert.notNull(args, "可靠队列删除参数不能为空");
return redissonClient.getReliableQueue(key).removeMany(args);
}
/**
* 移动可靠队列消息。
*
* @param key 队列 key
* @param args 移动参数
* @return 移动数量
*/
@Override
public int reliableQueueMove(String key, QueueMoveArgs args) {
checkKey(key);
Assert.notNull(args, "可靠队列移动参数不能为空");
return redissonClient.getReliableQueue(key).move(args);
}
/**
* 获取可靠队列消息数量。
*
* @param key 队列 key
* @return 消息数量
*/
@Override
public int reliableQueueSize(String key) {
if (StrUtil.isBlank(key)) {
return 0;
}
return redissonClient.getReliableQueue(key).size();
}
/**
* 获取可靠队列延迟消息数量。
*
* @param key 队列 key
* @return 延迟消息数量
*/
@Override
public int reliableQueueDelayedSize(String key) {
if (StrUtil.isBlank(key)) {
return 0;
}
return redissonClient.getReliableQueue(key).countDelayedMessages();
}
/**
* 获取可靠队列未确认消息数量。
*
* @param key 队列 key
* @return 未确认消息数量
*/
@Override
public int reliableQueueUnacknowledgedSize(String key) {
if (StrUtil.isBlank(key)) {
return 0;
}
return redissonClient.getReliableQueue(key).countUnacknowledgedMessages();
}
/**
* 清空可靠队列全部状态消息。
*
* @param key 队列 key
* @return 是否清空成功
*/
@Override
public boolean reliableQueueClear(String key) {
if (StrUtil.isBlank(key)) {
return false;
}
return redissonClient.getReliableQueue(key).clear();
}
/**
* 获取将当前队列作为死信队列的源队列名称。
*
* @param key 队列 key
* @return 源队列名称集合
*/
@Override
public Set<String> reliableQueueDeadLetterSources(String key) {
if (StrUtil.isBlank(key)) {
return Collections.emptySet();
}
return redissonClient.getReliableQueue(key).getDeadLetterQueueSources();
}
/**
* 添加可靠队列事件监听器。
*
* @param key 队列 key
* @param listener 监听器
* @return 监听器 ID
*/
@Override
public String reliableQueueAddListener(String key, QueueEventListener listener) {
checkKey(key);
Assert.notNull(listener, "可靠队列监听器不能为空");
return redissonClient.getReliableQueue(key).addListener(listener);
}
/**
* 移除可靠队列事件监听器。
*
* @param key 队列 key
* @param listenerId 监听器 ID
*/
@Override
public void reliableQueueRemoveListener(String key, String listenerId) {
if (StrUtil.hasBlank(key, listenerId)) {
return;
}
redissonClient.getReliableQueue(key).removeListener(listenerId);
}
/**
* 启用可靠队列指定操作。
*
* @param key 队列 key
* @param operation 队列操作
*/
@Override
public void reliableQueueEnableOperation(String key, QueueOperation operation) {
checkKey(key);
Assert.notNull(operation, "可靠队列操作不能为空");
redissonClient.getReliableQueue(key).enableOperation(operation);
}
/**
* 禁用可靠队列指定操作。
*
* @param key 队列 key
* @param operation 队列操作
*/
@Override
public void reliableQueueDisableOperation(String key, QueueOperation operation) {
checkKey(key);
Assert.notNull(operation, "可靠队列操作不能为空");
redissonClient.getReliableQueue(key).disableOperation(operation);
}
// -------------------------------------------------------------------------
// Stream / 高级消费治理
// -------------------------------------------------------------------------
/**
* 获取 Stream 详细信息。
*
* @param streamKey Stream key
* @return Stream 信息
*/
@Override
public StreamInfo<Object, Object> streamInfo(String streamKey) {
checkKey(streamKey);
return redissonClient.<Object, Object>getStream(streamKey).getInfo();
}
/**
* 获取 Stream 消费组列表。
*
* @param streamKey Stream key
* @return 消费组列表
*/
@Override
public List<StreamGroup> streamListGroups(String streamKey) {
if (StrUtil.isBlank(streamKey)) {
return Collections.emptyList();
}
return redissonClient.<Object, Object>getStream(streamKey).listGroups();
}
/**
* 获取 Stream 指定消费组的消费者列表。
*
* @param streamKey Stream key
* @param groupName 消费组
* @return 消费者列表
*/
@Override
public List<StreamConsumer> streamListConsumers(String streamKey, String groupName) {
if (StrUtil.hasBlank(streamKey, groupName)) {
return Collections.emptyList();
}
return redissonClient.<Object, Object>getStream(streamKey).listConsumers(groupName);
}
/**
* 创建 Stream 消费者。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 消费者
*/
@Override
public void streamCreateConsumer(String streamKey, String groupName, String consumerName) {
checkKey(streamKey);
checkKey(groupName);
checkKey(consumerName);
redissonClient.<Object, Object>getStream(streamKey).createConsumer(groupName, consumerName);
}
/**
* 删除 Stream 消费者。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 消费者
* @return 该消费者名下的待处理消息数量
*/
@Override
public long streamRemoveConsumer(String streamKey, String groupName, String consumerName) {
checkKey(streamKey);
checkKey(groupName);
checkKey(consumerName);
return redissonClient.<Object, Object>getStream(streamKey).removeConsumer(groupName, consumerName);
}
/**
* 删除 Stream 消费组。
*
* @param streamKey Stream key
* @param groupName 消费组
*/
@Override
public void streamRemoveGroup(String streamKey, String groupName) {
checkKey(streamKey);
checkKey(groupName);
redissonClient.<Object, Object>getStream(streamKey).removeGroup(groupName);
}
/**
* 更新 Stream 消费组读取起始 ID。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param id 消息 ID
*/
@Override
public void streamUpdateGroupMessageId(String streamKey, String groupName, StreamMessageId id) {
checkKey(streamKey);
checkKey(groupName);
Assert.notNull(id, "Stream消息ID不能为空");
redissonClient.<Object, Object>getStream(streamKey).updateGroupMessageId(groupName, id);
}
/**
* 获取 Stream 消费组待处理消息概要。
*
* @param streamKey Stream key
* @param groupName 消费组
* @return 待处理概要
*/
@Override
public PendingResult streamPendingInfo(String streamKey, String groupName) {
checkKey(streamKey);
checkKey(groupName);
return redissonClient.<Object, Object>getStream(streamKey).getPendingInfo(groupName);
}
/**
* 获取 Stream 消费组待处理消息列表。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param startId 开始 ID
* @param endId 结束 ID
* @param count 数量
* @return 待处理消息列表
*/
@Override
public List<PendingEntry> streamListPending(String streamKey, String groupName, StreamMessageId startId, StreamMessageId endId, int count) {
checkKey(streamKey);
checkKey(groupName);
checkStreamRangeArgs(startId, endId, count);
return redissonClient.<Object, Object>getStream(streamKey).listPending(groupName, startId, endId, count);
}
/**
* 获取 Stream 指定消费者的待处理消息列表。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 消费者
* @param startId 开始 ID
* @param endId 结束 ID
* @param count 数量
* @return 待处理消息列表
*/
@Override
public List<PendingEntry> streamListPending(String streamKey, String groupName, String consumerName, StreamMessageId startId, StreamMessageId endId, int count) {
checkKey(streamKey);
checkKey(groupName);
checkKey(consumerName);
checkStreamRangeArgs(startId, endId, count);
return redissonClient.<Object, Object>getStream(streamKey).listPending(groupName, consumerName, startId, endId, count);
}
/**
* 获取 Stream 指定消费者满足最小空闲时间的待处理消息列表。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 消费者
* @param startId 开始 ID
* @param endId 结束 ID
* @param idleTime 最小空闲时间
* @param unit 时间单位
* @param count 数量
* @return 待处理消息列表
*/
@Override
public List<PendingEntry> streamListPending(String streamKey, String groupName, String consumerName,
StreamMessageId startId, StreamMessageId endId,
long idleTime, TimeUnit unit, int count) {
checkKey(streamKey);
checkKey(groupName);
checkKey(consumerName);
checkStreamRangeArgs(startId, endId, count);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(idleTime >= 0, "最小空闲时间不能小于0");
return redissonClient.<Object, Object>getStream(streamKey)
.listPending(groupName, consumerName, startId, endId, idleTime, unit, count);
}
/**
* 按 ID 范围读取 Stream 消息。
*
* @param streamKey Stream key
* @param startId 开始 ID
* @param endId 结束 ID
* @return 消息 Map
*/
@Override
public Map<StreamMessageId, Map<Object, Object>> streamRange(String streamKey, StreamMessageId startId, StreamMessageId endId) {
checkKey(streamKey);
Assert.notNull(startId, "开始消息ID不能为空");
Assert.notNull(endId, "结束消息ID不能为空");
return redissonClient.<Object, Object>getStream(streamKey).range(startId, endId);
}
/**
* 按 ID 范围读取 Stream 消息并限制数量。
*
* @param streamKey Stream key
* @param startId 开始 ID
* @param endId 结束 ID
* @param count 数量
* @return 消息 Map
*/
@Override
public Map<StreamMessageId, Map<Object, Object>> streamRange(String streamKey, StreamMessageId startId, StreamMessageId endId, int count) {
checkKey(streamKey);
checkStreamRangeArgs(startId, endId, count);
return redissonClient.<Object, Object>getStream(streamKey).range(count, startId, endId);
}
/**
* 按 ID 范围倒序读取 Stream 消息。
*
* @param streamKey Stream key
* @param startId 开始 ID
* @param endId 结束 ID
* @return 消息 Map
*/
@Override
public Map<StreamMessageId, Map<Object, Object>> streamRangeReversed(String streamKey, StreamMessageId startId, StreamMessageId endId) {
checkKey(streamKey);
Assert.notNull(startId, "开始消息ID不能为空");
Assert.notNull(endId, "结束消息ID不能为空");
return redissonClient.<Object, Object>getStream(streamKey).rangeReversed(startId, endId);
}
/**
* 按 ID 范围倒序读取 Stream 消息并限制数量。
*
* @param streamKey Stream key
* @param startId 开始 ID
* @param endId 结束 ID
* @param count 数量
* @return 消息 Map
*/
@Override
public Map<StreamMessageId, Map<Object, Object>> streamRangeReversed(String streamKey, StreamMessageId startId, StreamMessageId endId, int count) {
checkKey(streamKey);
checkStreamRangeArgs(startId, endId, count);
return redissonClient.<Object, Object>getStream(streamKey).rangeReversed(count, startId, endId);
}
/**
* 转移待处理 Stream 消息所有权。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 新消费者
* @param idleTime 最小空闲时间
* @param unit 时间单位
* @param ids 消息 ID
* @return 转移后的消息 Map
*/
@Override
public Map<StreamMessageId, Map<Object, Object>> streamClaim(String streamKey, String groupName, String consumerName,
long idleTime, TimeUnit unit, StreamMessageId... ids) {
checkKey(streamKey);
checkKey(groupName);
checkKey(consumerName);
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(idleTime >= 0, "最小空闲时间不能小于0");
if (ArrayUtil.isEmpty(ids)) {
return Collections.emptyMap();
}
return redissonClient.<Object, Object>getStream(streamKey)
.claim(groupName, consumerName, idleTime, unit, ids);
}
/**
* 自动转移待处理 Stream 消息所有权。
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 新消费者
* @param idleTime 最小空闲时间
* @param unit 时间单位
* @param startId 起始 ID
* @param count 数量
* @return 自动转移结果
*/
@Override
public AutoClaimResult<Object, Object> streamAutoClaim(String streamKey, String groupName, String consumerName,
long idleTime, TimeUnit unit, StreamMessageId startId, int count) {
checkKey(streamKey);
checkKey(groupName);
checkKey(consumerName);
Assert.notNull(unit, "时间单位不能为空");
Assert.notNull(startId, "起始消息ID不能为空");
Assert.isTrue(idleTime >= 0, "最小空闲时间不能小于0");
Assert.isTrue(count > 0, "读取数量必须大于0");
return redissonClient.<Object, Object>getStream(streamKey)
.autoClaim(groupName, consumerName, idleTime, unit, startId, count);
}
/**
* 裁剪 Stream。
*
* @param streamKey Stream key
* @param args 裁剪参数
* @return 裁剪数量
*/
@Override
public long streamTrim(String streamKey, StreamTrimArgs args) {
checkKey(streamKey);
Assert.notNull(args, "Stream裁剪参数不能为空");
return redissonClient.<Object, Object>getStream(streamKey).trim(args);
}
/**
* 添加 Stream 对象监听器。
*
* @param streamKey Stream key
* @param listener 对象监听器
* @return 监听器 ID
*/
@Override
public int streamAddListener(String streamKey, ObjectListener listener) {
checkKey(streamKey);
Assert.notNull(listener, "Stream监听器不能为空");
return redissonClient.<Object, Object>getStream(streamKey).addListener(listener);
}
/**
* 移除 Stream 对象监听器。
*
* @param streamKey Stream key
* @param listenerId 监听器 ID
*/
@Override
public void streamRemoveListener(String streamKey, int listenerId) {
if (StrUtil.isBlank(streamKey)) {
return;
}
redissonClient.<Object, Object>getStream(streamKey).removeListener(listenerId);
}
// -------------------------------------------------------------------------
// Executor / Scheduler 分布式任务
// -------------------------------------------------------------------------
/**
* 获取分布式执行器。
*
* @param name 执行器名称
* @return RExecutorService
*/
@Override
public RExecutorService getExecutorService(String name) {
checkKey(name);
return redissonClient.getExecutorService(name);
}
/**
* 获取分布式定时执行器。
*
* @param name 执行器名称
* @return RScheduledExecutorService
*/
@Override
public RScheduledExecutorService getScheduledExecutorService(String name) {
checkKey(name);
// Redisson 3.52.0 中 getExecutorService 返回 RScheduledExecutorService,可同时作为普通执行器和定时执行器使用
return redissonClient.getExecutorService(name);
}
/**
* 执行 Runnable 分布式任务。
*
* @param name 执行器名称
* @param task 任务
*/
@Override
public void executorExecute(String name, Runnable task) {
checkKey(name);
Assert.notNull(task, "分布式任务不能为空");
redissonClient.getExecutorService(name).execute(task);
}
/**
* 提交 Runnable 分布式任务。
*
* @param name 执行器名称
* @param task 任务
* @return Future
*/
@Override
public Future<?> executorSubmit(String name, Runnable task) {
checkKey(name);
Assert.notNull(task, "分布式任务不能为空");
return redissonClient.getExecutorService(name).submit(task);
}
/**
* 提交 Callable 分布式任务。
*
* @param name 执行器名称
* @param task 任务
* @param <T> 返回类型
* @return Future
*/
@Override
public <T> Future<T> executorSubmit(String name, Callable<T> task) {
checkKey(name);
Assert.notNull(task, "分布式任务不能为空");
return redissonClient.getExecutorService(name).submit(task);
}
/**
* 关闭分布式执行器。
*
* @param name 执行器名称
*/
@Override
public void executorShutdown(String name) {
if (StrUtil.isBlank(name)) {
return;
}
redissonClient.getExecutorService(name).shutdown();
}
/**
* 立即关闭分布式执行器。
*
* @param name 执行器名称
* @return 未执行任务集合
*/
@Override
public List<Runnable> executorShutdownNow(String name) {
if (StrUtil.isBlank(name)) {
return Collections.emptyList();
}
return redissonClient.getExecutorService(name).shutdownNow();
}
/**
* 调度 Runnable 分布式任务。
*
* @param name 执行器名称
* @param task 任务
* @param delay 延迟时间
* @param unit 时间单位
* @return ScheduledFuture
*/
@Override
public ScheduledFuture<?> schedule(String name, Runnable task, long delay, TimeUnit unit) {
checkKey(name);
Assert.notNull(task, "分布式定时任务不能为空");
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(delay >= 0, "延迟时间不能小于0");
return redissonClient.getExecutorService(name).schedule(task, delay, unit);
}
/**
* 调度 Callable 分布式任务。
*
* @param name 执行器名称
* @param task 任务
* @param delay 延迟时间
* @param unit 时间单位
* @param <T> 返回类型
* @return ScheduledFuture
*/
@Override
public <T> ScheduledFuture<T> schedule(String name, Callable<T> task, long delay, TimeUnit unit) {
checkKey(name);
Assert.notNull(task, "分布式定时任务不能为空");
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(delay >= 0, "延迟时间不能小于0");
return redissonClient.getExecutorService(name).schedule(task, delay, unit);
}
/**
* 固定频率调度 Runnable 分布式任务。
*
* @param name 执行器名称
* @param task 任务
* @param initialDelay 初始延迟
* @param period 执行周期
* @param unit 时间单位
* @return ScheduledFuture
*/
@Override
public ScheduledFuture<?> scheduleAtFixedRate(String name, Runnable task, long initialDelay, long period, TimeUnit unit) {
checkKey(name);
Assert.notNull(task, "分布式定时任务不能为空");
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(initialDelay >= 0, "初始延迟时间不能小于0");
Assert.isTrue(period > 0, "执行周期必须大于0");
return redissonClient.getExecutorService(name).scheduleAtFixedRate(task, initialDelay, period, unit);
}
/**
* 固定延迟调度 Runnable 分布式任务。
*
* @param name 执行器名称
* @param task 任务
* @param initialDelay 初始延迟
* @param delay 执行间隔
* @param unit 时间单位
* @return ScheduledFuture
*/
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(String name, Runnable task, long initialDelay, long delay, TimeUnit unit) {
checkKey(name);
Assert.notNull(task, "分布式定时任务不能为空");
Assert.notNull(unit, "时间单位不能为空");
Assert.isTrue(initialDelay >= 0, "初始延迟时间不能小于0");
Assert.isTrue(delay > 0, "执行间隔必须大于0");
return redissonClient.getExecutorService(name).scheduleWithFixedDelay(task, initialDelay, delay, unit);
}
// -------------------------------------------------------------------------
// RemoteService / LiveObject
// -------------------------------------------------------------------------
/**
* 获取远程服务。
*
* @return RRemoteService
*/
@Override
public RRemoteService getRemoteService() {
return redissonClient.getRemoteService();
}
/**
* 获取指定名称的远程服务。
*
* @param name 服务名称
* @return RRemoteService
*/
@Override
public RRemoteService getRemoteService(String name) {
checkKey(name);
return redissonClient.getRemoteService(name);
}
/**
* 注册远程服务实现。
*
* @param remoteInterface 远程服务接口
* @param implementation 远程服务实现
* @param <T> 服务类型
*/
@Override
public <T> void remoteRegister(Class<T> remoteInterface, T implementation) {
Assert.notNull(remoteInterface, "远程服务接口不能为空");
Assert.notNull(implementation, "远程服务实现不能为空");
redissonClient.getRemoteService().register(remoteInterface, implementation);
}
/**
* 注册远程服务实现并指定工作线程数量。
*
* @param remoteInterface 远程服务接口
* @param implementation 远程服务实现
* @param workers 工作线程数量
* @param <T> 服务类型
*/
@Override
public <T> void remoteRegister(Class<T> remoteInterface, T implementation, int workers) {
Assert.notNull(remoteInterface, "远程服务接口不能为空");
Assert.notNull(implementation, "远程服务实现不能为空");
Assert.isTrue(workers > 0, "远程服务工作线程数量必须大于0");
redissonClient.getRemoteService().register(remoteInterface, implementation, workers);
}
/**
* 获取远程服务代理。
*
* @param remoteInterface 远程服务接口
* @param <T> 服务类型
* @return 服务代理
*/
@Override
public <T> T remoteGet(Class<T> remoteInterface) {
Assert.notNull(remoteInterface, "远程服务接口不能为空");
return redissonClient.getRemoteService().get(remoteInterface);
}
/**
* 获取 LiveObject 服务。
*
* @return RLiveObjectService
*/
@Override
public RLiveObjectService getLiveObjectService() {
return redissonClient.getLiveObjectService();
}
/**
* 附加 LiveObject。
*
* @param detachedObject 游离对象
* @param <T> 对象类型
* @return LiveObject
*/
@Override
public <T> T liveObjectAttach(T detachedObject) {
Assert.notNull(detachedObject, "LiveObject游离对象不能为空");
return redissonClient.getLiveObjectService().attach(detachedObject);
}
/**
* 合并 LiveObject。
*
* @param detachedObject 游离对象
* @param <T> 对象类型
* @return LiveObject
*/
@Override
public <T> T liveObjectMerge(T detachedObject) {
Assert.notNull(detachedObject, "LiveObject游离对象不能为空");
return redissonClient.getLiveObjectService().merge(detachedObject);
}
/**
* 获取 LiveObject。
*
* @param entityClass 实体类型
* @param id 实体 ID
* @param <T> 对象类型
* @return LiveObject
*/
@Override
public <T> T liveObjectGet(Class<T> entityClass, Object id) {
Assert.notNull(entityClass, "LiveObject实体类型不能为空");
Assert.notNull(id, "LiveObject实体ID不能为空");
return redissonClient.getLiveObjectService().get(entityClass, id);
}
/**
* 删除 LiveObject。
*
* @param attachedObject 已附加对象
*/
@Override
public void liveObjectDelete(Object attachedObject) {
if (ObjectUtil.isNull(attachedObject)) {
return;
}
redissonClient.getLiveObjectService().delete(attachedObject);
}
/**
* 根据类型和 ID 删除 LiveObject。
*
* @param entityClass 实体类型
* @param id 实体 ID
*/
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public void liveObjectDelete(Class<?> entityClass, Object id) {
Assert.notNull(entityClass, "LiveObject实体类型不能为空");
Assert.notNull(id, "LiveObject实体ID不能为空");
redissonClient.getLiveObjectService().delete((Class) entityClass, id);
}
// -------------------------------------------------------------------------
// Object Listener / 对象监听
// -------------------------------------------------------------------------
/**
* 添加全局对象监听器。
*
* @param listener 对象监听器
* @return 监听器 ID
*/
@Override
public int addGlobalObjectListener(ObjectListener listener) {
Assert.notNull(listener, "全局对象监听器不能为空");
return redissonClient.getKeys().addListener(listener);
}
/**
* 移除全局对象监听器。
*
* @param listenerId 监听器 ID
*/
@Override
public void removeGlobalObjectListener(int listenerId) {
redissonClient.getKeys().removeListener(listenerId);
}
/**
* 添加 Bucket 对象监听器。
*
* @param key Redis 键
* @param listener 对象监听器
* @return 监听器 ID
*/
@Override
public int addBucketListener(String key, ObjectListener listener) {
checkKey(key);
Assert.notNull(listener, "Bucket监听器不能为空");
return redissonClient.getBucket(key).addListener(listener);
}
/**
* 移除 Bucket 对象监听器。
*
* @param key Redis 键
* @param listenerId 监听器 ID
*/
@Override
public void removeBucketListener(String key, int listenerId) {
if (StrUtil.isBlank(key)) {
return;
}
redissonClient.getBucket(key).removeListener(listenerId);
}
/**
* 添加 Map 对象监听器。
*
* @param key Redis 键
* @param listener 对象监听器
* @return 监听器 ID
*/
@Override
public int addMapListener(String key, ObjectListener listener) {
checkKey(key);
Assert.notNull(listener, "Map监听器不能为空");
return redissonClient.getMap(key).addListener(listener);
}
/**
* 添加 Map Entry 监听器。
*
* @param key Redis 键
* @param listener Entry 监听器
* @return 监听器 ID
*/
@Override
public int addMapEntryListener(String key, ObjectListener listener) {
checkKey(key);
Assert.notNull(listener, "Map Entry监听器不能为空");
return redissonClient.getMap(key).addListener(listener);
}
/**
* 移除 Map 监听器。
*
* @param key Redis 键
* @param listenerId 监听器 ID
*/
@Override
public void removeMapListener(String key, int listenerId) {
if (StrUtil.isBlank(key)) {
return;
}
redissonClient.getMap(key).removeListener(listenerId);
}
/**
* 添加 Queue 对象监听器。
*
* @param key Redis 键
* @param listener 对象监听器
* @return 监听器 ID
*/
@Override
public int addQueueListener(String key, ObjectListener listener) {
checkKey(key);
Assert.notNull(listener, "Queue监听器不能为空");
return redissonClient.getQueue(key).addListener(listener);
}
/**
* 移除 Queue 对象监听器。
*
* @param key Redis 键
* @param listenerId 监听器 ID
*/
@Override
public void removeQueueListener(String key, int listenerId) {
if (StrUtil.isBlank(key)) {
return;
}
redissonClient.getQueue(key).removeListener(listenerId);
}
/**
* 添加 Set 对象监听器。
*
* @param key Redis 键
* @param listener 对象监听器
* @return 监听器 ID
*/
@Override
public int addSetListener(String key, ObjectListener listener) {
checkKey(key);
Assert.notNull(listener, "Set监听器不能为空");
return redissonClient.getSet(key).addListener(listener);
}
/**
* 移除 Set 对象监听器。
*
* @param key Redis 键
* @param listenerId 监听器 ID
*/
@Override
public void removeSetListener(String key, int listenerId) {
if (StrUtil.isBlank(key)) {
return;
}
redissonClient.getSet(key).removeListener(listenerId);
}
// -------------------------------------------------------------------------
// 私有辅助方法
// -------------------------------------------------------------------------
/**
* 校验 Redis Key。
*
* @param key Redis 键
*/
private void checkKey(String key) {
Assert.isTrue(StrUtil.isNotBlank(key), "Redis Key 不能为空");
}
/**
* 校验正数时间。
*
* @param duration 时间
* @param message 异常消息
*/
private void checkPositiveDuration(Duration duration, String message) {
Assert.notNull(duration, message);
Assert.isTrue(!duration.isZero() && !duration.isNegative(), message);
}
/**
* 过滤空白 Redis Key。
*
* @param keys Redis 键数组
* @return 过滤后的 Redis 键数组
*/
private String[] filterKeys(String... keys) {
if (ArrayUtil.isEmpty(keys)) {
return new String[0];
}
return java.util.Arrays.stream(keys)
.filter(StrUtil::isNotBlank)
.distinct()
.toArray(String[]::new);
}
/**
* 校验哈希字段。
*
* @param field 字段名
*/
private void checkField(String field) {
Assert.isTrue(StrUtil.isNotBlank(field), "Redis Hash 字段不能为空");
}
/**
* 规范化列表索引,支持负数索引。
*
* @param index 原始索引
* @param size 列表长度
* @return 实际索引
*/
private int normalizeIndex(long index, int size) {
long realIndex = index < 0 ? size + index : index;
if (realIndex > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
if (realIndex < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return (int) realIndex;
}
/**
* 将集合元素转换为指定类型 Set。
*
* @param source 原始集合
* @param clazz 目标类型
* @param <T> 泛型类型
* @return 转换后的 Set
*/
private <T> Set<T> convertSet(Collection<?> source, Class<T> clazz) {
if (CollUtil.isEmpty(source) || ObjectUtil.isNull(clazz)) {
return Collections.emptySet();
}
Set<T> result = new LinkedHashSet<>(source.size());
for (Object item : source) {
T converted = convertValue(item, clazz);
if (ObjectUtil.isNotNull(converted)) {
result.add(converted);
}
}
return result;
}
/**
* 将 ScoredEntry 集合转换为有序 Map。
*
* @param entries ScoredEntry 集合
* @return 元素分数 Map
*/
private Map<Object, Double> scoredEntriesToMap(Collection<ScoredEntry<Object>> entries) {
if (CollUtil.isEmpty(entries)) {
return Collections.emptyMap();
}
Map<Object, Double> result = new LinkedHashMap<>(entries.size());
for (ScoredEntry<Object> entry : entries) {
result.put(entry.getValue(), entry.getScore());
}
return result;
}
/**
* 安全释放分布式锁。
*
* @param lockKey 锁 key
* @param lock 锁对象
* @param locked 是否已加锁
*/
private void unlockSafely(String lockKey, RLock lock, boolean locked) {
if (!locked || ObjectUtil.isNull(lock)) {
return;
}
if (!lock.isHeldByCurrentThread()) {
log.warn("当前线程未持有分布式锁,跳过释放,lockKey={}", lockKey);
return;
}
try {
lock.unlock();
} catch (Exception e) {
log.error("释放分布式锁异常,lockKey={}", lockKey, e);
}
}
/**
* 构建签到 Key。
*
* @param keyPrefix 业务 key 前缀
* @param userId 用户 ID
* @param year 年份
* @return 签到 Key
*/
private String buildSignKey(String keyPrefix, Object userId, int year) {
return StrUtil.format("{}:{}:{}", keyPrefix, userId, year);
}
/**
* 构建 UV Key。
*
* @param keyPrefix 业务 key 前缀
* @param bizKey 业务标识
* @param date 日期
* @return UV Key
*/
private String buildUvKey(String keyPrefix, String bizKey, LocalDate date) {
String dateText = date.format(DateTimeFormatter.ISO_LOCAL_DATE);
return StrUtil.format("{}:{}:{}", keyPrefix, bizKey, dateText);
}
/**
* 构建会话 Key。
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @return 会话 Key
*/
private String buildSessionKey(String keyPrefix, String token) {
return StrUtil.format("{}:{}", keyPrefix, token);
}
/**
* 校验 Geo 坐标。
*
* @param longitude 经度
* @param latitude 纬度
*/
private void checkGeoCoordinate(double longitude, double latitude) {
Assert.isTrue(longitude >= -180D && longitude <= 180D, "经度必须在 -180 到 180 之间");
Assert.isTrue(latitude >= -90D && latitude <= 90D, "纬度必须在 -90 到 90 之间");
}
/**
* 获取安全的 Lua KEYS 参数。
*
* @param keys 原始 KEYS
* @return 非空 KEYS
*/
private List<Object> safeScriptKeys(List<Object> keys) {
if (CollUtil.isEmpty(keys)) {
return Collections.emptyList();
}
return keys;
}
/**
* 校验 Stream 范围参数。
*
* @param startId 开始消息 ID
* @param endId 结束消息 ID
* @param count 数量
*/
private void checkStreamRangeArgs(StreamMessageId startId, StreamMessageId endId, int count) {
Assert.notNull(startId, "开始消息ID不能为空");
Assert.notNull(endId, "结束消息ID不能为空");
Assert.isTrue(count > 0, "读取数量必须大于0");
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
使用Redisson
基础能力测试控制器
用于演示 RedissonService 的 Key 管理、Bucket、计数器、分布式 ID 等基础能力。
package local.ateng.java.redis.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import local.ateng.java.redis.service.RedissonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.*;
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Redisson 基础能力测试控制器
* 用于演示 RedissonService 的 Key 管理、Bucket、计数器、分布式 ID 等基础能力。
*
* @author Ateng
* @since 2026-04-26
*/
@Slf4j
@RestController
@RequestMapping("/redisson/basic")
@RequiredArgsConstructor
public class RedissonBasicController {
private final RedissonService redissonService;
/**
* 获取 RedissonClient 基础信息。
* <p>
* curl "http://localhost:8080/redisson/basic/client"
*
* @return RedissonClient 信息
*/
@GetMapping("/client")
public Map<String, Object> client() {
RedissonClient client = redissonService.getClient();
Map<String, Object> data = new LinkedHashMap<>();
data.put("className", client.getClass().getName());
data.put("shutdown", client.isShutdown());
data.put("shuttingDown", client.isShuttingDown());
return ok(data);
}
/**
* 判断 Key 是否存在。
* <p>
* curl "http://localhost:8080/redisson/basic/key/exists?key=user:1"
*
* @param key Redis 键
* @return 是否存在
*/
@GetMapping("/key/exists")
public Map<String, Object> hasKey(@RequestParam String key) {
checkKey(key);
boolean exists = redissonService.hasKey(key);
return ok(exists);
}
/**
* 统计多个 Key 中存在的数量。
* <p>
* curl "http://localhost:8080/redisson/basic/key/count-exists?keys=user:1&keys=user:2"
*
* @param keys Redis 键集合
* @return 存在数量
*/
@GetMapping("/key/count-exists")
public Map<String, Object> countExists(@RequestParam List<String> keys) {
Assert.isTrue(CollUtil.isNotEmpty(keys), "keys不能为空");
long count = redissonService.countExists(keys.toArray(new String[0]));
return ok(count);
}
/**
* 删除指定 Key。
* <p>
* curl -X DELETE "http://localhost:8080/redisson/basic/key?key=user:1"
*
* @param key Redis 键
* @return 是否删除成功
*/
@DeleteMapping("/key")
public Map<String, Object> deleteKey(@RequestParam String key) {
checkKey(key);
boolean deleted = redissonService.deleteKey(key);
log.info("删除 Redis Key,key={},deleted={}", key, deleted);
return ok(deleted);
}
/**
* 批量删除 Key。
* <p>
* curl -X DELETE "http://localhost:8080/redisson/basic/keys?keys=user:1&keys=user:2"
*
* @param keys Redis 键集合
* @return 删除数量
*/
@DeleteMapping("/keys")
public Map<String, Object> deleteKeys(@RequestParam List<String> keys) {
Assert.isTrue(CollUtil.isNotEmpty(keys), "keys不能为空");
long count = redissonService.deleteKeys(keys);
log.info("批量删除 Redis Key,keys={},count={}", keys, count);
return ok(count);
}
/**
* 根据 pattern 删除 Key。
* <p>
* curl -X DELETE "http://localhost:8080/redisson/basic/keys/pattern?pattern=test:*"
*
* @param pattern 通配符表达式
* @return 删除数量
*/
@DeleteMapping("/keys/pattern")
public Map<String, Object> deleteByPattern(@RequestParam String pattern) {
checkKey(pattern);
long count = redissonService.deleteByPattern(pattern);
log.info("根据 pattern 删除 Redis Key,pattern={},count={}", pattern, count);
return ok(count);
}
/**
* 设置 Key 过期时间。
* <p>
* curl -X PUT "http://localhost:8080/redisson/basic/key/expire?key=user:1&timeout=60"
*
* @param key Redis 键
* @param timeout 过期时间,单位秒
* @return 是否设置成功
*/
@PutMapping("/key/expire")
public Map<String, Object> expire(@RequestParam String key,
@RequestParam Long timeout) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(timeout) && timeout > 0, "timeout必须大于0");
boolean success = redissonService.expire(key, timeout, TimeUnit.SECONDS);
return ok(success);
}
/**
* 获取 Key 剩余过期时间。
* <p>
* curl "http://localhost:8080/redisson/basic/key/ttl?key=user:1"
*
* @param key Redis 键
* @return 剩余秒数,-1 表示永久,-2 表示不存在
*/
@GetMapping("/key/ttl")
public Map<String, Object> ttl(@RequestParam String key) {
checkKey(key);
long ttl = redissonService.getTtl(key, TimeUnit.SECONDS);
return ok(ttl);
}
/**
* 移除 Key 过期时间。
* <p>
* curl -X PUT "http://localhost:8080/redisson/basic/key/persist?key=user:1"
*
* @param key Redis 键
* @return 是否成功
*/
@PutMapping("/key/persist")
public Map<String, Object> persist(@RequestParam String key) {
checkKey(key);
boolean success = redissonService.persist(key);
return ok(success);
}
/**
* 重命名 Key。
* <p>
* curl -X PUT "http://localhost:8080/redisson/basic/key/rename?oldKey=user:1&newKey=user:100"
*
* @param oldKey 旧 Key
* @param newKey 新 Key
* @return 是否成功
*/
@PutMapping("/key/rename")
public Map<String, Object> rename(@RequestParam String oldKey,
@RequestParam String newKey) {
checkKey(oldKey);
checkKey(newKey);
boolean success = redissonService.renameKey(oldKey, newKey);
return ok(success);
}
/**
* 新 Key 不存在时重命名。
* <p>
* curl -X PUT "http://localhost:8080/redisson/basic/key/rename-if-absent?oldKey=user:1&newKey=user:100"
*
* @param oldKey 旧 Key
* @param newKey 新 Key
* @return 是否成功
*/
@PutMapping("/key/rename-if-absent")
public Map<String, Object> renameIfAbsent(@RequestParam String oldKey,
@RequestParam String newKey) {
checkKey(oldKey);
checkKey(newKey);
boolean success = redissonService.renameKeyIfAbsent(oldKey, newKey);
return ok(success);
}
/**
* 查询匹配 pattern 的 Key。
* <p>
* curl "http://localhost:8080/redisson/basic/keys/scan?pattern=user:*&count=20"
*
* @param pattern 通配符表达式
* @param count 最大返回数量
* @return Key 集合
*/
@GetMapping("/keys/scan")
public Map<String, Object> scanKeys(@RequestParam String pattern,
@RequestParam(defaultValue = "20") Integer count) {
checkKey(pattern);
Assert.isTrue(ObjectUtil.isNotNull(count) && count > 0, "count必须大于0");
return ok(redissonService.scanKeys(pattern, count));
}
/**
* 获取 Key 类型。
* <p>
* curl "http://localhost:8080/redisson/basic/key/type?key=user:1"
*
* @param key Redis 键
* @return 类型
*/
@GetMapping("/key/type")
public Map<String, Object> keyType(@RequestParam String key) {
checkKey(key);
String type = redissonService.getKeyType(key);
return ok(type);
}
/**
* 设置 Bucket 值。
* <p>
* curl -X POST "http://localhost:8080/redisson/basic/bucket/set?key=user:1&ttlSeconds=300" \\
* -H "Content-Type: application/json" \\
* -d '{"id":1,"name":"Ateng"}'
*
* @param key Redis 键
* @param ttlSeconds 过期秒数,不传则永久
* @param value 值
* @return 执行结果
*/
@PostMapping("/bucket/set")
public Map<String, Object> set(@RequestParam String key,
@RequestParam(required = false) Long ttlSeconds,
@RequestBody Object value) {
checkKey(key);
if (ObjectUtil.isNotNull(ttlSeconds) && ttlSeconds > 0) {
redissonService.set(key, value, Duration.ofSeconds(ttlSeconds));
} else {
redissonService.set(key, value);
}
log.info("设置 Redis Bucket 成功,key={},ttlSeconds={}", key, ttlSeconds);
return ok(true);
}
/**
* 获取 Bucket 值。
* <p>
* curl "http://localhost:8080/redisson/basic/bucket/get?key=user:1"
*
* @param key Redis 键
* @return 值
*/
@GetMapping("/bucket/get")
public Map<String, Object> get(@RequestParam String key) {
checkKey(key);
Object value = redissonService.get(key, Object.class);
return ok(value);
}
/**
* Key 不存在时设置 Bucket 值。
* <p>
* curl -X POST "http://localhost:8080/redisson/basic/bucket/set-if-absent?key=lock:test&ttlSeconds=60" \\
* -H "Content-Type: application/json" \\
* -d '"value"'
*
* @param key Redis 键
* @param ttlSeconds 过期秒数
* @param value 值
* @return 是否设置成功
*/
@PostMapping("/bucket/set-if-absent")
public Map<String, Object> setIfAbsent(@RequestParam String key,
@RequestParam Long ttlSeconds,
@RequestBody Object value) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(ttlSeconds) && ttlSeconds > 0, "ttlSeconds必须大于0");
boolean success = redissonService.setIfAbsent(key, value, Duration.ofSeconds(ttlSeconds));
return ok(success);
}
/**
* Key 存在时设置 Bucket 值。
* <p>
* curl -X POST "http://localhost:8080/redisson/basic/bucket/set-if-exists?key=user:1&ttlSeconds=300" \\
* -H "Content-Type: application/json" \\
* -d '{"id":1,"name":"Ateng Updated"}'
*
* @param key Redis 键
* @param ttlSeconds 过期秒数,不传则不改过期时间
* @param value 值
* @return 是否设置成功
*/
@PostMapping("/bucket/set-if-exists")
public Map<String, Object> setIfExists(@RequestParam String key,
@RequestParam(required = false) Long ttlSeconds,
@RequestBody Object value) {
checkKey(key);
boolean success;
if (ObjectUtil.isNotNull(ttlSeconds) && ttlSeconds > 0) {
success = redissonService.setIfExists(key, value, Duration.ofSeconds(ttlSeconds));
} else {
success = redissonService.setIfExists(key, value);
}
return ok(success);
}
/**
* 原子替换 Bucket 值并返回旧值。
* <p>
* curl -X POST "http://localhost:8080/redisson/basic/bucket/get-and-set?key=user:1" \\
* -H "Content-Type: application/json" \\
* -d '{"id":1,"name":"new"}'
*
* @param key Redis 键
* @param value 新值
* @return 旧值
*/
@PostMapping("/bucket/get-and-set")
public Map<String, Object> getAndSet(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
Object oldValue = redissonService.getAndSet(key, value, Object.class);
return ok(oldValue);
}
/**
* 获取并删除 Bucket 值。
* <p>
* curl -X DELETE "http://localhost:8080/redisson/basic/bucket/get-and-delete?key=user:1"
*
* @param key Redis 键
* @return 删除前的值
*/
@DeleteMapping("/bucket/get-and-delete")
public Map<String, Object> getAndDelete(@RequestParam String key) {
checkKey(key);
Object oldValue = redissonService.getAndDelete(key, Object.class);
return ok(oldValue);
}
/**
* 批量获取 Bucket 值。
* <p>
* curl "http://localhost:8080/redisson/basic/bucket/entries?keys=user:1&keys=user:2"
*
* @param keys Redis 键集合
* @return Key-Value Map
*/
@GetMapping("/bucket/entries")
public Map<String, Object> entries(@RequestParam List<String> keys) {
Assert.isTrue(CollUtil.isNotEmpty(keys), "keys不能为空");
Map<String, Object> data = redissonService.entries(keys);
return ok(data);
}
/**
* 获取 Bucket 值大小。
* <p>
* curl "http://localhost:8080/redisson/basic/bucket/size?key=user:1"
*
* @param key Redis 键
* @return 字节大小
*/
@GetMapping("/bucket/size")
public Map<String, Object> size(@RequestParam String key) {
checkKey(key);
long size = redissonService.size(key);
return ok(size);
}
/**
* AtomicLong 自增。
* <p>
* curl -X POST "http://localhost:8080/redisson/basic/atomic-long/increment?key=count:like:1&delta=1"
*
* @param key Redis 键
* @param delta 增量
* @return 最新值
*/
@PostMapping("/atomic-long/increment")
public Map<String, Object> increment(@RequestParam String key,
@RequestParam(defaultValue = "1") Long delta) {
checkKey(key);
long value = redissonService.increment(key, delta);
return ok(value);
}
/**
* AtomicLong 自减。
* <p>
* curl -X POST "http://localhost:8080/redisson/basic/atomic-long/decrement?key=count:like:1&delta=1"
*
* @param key Redis 键
* @param delta 减量
* @return 最新值
*/
@PostMapping("/atomic-long/decrement")
public Map<String, Object> decrement(@RequestParam String key,
@RequestParam(defaultValue = "1") Long delta) {
checkKey(key);
long value = redissonService.decrement(key, delta);
return ok(value);
}
/**
* 设置 AtomicLong 值。
* <p>
* curl -X PUT "http://localhost:8080/redisson/basic/atomic-long/set?key=count:like:1&value=100"
*
* @param key Redis 键
* @param value 值
* @return 执行结果
*/
@PutMapping("/atomic-long/set")
public Map<String, Object> setAtomicLong(@RequestParam String key,
@RequestParam Long value) {
checkKey(key);
Assert.notNull(value, "value不能为空");
redissonService.setAtomicLong(key, value);
return ok(true);
}
/**
* 获取 AtomicLong 值。
* <p>
* curl "http://localhost:8080/redisson/basic/atomic-long/get?key=count:like:1"
*
* @param key Redis 键
* @return 当前值
*/
@GetMapping("/atomic-long/get")
public Map<String, Object> getAtomicLong(@RequestParam String key) {
checkKey(key);
long value = redissonService.getAtomicLongValue(key);
return ok(value);
}
/**
* 重置 AtomicLong 值。
* <p>
* curl -X PUT "http://localhost:8080/redisson/basic/atomic-long/reset?key=count:like:1"
*
* @param key Redis 键
* @return 执行结果
*/
@PutMapping("/atomic-long/reset")
public Map<String, Object> resetAtomicLong(@RequestParam String key) {
checkKey(key);
redissonService.resetAtomicLong(key);
return ok(true);
}
/**
* AtomicDouble 自增。
* <p>
* curl -X POST "http://localhost:8080/redisson/basic/atomic-double/increment?key=score:user:1&delta=1.5"
*
* @param key Redis 键
* @param delta 增量
* @return 最新值
*/
@PostMapping("/atomic-double/increment")
public Map<String, Object> incrementDouble(@RequestParam String key,
@RequestParam(defaultValue = "1") Double delta) {
checkKey(key);
double value = redissonService.incrementDouble(key, delta);
return ok(value);
}
/**
* AtomicDouble 自减。
* <p>
* curl -X POST "http://localhost:8080/redisson/basic/atomic-double/decrement?key=score:user:1&delta=1.5"
*
* @param key Redis 键
* @param delta 减量
* @return 最新值
*/
@PostMapping("/atomic-double/decrement")
public Map<String, Object> decrementDouble(@RequestParam String key,
@RequestParam(defaultValue = "1") Double delta) {
checkKey(key);
double value = redissonService.decrementDouble(key, delta);
return ok(value);
}
/**
* 初始化分布式 ID 生成器。
* <p>
* curl -X POST "http://localhost:8080/redisson/basic/id-generator/init?key=id:order&initialValue=1000&allocationSize=100"
*
* @param key Redis 键
* @param initialValue 初始值
* @param allocationSize 分配步长
* @return 是否初始化成功
*/
@PostMapping("/id-generator/init")
public Map<String, Object> idGeneratorInit(@RequestParam String key,
@RequestParam(defaultValue = "1") Long initialValue,
@RequestParam(defaultValue = "100") Long allocationSize) {
checkKey(key);
boolean success = redissonService.idGeneratorInit(key, initialValue, allocationSize);
return ok(success);
}
/**
* 获取下一个分布式 ID。
* <p>
* curl "http://localhost:8080/redisson/basic/id-generator/next?key=id:order"
*
* @param key Redis 键
* @return ID
*/
@GetMapping("/id-generator/next")
public Map<String, Object> nextId(@RequestParam String key) {
checkKey(key);
long id = redissonService.nextId(key);
return ok(id);
}
/**
* 校验 Redis Key。
*
* @param key Redis 键
*/
private void checkKey(String key) {
Assert.isTrue(StrUtil.isNotBlank(key), "Redis Key不能为空");
}
/**
* 成功响应。
*
* @param data 响应数据
* @return 响应 Map
*/
private Map<String, Object> ok(Object data) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("code", 0);
result.put("message", "操作成功");
result.put("data", data);
return result;
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
数据结构测试控制器
用于演示 RedissonService 的 Hash、MapCache、List、Deque、Set、SetCache、ZSet 等能力。
package local.ateng.java.redis.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import local.ateng.java.redis.service.RedissonService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.client.protocol.ScoredEntry;
import org.springframework.web.bind.annotation.*;
import java.time.Duration;
import java.util.*;
/**
* Redisson 数据结构测试控制器
* 用于演示 RedissonService 的 Hash、MapCache、List、Deque、Set、SetCache、ZSet 等能力。
*
* @author Ateng
* @since 2026-04-26
*/
@Slf4j
@RestController
@RequestMapping("/redisson/data")
@RequiredArgsConstructor
public class RedissonDataStructureController {
private final RedissonService redissonService;
// -------------------------------------------------------------------------
// Hash / Map
// -------------------------------------------------------------------------
/**
* 设置 Hash 字段值。
*
* curl -X POST "http://localhost:8080/redisson/data/hash/put?key=user:hash:1&field=name" \
* -H "Content-Type: application/json" \
* -d '"Ateng"'
*
* @param key Redis 键
* @param field 字段名
* @param value 字段值
* @return 执行结果
*/
@PostMapping("/hash/put")
public Map<String, Object> hPut(@RequestParam String key,
@RequestParam String field,
@RequestBody Object value) {
checkKey(key);
checkField(field);
redissonService.hPut(key, field, value);
log.info("Hash 字段写入成功,key={},field={}", key, field);
return ok(true);
}
/**
* 批量设置 Hash 字段值。
*
* curl -X POST "http://localhost:8080/redisson/data/hash/put-all?key=user:hash:1" \
* -H "Content-Type: application/json" \
* -d '{"name":"Ateng","age":18,"city":"Chongqing"}'
*
* @param key Redis 键
* @param map 字段 Map
* @return 执行结果
*/
@PostMapping("/hash/put-all")
public Map<String, Object> hPutAll(@RequestParam String key,
@RequestBody Map<String, Object> map) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(map), "字段Map不能为空");
redissonService.hPutAll(key, map);
log.info("Hash 字段批量写入成功,key={},size={}", key, map.size());
return ok(true);
}
/**
* 字段不存在时设置 Hash 字段值。
*
* curl -X POST "http://localhost:8080/redisson/data/hash/put-if-absent?key=user:hash:1&field=email" \
* -H "Content-Type: application/json" \
* -d '"ateng@example.com"'
*
* @param key Redis 键
* @param field 字段名
* @param value 字段值
* @return 是否设置成功
*/
@PostMapping("/hash/put-if-absent")
public Map<String, Object> hPutIfAbsent(@RequestParam String key,
@RequestParam String field,
@RequestBody Object value) {
checkKey(key);
checkField(field);
boolean success = redissonService.hPutIfAbsent(key, field, value);
return ok(success);
}
/**
* 获取 Hash 字段值。
*
* curl "http://localhost:8080/redisson/data/hash/get?key=user:hash:1&field=name"
*
* @param key Redis 键
* @param field 字段名
* @return 字段值
*/
@GetMapping("/hash/get")
public Map<String, Object> hGet(@RequestParam String key,
@RequestParam String field) {
checkKey(key);
checkField(field);
Object value = redissonService.hGet(key, field, Object.class);
return ok(value);
}
/**
* 批量获取 Hash 字段值。
*
* curl "http://localhost:8080/redisson/data/hash/multi-get?key=user:hash:1&fields=name&fields=age"
*
* @param key Redis 键
* @param fields 字段集合
* @return 字段值 Map
*/
@GetMapping("/hash/multi-get")
public Map<String, Object> hMultiGet(@RequestParam String key,
@RequestParam List<String> fields) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(fields), "fields不能为空");
Map<String, Object> data = redissonService.hMultiGet(key, fields);
return ok(data);
}
/**
* 删除 Hash 字段。
*
* curl -X DELETE "http://localhost:8080/redisson/data/hash/delete?key=user:hash:1&fields=name&fields=age"
*
* @param key Redis 键
* @param fields 字段集合
* @return 删除数量
*/
@DeleteMapping("/hash/delete")
public Map<String, Object> hDelete(@RequestParam String key,
@RequestParam List<String> fields) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(fields), "fields不能为空");
long count = redissonService.hDelete(key, fields.toArray(new String[0]));
log.info("Hash 字段删除成功,key={},fields={},count={}", key, fields, count);
return ok(count);
}
/**
* 判断 Hash 字段是否存在。
*
* curl "http://localhost:8080/redisson/data/hash/has-key?key=user:hash:1&field=name"
*
* @param key Redis 键
* @param field 字段名
* @return 是否存在
*/
@GetMapping("/hash/has-key")
public Map<String, Object> hHasKey(@RequestParam String key,
@RequestParam String field) {
checkKey(key);
checkField(field);
boolean exists = redissonService.hHasKey(key, field);
return ok(exists);
}
/**
* 获取 Hash 全部字段和值。
*
* curl "http://localhost:8080/redisson/data/hash/entries?key=user:hash:1"
*
* @param key Redis 键
* @return 字段值 Map
*/
@GetMapping("/hash/entries")
public Map<String, Object> hEntries(@RequestParam String key) {
checkKey(key);
Map<String, Object> data = redissonService.hEntries(key);
return ok(data);
}
/**
* 获取 Hash 全部字段名。
*
* curl "http://localhost:8080/redisson/data/hash/keys?key=user:hash:1"
*
* @param key Redis 键
* @return 字段名集合
*/
@GetMapping("/hash/keys")
public Map<String, Object> hKeys(@RequestParam String key) {
checkKey(key);
Set<String> data = redissonService.hKeys(key);
return ok(data);
}
/**
* 获取 Hash 全部字段值。
*
* curl "http://localhost:8080/redisson/data/hash/values?key=user:hash:1"
*
* @param key Redis 键
* @return 字段值集合
*/
@GetMapping("/hash/values")
public Map<String, Object> hValues(@RequestParam String key) {
checkKey(key);
Collection<Object> data = redissonService.hValues(key);
return ok(data);
}
/**
* 获取 Hash 字段数量。
*
* curl "http://localhost:8080/redisson/data/hash/size?key=user:hash:1"
*
* @param key Redis 键
* @return 字段数量
*/
@GetMapping("/hash/size")
public Map<String, Object> hSize(@RequestParam String key) {
checkKey(key);
int size = redissonService.hSize(key);
return ok(size);
}
/**
* Hash 字段整数自增。
*
* curl -X POST "http://localhost:8080/redisson/data/hash/increment?key=user:hash:1&field=count&delta=1"
*
* @param key Redis 键
* @param field 字段名
* @param delta 增量
* @return 最新值
*/
@PostMapping("/hash/increment")
public Map<String, Object> hIncrement(@RequestParam String key,
@RequestParam String field,
@RequestParam(defaultValue = "1") Long delta) {
checkKey(key);
checkField(field);
long value = redissonService.hIncrement(key, field, delta);
return ok(value);
}
/**
* Hash 字段浮点数自增。
*
* curl -X POST "http://localhost:8080/redisson/data/hash/increment-double?key=user:hash:1&field=score&delta=1.5"
*
* @param key Redis 键
* @param field 字段名
* @param delta 增量
* @return 最新值
*/
@PostMapping("/hash/increment-double")
public Map<String, Object> hIncrementDouble(@RequestParam String key,
@RequestParam String field,
@RequestParam(defaultValue = "1") Double delta) {
checkKey(key);
checkField(field);
double value = redissonService.hIncrementDouble(key, field, delta);
return ok(value);
}
/**
* 清空 Hash。
*
* curl -X DELETE "http://localhost:8080/redisson/data/hash/clear?key=user:hash:1"
*
* @param key Redis 键
* @return 执行结果
*/
@DeleteMapping("/hash/clear")
public Map<String, Object> hClear(@RequestParam String key) {
checkKey(key);
redissonService.hClear(key);
log.info("Hash 清空成功,key={}", key);
return ok(true);
}
// -------------------------------------------------------------------------
// MapCache
// -------------------------------------------------------------------------
/**
* 设置 MapCache 字段值。
*
* curl -X POST "http://localhost:8080/redisson/data/map-cache/put?key=user:cache:1&field=profile&ttlSeconds=300" \
* -H "Content-Type: application/json" \
* -d '{"name":"Ateng","age":18}'
*
* @param key Redis 键
* @param field 字段名
* @param ttlSeconds 字段 TTL 秒数
* @param maxIdleSeconds 最大空闲秒数,可选
* @param value 字段值
* @return 执行结果
*/
@PostMapping("/map-cache/put")
public Map<String, Object> hcPut(@RequestParam String key,
@RequestParam String field,
@RequestParam Long ttlSeconds,
@RequestParam(required = false) Long maxIdleSeconds,
@RequestBody Object value) {
checkKey(key);
checkField(field);
Assert.isTrue(ObjectUtil.isNotNull(ttlSeconds) && ttlSeconds > 0, "ttlSeconds必须大于0");
if (ObjectUtil.isNotNull(maxIdleSeconds) && maxIdleSeconds > 0) {
redissonService.hcPut(key, field, value, Duration.ofSeconds(ttlSeconds), Duration.ofSeconds(maxIdleSeconds));
} else {
redissonService.hcPut(key, field, value, Duration.ofSeconds(ttlSeconds));
}
log.info("MapCache 字段写入成功,key={},field={},ttlSeconds={},maxIdleSeconds={}",
key, field, ttlSeconds, maxIdleSeconds);
return ok(true);
}
/**
* 获取 MapCache 字段值。
*
* curl "http://localhost:8080/redisson/data/map-cache/get?key=user:cache:1&field=profile"
*
* @param key Redis 键
* @param field 字段名
* @return 字段值
*/
@GetMapping("/map-cache/get")
public Map<String, Object> hcGet(@RequestParam String key,
@RequestParam String field) {
checkKey(key);
checkField(field);
Object value = redissonService.hcGet(key, field, Object.class);
return ok(value);
}
/**
* 删除 MapCache 字段。
*
* curl -X DELETE "http://localhost:8080/redisson/data/map-cache/delete?key=user:cache:1&fields=profile"
*
* @param key Redis 键
* @param fields 字段集合
* @return 删除数量
*/
@DeleteMapping("/map-cache/delete")
public Map<String, Object> hcDelete(@RequestParam String key,
@RequestParam List<String> fields) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(fields), "fields不能为空");
long count = redissonService.hcDelete(key, fields.toArray(new String[0]));
return ok(count);
}
// -------------------------------------------------------------------------
// List / Deque
// -------------------------------------------------------------------------
/**
* 从左侧压入列表。
*
* curl -X POST "http://localhost:8080/redisson/data/list/left-push?key=list:test" \
* -H "Content-Type: application/json" \
* -d '"A"'
*
* @param key Redis 键
* @param value 元素
* @return 执行结果
*/
@PostMapping("/list/left-push")
public Map<String, Object> lLeftPush(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
redissonService.lLeftPush(key, value);
return ok(true);
}
/**
* 从右侧压入列表。
*
* curl -X POST "http://localhost:8080/redisson/data/list/right-push?key=list:test" \
* -H "Content-Type: application/json" \
* -d '"B"'
*
* @param key Redis 键
* @param value 元素
* @return 执行结果
*/
@PostMapping("/list/right-push")
public Map<String, Object> lRightPush(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
redissonService.lRightPush(key, value);
return ok(true);
}
/**
* 批量从右侧压入列表。
*
* curl -X POST "http://localhost:8080/redisson/data/list/right-push-all?key=list:test" \
* -H "Content-Type: application/json" \
* -d '["A","B","C"]'
*
* @param key Redis 键
* @param values 元素集合
* @return 执行结果
*/
@PostMapping("/list/right-push-all")
public Map<String, Object> lRightPushAll(@RequestParam String key,
@RequestBody List<Object> values) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(values), "values不能为空");
redissonService.lRightPushAll(key, values);
return ok(true);
}
/**
* 从左侧弹出列表元素。
*
* curl -X POST "http://localhost:8080/redisson/data/list/left-pop?key=list:test"
*
* @param key Redis 键
* @return 弹出的元素
*/
@PostMapping("/list/left-pop")
public Map<String, Object> lLeftPop(@RequestParam String key) {
checkKey(key);
Object value = redissonService.lLeftPop(key);
return ok(value);
}
/**
* 从右侧弹出列表元素。
*
* curl -X POST "http://localhost:8080/redisson/data/list/right-pop?key=list:test"
*
* @param key Redis 键
* @return 弹出的元素
*/
@PostMapping("/list/right-pop")
public Map<String, Object> lRightPop(@RequestParam String key) {
checkKey(key);
Object value = redissonService.lRightPop(key);
return ok(value);
}
/**
* 获取列表范围。
*
* curl "http://localhost:8080/redisson/data/list/range?key=list:test&start=0&end=-1"
*
* @param key Redis 键
* @param start 开始索引
* @param end 结束索引
* @return 元素列表
*/
@GetMapping("/list/range")
public Map<String, Object> lRange(@RequestParam String key,
@RequestParam(defaultValue = "0") Long start,
@RequestParam(defaultValue = "-1") Long end) {
checkKey(key);
List<Object> data = redissonService.lRange(key, start, end);
return ok(data);
}
/**
* 获取列表长度。
*
* curl "http://localhost:8080/redisson/data/list/size?key=list:test"
*
* @param key Redis 键
* @return 长度
*/
@GetMapping("/list/size")
public Map<String, Object> lSize(@RequestParam String key) {
checkKey(key);
long size = redissonService.lSize(key);
return ok(size);
}
/**
* 删除列表元素。
*
* curl -X POST "http://localhost:8080/redisson/data/list/remove?key=list:test&count=0" \
* -H "Content-Type: application/json" \
* -d '"A"'
*
* @param key Redis 键
* @param count 删除数量规则
* @param value 元素
* @return 删除数量
*/
@PostMapping("/list/remove")
public Map<String, Object> lRemove(@RequestParam String key,
@RequestParam(defaultValue = "0") Long count,
@RequestBody Object value) {
checkKey(key);
long removed = redissonService.lRemove(key, count, value);
return ok(removed);
}
/**
* 获取列表指定索引元素。
*
* curl "http://localhost:8080/redisson/data/list/index?key=list:test&index=0"
*
* @param key Redis 键
* @param index 索引
* @return 元素
*/
@GetMapping("/list/index")
public Map<String, Object> lIndex(@RequestParam String key,
@RequestParam Long index) {
checkKey(key);
Assert.notNull(index, "index不能为空");
Object value = redissonService.lIndex(key, index);
return ok(value);
}
/**
* 设置列表指定索引元素。
*
* curl -X PUT "http://localhost:8080/redisson/data/list/set?key=list:test&index=0" \
* -H "Content-Type: application/json" \
* -d '"NEW"'
*
* @param key Redis 键
* @param index 索引
* @param value 元素
* @return 执行结果
*/
@PutMapping("/list/set")
public Map<String, Object> lSet(@RequestParam String key,
@RequestParam Long index,
@RequestBody Object value) {
checkKey(key);
Assert.notNull(index, "index不能为空");
redissonService.lSet(key, index, value);
return ok(true);
}
/**
* 裁剪列表范围。
*
* curl -X PUT "http://localhost:8080/redisson/data/list/trim?key=list:test&start=0&end=9"
*
* @param key Redis 键
* @param start 开始索引
* @param end 结束索引
* @return 执行结果
*/
@PutMapping("/list/trim")
public Map<String, Object> lTrim(@RequestParam String key,
@RequestParam Integer start,
@RequestParam Integer end) {
checkKey(key);
Assert.notNull(start, "start不能为空");
Assert.notNull(end, "end不能为空");
redissonService.lTrim(key, start, end);
return ok(true);
}
/**
* 清空列表。
*
* curl -X DELETE "http://localhost:8080/redisson/data/list/clear?key=list:test"
*
* @param key Redis 键
* @return 执行结果
*/
@DeleteMapping("/list/clear")
public Map<String, Object> lClear(@RequestParam String key) {
checkKey(key);
redissonService.lClear(key);
return ok(true);
}
// -------------------------------------------------------------------------
// Set / SetCache
// -------------------------------------------------------------------------
/**
* 添加集合元素。
*
* curl -X POST "http://localhost:8080/redisson/data/set/add?key=set:test" \
* -H "Content-Type: application/json" \
* -d '["A","B","C"]'
*
* @param key Redis 键
* @param values 元素集合
* @return 是否有新增
*/
@PostMapping("/set/add")
public Map<String, Object> sAdd(@RequestParam String key,
@RequestBody List<Object> values) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(values), "values不能为空");
boolean success = redissonService.sAdd(key, values);
return ok(success);
}
/**
* 添加 SetCache 元素。
*
* curl -X POST "http://localhost:8080/redisson/data/set-cache/add?key=set:cache:test&ttlSeconds=60" \
* -H "Content-Type: application/json" \
* -d '"A"'
*
* @param key Redis 键
* @param ttlSeconds 元素 TTL 秒数
* @param value 元素
* @return 是否添加成功
*/
@PostMapping("/set-cache/add")
public Map<String, Object> scAdd(@RequestParam String key,
@RequestParam Long ttlSeconds,
@RequestBody Object value) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(ttlSeconds) && ttlSeconds > 0, "ttlSeconds必须大于0");
boolean success = redissonService.scAdd(key, value, Duration.ofSeconds(ttlSeconds));
return ok(success);
}
/**
* 判断集合是否包含元素。
*
* curl -X POST "http://localhost:8080/redisson/data/set/is-member?key=set:test" \
* -H "Content-Type: application/json" \
* -d '"A"'
*
* @param key Redis 键
* @param value 元素
* @return 是否包含
*/
@PostMapping("/set/is-member")
public Map<String, Object> sIsMember(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
boolean exists = redissonService.sIsMember(key, value);
return ok(exists);
}
/**
* 获取集合全部元素。
*
* curl "http://localhost:8080/redisson/data/set/members?key=set:test"
*
* @param key Redis 键
* @return 元素集合
*/
@GetMapping("/set/members")
public Map<String, Object> sMembers(@RequestParam String key) {
checkKey(key);
Set<Object> data = redissonService.sMembers(key);
return ok(data);
}
/**
* 获取集合大小。
*
* curl "http://localhost:8080/redisson/data/set/size?key=set:test"
*
* @param key Redis 键
* @return 集合大小
*/
@GetMapping("/set/size")
public Map<String, Object> sSize(@RequestParam String key) {
checkKey(key);
long size = redissonService.sSize(key);
return ok(size);
}
/**
* 随机弹出集合元素。
*
* curl -X POST "http://localhost:8080/redisson/data/set/pop?key=set:test"
*
* @param key Redis 键
* @return 元素
*/
@PostMapping("/set/pop")
public Map<String, Object> sPop(@RequestParam String key) {
checkKey(key);
Object value = redissonService.sPop(key);
return ok(value);
}
/**
* 删除集合元素。
*
* curl -X POST "http://localhost:8080/redisson/data/set/remove?key=set:test" \
* -H "Content-Type: application/json" \
* -d '["A","B"]'
*
* @param key Redis 键
* @param values 元素集合
* @return 是否删除成功
*/
@PostMapping("/set/remove")
public Map<String, Object> sRemove(@RequestParam String key,
@RequestBody List<Object> values) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(values), "values不能为空");
boolean success = redissonService.sRemove(key, values.toArray());
return ok(success);
}
/**
* 随机获取集合元素但不删除。
*
* curl "http://localhost:8080/redisson/data/set/random-member?key=set:test"
*
* @param key Redis 键
* @return 元素
*/
@GetMapping("/set/random-member")
public Map<String, Object> sRandomMember(@RequestParam String key) {
checkKey(key);
Object value = redissonService.sRandomMember(key);
return ok(value);
}
/**
* 随机获取多个集合元素。
*
* curl "http://localhost:8080/redisson/data/set/random-members?key=set:test&count=2"
*
* @param key Redis 键
* @param count 数量
* @return 元素集合
*/
@GetMapping("/set/random-members")
public Map<String, Object> sRandomMembers(@RequestParam String key,
@RequestParam Integer count) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(count) && count > 0, "count必须大于0");
Set<Object> data = redissonService.sRandomMembers(key, count);
return ok(data);
}
/**
* 获取两个集合的并集。
*
* curl "http://localhost:8080/redisson/data/set/union?key1=set:a&key2=set:b"
*
* @param key1 第一个 Key
* @param key2 第二个 Key
* @return 并集
*/
@GetMapping("/set/union")
public Map<String, Object> sUnion(@RequestParam String key1,
@RequestParam String key2) {
checkKey(key1);
checkKey(key2);
Set<Object> data = redissonService.sUnion(key1, key2);
return ok(data);
}
/**
* 获取两个集合的交集。
*
* curl "http://localhost:8080/redisson/data/set/intersect?key1=set:a&key2=set:b"
*
* @param key1 第一个 Key
* @param key2 第二个 Key
* @return 交集
*/
@GetMapping("/set/intersect")
public Map<String, Object> sIntersect(@RequestParam String key1,
@RequestParam String key2) {
checkKey(key1);
checkKey(key2);
Set<Object> data = redissonService.sIntersect(key1, key2);
return ok(data);
}
/**
* 获取两个集合的差集。
*
* curl "http://localhost:8080/redisson/data/set/difference?key1=set:a&key2=set:b"
*
* @param key1 第一个 Key
* @param key2 第二个 Key
* @return 差集
*/
@GetMapping("/set/difference")
public Map<String, Object> sDifference(@RequestParam String key1,
@RequestParam String key2) {
checkKey(key1);
checkKey(key2);
Set<Object> data = redissonService.sDifference(key1, key2);
return ok(data);
}
/**
* 并集存储。
*
* curl -X POST "http://localhost:8080/redisson/data/set/union-store?destKey=set:dest&keys=set:a&keys=set:b"
*
* @param destKey 目标 Key
* @param keys 源 Key 集合
* @return 存储数量
*/
@PostMapping("/set/union-store")
public Map<String, Object> sUnionStore(@RequestParam String destKey,
@RequestParam List<String> keys) {
checkKey(destKey);
Assert.isTrue(CollUtil.isNotEmpty(keys), "keys不能为空");
long count = redissonService.sUnionStore(destKey, keys.toArray(new String[0]));
return ok(count);
}
/**
* 交集存储。
*
* curl -X POST "http://localhost:8080/redisson/data/set/intersect-store?destKey=set:dest&keys=set:a&keys=set:b"
*
* @param destKey 目标 Key
* @param keys 源 Key 集合
* @return 存储数量
*/
@PostMapping("/set/intersect-store")
public Map<String, Object> sIntersectStore(@RequestParam String destKey,
@RequestParam List<String> keys) {
checkKey(destKey);
Assert.isTrue(CollUtil.isNotEmpty(keys), "keys不能为空");
long count = redissonService.sIntersectStore(destKey, keys.toArray(new String[0]));
return ok(count);
}
/**
* 差集存储。
*
* curl -X POST "http://localhost:8080/redisson/data/set/difference-store?destKey=set:dest&keys=set:a&keys=set:b"
*
* @param destKey 目标 Key
* @param keys 源 Key 集合
* @return 存储数量
*/
@PostMapping("/set/difference-store")
public Map<String, Object> sDifferenceStore(@RequestParam String destKey,
@RequestParam List<String> keys) {
checkKey(destKey);
Assert.isTrue(CollUtil.isNotEmpty(keys), "keys不能为空");
long count = redissonService.sDifferenceStore(destKey, keys.toArray(new String[0]));
return ok(count);
}
// -------------------------------------------------------------------------
// ZSet / 排行榜
// -------------------------------------------------------------------------
/**
* 添加 ZSet 元素。
*
* curl -X POST "http://localhost:8080/redisson/data/zset/add?key=rank:score" \
* -H "Content-Type: application/json" \
* -d '{"value":"user:1","score":100}'
*
* @param key Redis 键
* @param request 请求体
* @return 是否新增
*/
@PostMapping("/zset/add")
public Map<String, Object> zAdd(@RequestParam String key,
@RequestBody ZSetValueScoreRequest request) {
checkKey(key);
Assert.notNull(request, "请求体不能为空");
Assert.notNull(request.getValue(), "value不能为空");
Assert.notNull(request.getScore(), "score不能为空");
boolean success = redissonService.zAdd(key, request.getValue(), request.getScore());
return ok(success);
}
/**
* 批量添加 ZSet 元素。
*
* curl -X POST "http://localhost:8080/redisson/data/zset/add-all?key=rank:score" \
* -H "Content-Type: application/json" \
* -d '{"scoreMap":{"user:1":100,"user:2":90}}'
*
* @param key Redis 键
* @param request 请求体
* @return 新增数量
*/
@PostMapping("/zset/add-all")
public Map<String, Object> zAddAll(@RequestParam String key,
@RequestBody ZSetAddAllRequest request) {
checkKey(key);
Assert.notNull(request, "请求体不能为空");
Assert.isTrue(CollUtil.isNotEmpty(request.getScoreMap()), "scoreMap不能为空");
int count = redissonService.zAddAll(key, request.getScoreMap());
return ok(count);
}
/**
* 删除 ZSet 元素。
*
* curl -X POST "http://localhost:8080/redisson/data/zset/remove?key=rank:score" \
* -H "Content-Type: application/json" \
* -d '["user:1","user:2"]'
*
* @param key Redis 键
* @param values 元素集合
* @return 是否删除成功
*/
@PostMapping("/zset/remove")
public Map<String, Object> zRemove(@RequestParam String key,
@RequestBody List<Object> values) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(values), "values不能为空");
boolean success = redissonService.zRemove(key, values.toArray());
return ok(success);
}
/**
* 获取 ZSet 元素分数。
*
* curl -X POST "http://localhost:8080/redisson/data/zset/score?key=rank:score" \
* -H "Content-Type: application/json" \
* -d '"user:1"'
*
* @param key Redis 键
* @param value 元素
* @return 分数
*/
@PostMapping("/zset/score")
public Map<String, Object> zScore(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
Double score = redissonService.zScore(key, value);
return ok(score);
}
/**
* 获取 ZSet 升序排名。
*
* curl -X POST "http://localhost:8080/redisson/data/zset/rank?key=rank:score" \
* -H "Content-Type: application/json" \
* -d '"user:1"'
*
* @param key Redis 键
* @param value 元素
* @return 排名
*/
@PostMapping("/zset/rank")
public Map<String, Object> zRank(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
Integer rank = redissonService.zRank(key, value);
return ok(rank);
}
/**
* 获取 ZSet 降序排名。
*
* curl -X POST "http://localhost:8080/redisson/data/zset/rev-rank?key=rank:score" \
* -H "Content-Type: application/json" \
* -d '"user:1"'
*
* @param key Redis 键
* @param value 元素
* @return 排名
*/
@PostMapping("/zset/rev-rank")
public Map<String, Object> zRevRank(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
Integer rank = redissonService.zRevRank(key, value);
return ok(rank);
}
/**
* 获取 ZSet 分数区间元素。
*
* curl "http://localhost:8080/redisson/data/zset/range-by-score?key=rank:score&min=0&max=100"
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 元素集合
*/
@GetMapping("/zset/range-by-score")
public Map<String, Object> zRangeByScore(@RequestParam String key,
@RequestParam Double min,
@RequestParam Double max) {
checkKey(key);
Assert.notNull(min, "min不能为空");
Assert.notNull(max, "max不能为空");
Set<Object> data = redissonService.zRangeByScore(key, min, max);
return ok(data);
}
/**
* 获取 ZSet 分数区间元素并分页。
*
* curl "http://localhost:8080/redisson/data/zset/range-by-score-page?key=rank:score&min=0&max=100&offset=0&count=10"
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @param offset 偏移量
* @param count 数量
* @return 元素集合
*/
@GetMapping("/zset/range-by-score-page")
public Map<String, Object> zRangeByScorePage(@RequestParam String key,
@RequestParam Double min,
@RequestParam Double max,
@RequestParam(defaultValue = "0") Integer offset,
@RequestParam(defaultValue = "10") Integer count) {
checkKey(key);
Collection<Object> data = redissonService.zRangeByScore(key, min, max, offset, count);
return ok(data);
}
/**
* 获取 ZSet 分数区间元素及分数。
*
* curl "http://localhost:8080/redisson/data/zset/range-by-score-with-scores?key=rank:score&min=0&max=100"
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 元素分数 Map
*/
@GetMapping("/zset/range-by-score-with-scores")
public Map<String, Object> zRangeByScoreWithScores(@RequestParam String key,
@RequestParam Double min,
@RequestParam Double max) {
checkKey(key);
Map<Object, Double> data = redissonService.zRangeByScoreWithScores(key, min, max);
return ok(data);
}
/**
* 获取 ZSet 降序分数区间元素及分数。
*
* curl "http://localhost:8080/redisson/data/zset/rev-range-by-score-with-scores?key=rank:score&min=0&max=100"
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 元素分数 Map
*/
@GetMapping("/zset/rev-range-by-score-with-scores")
public Map<String, Object> zRevRangeByScoreWithScores(@RequestParam String key,
@RequestParam Double min,
@RequestParam Double max) {
checkKey(key);
Map<Object, Double> data = redissonService.zRevRangeByScoreWithScores(key, min, max);
return ok(data);
}
/**
* 获取 ZSet 排名区间元素。
*
* curl "http://localhost:8080/redisson/data/zset/range?key=rank:score&start=0&end=9"
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 元素集合
*/
@GetMapping("/zset/range")
public Map<String, Object> zRange(@RequestParam String key,
@RequestParam(defaultValue = "0") Integer start,
@RequestParam(defaultValue = "9") Integer end) {
checkKey(key);
Set<Object> data = redissonService.zRange(key, start, end);
return ok(data);
}
/**
* 获取 ZSet 排名区间元素及分数。
*
* curl "http://localhost:8080/redisson/data/zset/range-with-scores?key=rank:score&start=0&end=9"
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 元素分数 Map
*/
@GetMapping("/zset/range-with-scores")
public Map<String, Object> zRangeWithScores(@RequestParam String key,
@RequestParam(defaultValue = "0") Integer start,
@RequestParam(defaultValue = "9") Integer end) {
checkKey(key);
Map<Object, Double> data = redissonService.zRangeWithScores(key, start, end);
return ok(data);
}
/**
* 获取 ZSet 降序排名区间元素。
*
* curl "http://localhost:8080/redisson/data/zset/rev-range?key=rank:score&start=0&end=9"
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 元素集合
*/
@GetMapping("/zset/rev-range")
public Map<String, Object> zRevRange(@RequestParam String key,
@RequestParam(defaultValue = "0") Integer start,
@RequestParam(defaultValue = "9") Integer end) {
checkKey(key);
Set<Object> data = redissonService.zRevRange(key, start, end);
return ok(data);
}
/**
* 获取 ZSet 降序排名区间元素及分数。
*
* curl "http://localhost:8080/redisson/data/zset/rev-range-with-scores?key=rank:score&start=0&end=9"
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 元素分数 Map
*/
@GetMapping("/zset/rev-range-with-scores")
public Map<String, Object> zRevRangeWithScores(@RequestParam String key,
@RequestParam(defaultValue = "0") Integer start,
@RequestParam(defaultValue = "9") Integer end) {
checkKey(key);
Map<Object, Double> data = redissonService.zRevRangeWithScores(key, start, end);
return ok(data);
}
/**
* ZSet 元素分数自增。
*
* curl -X POST "http://localhost:8080/redisson/data/zset/increment-score?key=rank:score" \
* -H "Content-Type: application/json" \
* -d '{"value":"user:1","score":10}'
*
* @param key Redis 键
* @param request 请求体
* @return 最新分数
*/
@PostMapping("/zset/increment-score")
public Map<String, Object> zIncrBy(@RequestParam String key,
@RequestBody ZSetValueScoreRequest request) {
checkKey(key);
Assert.notNull(request, "请求体不能为空");
Assert.notNull(request.getValue(), "value不能为空");
Assert.notNull(request.getScore(), "score不能为空");
Double score = redissonService.zIncrBy(key, request.getValue(), request.getScore());
return ok(score);
}
/**
* 获取 ZSet 元素数量。
*
* curl "http://localhost:8080/redisson/data/zset/card?key=rank:score"
*
* @param key Redis 键
* @return 数量
*/
@GetMapping("/zset/card")
public Map<String, Object> zCard(@RequestParam String key) {
checkKey(key);
int count = redissonService.zCard(key);
return ok(count);
}
/**
* 获取 ZSet 指定分数区间元素数量。
*
* curl "http://localhost:8080/redisson/data/zset/count?key=rank:score&min=0&max=100"
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 数量
*/
@GetMapping("/zset/count")
public Map<String, Object> zCount(@RequestParam String key,
@RequestParam Double min,
@RequestParam Double max) {
checkKey(key);
long count = redissonService.zCount(key, min, max);
return ok(count);
}
/**
* 删除 ZSet 指定分数区间元素。
*
* curl -X DELETE "http://localhost:8080/redisson/data/zset/remove-range-by-score?key=rank:score&min=0&max=10"
*
* @param key Redis 键
* @param min 最小分数
* @param max 最大分数
* @return 删除数量
*/
@DeleteMapping("/zset/remove-range-by-score")
public Map<String, Object> zRemoveRangeByScore(@RequestParam String key,
@RequestParam Double min,
@RequestParam Double max) {
checkKey(key);
long count = redissonService.zRemoveRangeByScore(key, min, max);
return ok(count);
}
/**
* 删除 ZSet 指定排名区间元素。
*
* curl -X DELETE "http://localhost:8080/redisson/data/zset/remove-range-by-rank?key=rank:score&start=0&end=9"
*
* @param key Redis 键
* @param start 开始排名
* @param end 结束排名
* @return 删除数量
*/
@DeleteMapping("/zset/remove-range-by-rank")
public Map<String, Object> zRemoveRangeByRank(@RequestParam String key,
@RequestParam Integer start,
@RequestParam Integer end) {
checkKey(key);
long count = redissonService.zRemoveRangeByRank(key, start, end);
return ok(count);
}
/**
* 弹出 ZSet 分数最小元素。
*
* curl -X POST "http://localhost:8080/redisson/data/zset/pop-first?key=rank:score"
*
* @param key Redis 键
* @return 元素和分数
*/
@PostMapping("/zset/pop-first")
public Map<String, Object> zPopFirst(@RequestParam String key) {
checkKey(key);
ScoredEntry<Object> entry = redissonService.zPopFirst(key);
return ok(scoredEntryToMap(entry));
}
/**
* 弹出 ZSet 分数最大元素。
*
* curl -X POST "http://localhost:8080/redisson/data/zset/pop-last?key=rank:score"
*
* @param key Redis 键
* @return 元素和分数
*/
@PostMapping("/zset/pop-last")
public Map<String, Object> zPopLast(@RequestParam String key) {
checkKey(key);
ScoredEntry<Object> entry = redissonService.zPopLast(key);
return ok(scoredEntryToMap(entry));
}
/**
* 校验 Redis Key。
*
* @param key Redis 键
*/
private void checkKey(String key) {
Assert.isTrue(StrUtil.isNotBlank(key), "Redis Key不能为空");
}
/**
* 校验字段名。
*
* @param field 字段名
*/
private void checkField(String field) {
Assert.isTrue(StrUtil.isNotBlank(field), "字段名不能为空");
}
/**
* 成功响应。
*
* @param data 响应数据
* @return 响应 Map
*/
private Map<String, Object> ok(Object data) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("code", 0);
result.put("message", "操作成功");
result.put("data", data);
return result;
}
/**
* 转换 ScoredEntry。
*
* @param entry ScoredEntry
* @return Map
*/
private Map<String, Object> scoredEntryToMap(ScoredEntry<Object> entry) {
if (ObjectUtil.isNull(entry)) {
return null;
}
Map<String, Object> result = new LinkedHashMap<>();
result.put("value", entry.getValue());
result.put("score", entry.getScore());
return result;
}
/**
* ZSet 元素分数请求体。
*
* @author Ateng
* @since 2026-04-26
*/
@Data
public static class ZSetValueScoreRequest {
/**
* 元素值。
*/
private Object value;
/**
* 分数。
*/
private Double score;
}
/**
* ZSet 批量添加请求体。
*
* @author Ateng
* @since 2026-04-26
*/
@Data
public static class ZSetAddAllRequest {
/**
* 元素分数 Map。
*/
private Map<Object, Double> scoreMap;
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
分布式能力测试控制器
用于演示 RedissonService 的分布式锁、读写锁、闭锁、信号量、限流器、布隆过滤器等能力。
package local.ateng.java.redis.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import local.ateng.java.redis.service.RedissonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RLock;
import org.redisson.api.RateIntervalUnit;
import org.redisson.api.RateType;
import org.springframework.web.bind.annotation.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Redisson 分布式能力测试控制器
* 用于演示 RedissonService 的分布式锁、读写锁、闭锁、信号量、限流器、布隆过滤器等能力。
*
* @author Ateng
* @since 2026-04-26
*/
@Slf4j
@RestController
@RequestMapping("/redisson/distributed")
@RequiredArgsConstructor
public class RedissonDistributedController {
private final RedissonService redissonService;
// -------------------------------------------------------------------------
// Lock / FairLock / SpinLock
// -------------------------------------------------------------------------
/**
* 执行带可重入锁的业务。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/lock/execute?lockKey=lock:demo&businessMillis=1000"
*
* @param lockKey 锁 Key
* @param businessMillis 模拟业务耗时,单位毫秒
* @return 执行结果
*/
@PostMapping("/lock/execute")
public Map<String, Object> executeWithLock(@RequestParam String lockKey,
@RequestParam(defaultValue = "1000") Long businessMillis) {
checkKey(lockKey);
checkBusinessMillis(businessMillis);
redissonService.executeWithLock(lockKey, () -> {
log.info("执行可重入锁保护的业务,lockKey={},businessMillis={}", lockKey, businessMillis);
ThreadUtil.sleep(businessMillis);
});
return ok("可重入锁业务执行完成");
}
/**
* 尝试执行带可重入锁的业务。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/lock/try-execute?lockKey=lock:demo&waitSeconds=3&leaseSeconds=10&businessMillis=1000"
*
* @param lockKey 锁 Key
* @param waitSeconds 最大等待秒数
* @param leaseSeconds 锁持有秒数,传 0 或负数表示使用 watchdog
* @param businessMillis 模拟业务耗时,单位毫秒
* @return 是否执行成功
*/
@PostMapping("/lock/try-execute")
public Map<String, Object> tryExecuteWithLock(@RequestParam String lockKey,
@RequestParam(defaultValue = "3") Long waitSeconds,
@RequestParam(defaultValue = "10") Long leaseSeconds,
@RequestParam(defaultValue = "1000") Long businessMillis) {
checkKey(lockKey);
Assert.isTrue(ObjectUtil.isNotNull(waitSeconds) && waitSeconds >= 0, "waitSeconds不能小于0");
Assert.notNull(leaseSeconds, "leaseSeconds不能为空");
checkBusinessMillis(businessMillis);
boolean success = redissonService.tryExecuteWithLock(
lockKey,
waitSeconds,
leaseSeconds,
TimeUnit.SECONDS,
() -> {
log.info("执行可重入锁保护的业务,lockKey={},businessMillis={}", lockKey, businessMillis);
ThreadUtil.sleep(businessMillis);
}
);
return ok(success);
}
/**
* 执行带公平锁的业务。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/fair-lock/try-execute?lockKey=lock:fair:demo&waitSeconds=3&leaseSeconds=10&businessMillis=1000"
*
* @param lockKey 锁 Key
* @param waitSeconds 最大等待秒数
* @param leaseSeconds 锁持有秒数
* @param businessMillis 模拟业务耗时,单位毫秒
* @return 是否执行成功
*/
@PostMapping("/fair-lock/try-execute")
public Map<String, Object> tryExecuteWithFairLock(@RequestParam String lockKey,
@RequestParam(defaultValue = "3") Long waitSeconds,
@RequestParam(defaultValue = "10") Long leaseSeconds,
@RequestParam(defaultValue = "1000") Long businessMillis) {
checkKey(lockKey);
checkLockTime(waitSeconds, leaseSeconds);
checkBusinessMillis(businessMillis);
boolean success = executeWithRLock(
lockKey,
redissonService.getFairLock(lockKey),
waitSeconds,
leaseSeconds,
businessMillis
);
return ok(success);
}
/**
* 执行带自旋锁的业务。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/spin-lock/try-execute?lockKey=lock:spin:demo&waitSeconds=3&leaseSeconds=10&businessMillis=1000"
*
* @param lockKey 锁 Key
* @param waitSeconds 最大等待秒数
* @param leaseSeconds 锁持有秒数
* @param businessMillis 模拟业务耗时,单位毫秒
* @return 是否执行成功
*/
@PostMapping("/spin-lock/try-execute")
public Map<String, Object> tryExecuteWithSpinLock(@RequestParam String lockKey,
@RequestParam(defaultValue = "3") Long waitSeconds,
@RequestParam(defaultValue = "10") Long leaseSeconds,
@RequestParam(defaultValue = "1000") Long businessMillis) {
checkKey(lockKey);
checkLockTime(waitSeconds, leaseSeconds);
checkBusinessMillis(businessMillis);
boolean success = executeWithRLock(
lockKey,
redissonService.getSpinLock(lockKey),
waitSeconds,
leaseSeconds,
businessMillis
);
return ok(success);
}
/**
* 查询锁状态。
* <p>
* curl "http://localhost:8080/redisson/distributed/lock/status?lockKey=lock:demo"
*
* @param lockKey 锁 Key
* @return 锁状态
*/
@GetMapping("/lock/status")
public Map<String, Object> lockStatus(@RequestParam String lockKey) {
checkKey(lockKey);
Map<String, Object> data = new LinkedHashMap<>();
data.put("locked", redissonService.isLocked(lockKey));
data.put("heldByCurrentThread", redissonService.isHeldByCurrentThread(lockKey));
return ok(data);
}
// -------------------------------------------------------------------------
// ReadWriteLock
// -------------------------------------------------------------------------
/**
* 执行读锁保护的业务。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/read-write-lock/read?lockKey=rw:demo&waitSeconds=3&leaseSeconds=10&businessMillis=1000"
*
* @param lockKey 锁 Key
* @param waitSeconds 最大等待秒数
* @param leaseSeconds 锁持有秒数
* @param businessMillis 模拟业务耗时,单位毫秒
* @return 是否执行成功
*/
@PostMapping("/read-write-lock/read")
public Map<String, Object> executeWithReadLock(@RequestParam String lockKey,
@RequestParam(defaultValue = "3") Long waitSeconds,
@RequestParam(defaultValue = "10") Long leaseSeconds,
@RequestParam(defaultValue = "1000") Long businessMillis) {
checkKey(lockKey);
checkLockTime(waitSeconds, leaseSeconds);
checkBusinessMillis(businessMillis);
boolean success = executeWithRLock(
lockKey + ":read",
redissonService.getReadWriteLock(lockKey).readLock(),
waitSeconds,
leaseSeconds,
businessMillis
);
return ok(success);
}
/**
* 执行写锁保护的业务。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/read-write-lock/write?lockKey=rw:demo&waitSeconds=3&leaseSeconds=10&businessMillis=1000"
*
* @param lockKey 锁 Key
* @param waitSeconds 最大等待秒数
* @param leaseSeconds 锁持有秒数
* @param businessMillis 模拟业务耗时,单位毫秒
* @return 是否执行成功
*/
@PostMapping("/read-write-lock/write")
public Map<String, Object> executeWithWriteLock(@RequestParam String lockKey,
@RequestParam(defaultValue = "3") Long waitSeconds,
@RequestParam(defaultValue = "10") Long leaseSeconds,
@RequestParam(defaultValue = "1000") Long businessMillis) {
checkKey(lockKey);
checkLockTime(waitSeconds, leaseSeconds);
checkBusinessMillis(businessMillis);
boolean success = executeWithRLock(
lockKey + ":write",
redissonService.getReadWriteLock(lockKey).writeLock(),
waitSeconds,
leaseSeconds,
businessMillis
);
return ok(success);
}
// -------------------------------------------------------------------------
// CountDownLatch
// -------------------------------------------------------------------------
/**
* 设置闭锁计数。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/latch/set-count?latchKey=latch:demo&count=3"
*
* @param latchKey 闭锁 Key
* @param count 计数
* @return 执行结果
*/
@PostMapping("/latch/set-count")
public Map<String, Object> setCount(@RequestParam String latchKey,
@RequestParam Integer count) {
checkKey(latchKey);
Assert.isTrue(ObjectUtil.isNotNull(count) && count > 0, "count必须大于0");
redissonService.setCount(latchKey, count);
log.info("设置闭锁计数,latchKey={},count={}", latchKey, count);
return ok(true);
}
/**
* 闭锁计数减一。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/latch/count-down?latchKey=latch:demo"
*
* @param latchKey 闭锁 Key
* @return 执行结果
*/
@PostMapping("/latch/count-down")
public Map<String, Object> countDown(@RequestParam String latchKey) {
checkKey(latchKey);
redissonService.countDown(latchKey);
log.info("闭锁计数减一,latchKey={}", latchKey);
return ok(true);
}
/**
* 等待闭锁完成。
* <p>
* curl "http://localhost:8080/redisson/distributed/latch/await?latchKey=latch:demo&timeoutSeconds=10"
*
* @param latchKey 闭锁 Key
* @param timeoutSeconds 超时秒数
* @return 是否完成
* @throws InterruptedException 线程中断时抛出
*/
@GetMapping("/latch/await")
public Map<String, Object> await(@RequestParam String latchKey,
@RequestParam(defaultValue = "10") Long timeoutSeconds) throws InterruptedException {
checkKey(latchKey);
Assert.isTrue(ObjectUtil.isNotNull(timeoutSeconds) && timeoutSeconds >= 0, "timeoutSeconds不能小于0");
boolean success = redissonService.await(latchKey, timeoutSeconds, TimeUnit.SECONDS);
return ok(success);
}
// -------------------------------------------------------------------------
// Semaphore
// -------------------------------------------------------------------------
/**
* 初始化信号量许可。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/semaphore/init?semaphoreKey=semaphore:demo&permits=5"
*
* @param semaphoreKey 信号量 Key
* @param permits 许可数量
* @return 执行结果
*/
@PostMapping("/semaphore/init")
public Map<String, Object> trySetPermits(@RequestParam String semaphoreKey,
@RequestParam Integer permits) {
checkKey(semaphoreKey);
Assert.isTrue(ObjectUtil.isNotNull(permits) && permits > 0, "permits必须大于0");
redissonService.trySetPermits(semaphoreKey, permits);
return ok(true);
}
/**
* 尝试获取信号量许可。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/semaphore/try-acquire?semaphoreKey=semaphore:demo&permits=1&waitSeconds=3"
*
* @param semaphoreKey 信号量 Key
* @param permits 许可数量
* @param waitSeconds 等待秒数
* @return 是否获取成功
* @throws InterruptedException 线程中断时抛出
*/
@PostMapping("/semaphore/try-acquire")
public Map<String, Object> tryAcquire(@RequestParam String semaphoreKey,
@RequestParam(defaultValue = "1") Integer permits,
@RequestParam(defaultValue = "3") Long waitSeconds) throws InterruptedException {
checkKey(semaphoreKey);
Assert.isTrue(ObjectUtil.isNotNull(permits) && permits > 0, "permits必须大于0");
Assert.isTrue(ObjectUtil.isNotNull(waitSeconds) && waitSeconds >= 0, "waitSeconds不能小于0");
boolean success = redissonService.tryAcquire(semaphoreKey, permits, waitSeconds, TimeUnit.SECONDS);
return ok(success);
}
/**
* 释放一个信号量许可。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/semaphore/release?semaphoreKey=semaphore:demo"
*
* @param semaphoreKey 信号量 Key
* @return 执行结果
*/
@PostMapping("/semaphore/release")
public Map<String, Object> release(@RequestParam String semaphoreKey) {
checkKey(semaphoreKey);
redissonService.release(semaphoreKey);
return ok(true);
}
/**
* 获取信号量可用许可数量。
* <p>
* curl "http://localhost:8080/redisson/distributed/semaphore/available?semaphoreKey=semaphore:demo"
*
* @param semaphoreKey 信号量 Key
* @return 可用许可数量
*/
@GetMapping("/semaphore/available")
public Map<String, Object> availablePermits(@RequestParam String semaphoreKey) {
checkKey(semaphoreKey);
int permits = redissonService.availablePermits(semaphoreKey);
return ok(permits);
}
// -------------------------------------------------------------------------
// PermitExpirableSemaphore
// -------------------------------------------------------------------------
/**
* 初始化可过期信号量许可。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/permit-expirable-semaphore/init?semaphoreKey=permit:demo&permits=5"
*
* @param semaphoreKey 信号量 Key
* @param permits 许可数量
* @return 执行结果
*/
@PostMapping("/permit-expirable-semaphore/init")
public Map<String, Object> permitTrySetPermits(@RequestParam String semaphoreKey,
@RequestParam Integer permits) {
checkKey(semaphoreKey);
Assert.isTrue(ObjectUtil.isNotNull(permits) && permits > 0, "permits必须大于0");
boolean success = redissonService.getPermitExpirableSemaphore(semaphoreKey).trySetPermits(permits);
return ok(success);
}
/**
* 尝试获取可过期信号量许可。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/permit-expirable-semaphore/try-acquire?semaphoreKey=permit:demo&waitSeconds=3&leaseSeconds=30"
*
* @param semaphoreKey 信号量 Key
* @param waitSeconds 等待秒数
* @param leaseSeconds 许可租约秒数
* @return permitId,返回 null 表示未获取到
* @throws InterruptedException 线程中断时抛出
*/
@PostMapping("/permit-expirable-semaphore/try-acquire")
public Map<String, Object> permitTryAcquire(@RequestParam String semaphoreKey,
@RequestParam(defaultValue = "3") Long waitSeconds,
@RequestParam(defaultValue = "30") Long leaseSeconds) throws InterruptedException {
checkKey(semaphoreKey);
Assert.isTrue(ObjectUtil.isNotNull(waitSeconds) && waitSeconds >= 0, "waitSeconds不能小于0");
Assert.isTrue(ObjectUtil.isNotNull(leaseSeconds) && leaseSeconds > 0, "leaseSeconds必须大于0");
String permitId = redissonService.getPermitExpirableSemaphore(semaphoreKey)
.tryAcquire(waitSeconds, leaseSeconds, TimeUnit.SECONDS);
return ok(permitId);
}
/**
* 释放可过期信号量许可。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/permit-expirable-semaphore/release?semaphoreKey=permit:demo&permitId=xxxx"
*
* @param semaphoreKey 信号量 Key
* @param permitId 许可 ID
* @return 执行结果
*/
@PostMapping("/permit-expirable-semaphore/release")
public Map<String, Object> permitRelease(@RequestParam String semaphoreKey,
@RequestParam String permitId) {
checkKey(semaphoreKey);
Assert.isTrue(StrUtil.isNotBlank(permitId), "permitId不能为空");
redissonService.getPermitExpirableSemaphore(semaphoreKey).release(permitId);
return ok(true);
}
/**
* 获取可过期信号量可用许可数量。
* <p>
* curl "http://localhost:8080/redisson/distributed/permit-expirable-semaphore/available?semaphoreKey=permit:demo"
*
* @param semaphoreKey 信号量 Key
* @return 可用许可数量
*/
@GetMapping("/permit-expirable-semaphore/available")
public Map<String, Object> permitAvailablePermits(@RequestParam String semaphoreKey) {
checkKey(semaphoreKey);
int permits = redissonService.getPermitExpirableSemaphore(semaphoreKey).availablePermits();
return ok(permits);
}
// -------------------------------------------------------------------------
// RateLimiter
// -------------------------------------------------------------------------
/**
* 初始化限流器。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/rate-limiter/init?key=rate:sms&rate=5&interval=1&unit=SECONDS&rateType=OVERALL"
*
* @param key 限流器 Key
* @param rate 令牌数量
* @param interval 时间间隔
* @param unit 时间单位
* @param rateType 限流类型
* @return 是否初始化成功
*/
@PostMapping("/rate-limiter/init")
public Map<String, Object> rateLimiterInit(@RequestParam String key,
@RequestParam Long rate,
@RequestParam(defaultValue = "1") Long interval,
@RequestParam(defaultValue = "SECONDS") RateIntervalUnit unit,
@RequestParam(defaultValue = "OVERALL") RateType rateType) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(rate) && rate > 0, "rate必须大于0");
Assert.isTrue(ObjectUtil.isNotNull(interval) && interval > 0, "interval必须大于0");
boolean success = redissonService.rateLimiterInit(key, rateType, rate, interval, unit);
return ok(success);
}
/**
* 更新限流器速率。
* <p>
* curl -X PUT "http://localhost:8080/redisson/distributed/rate-limiter/rate?key=rate:sms&rate=10&interval=1&unit=SECONDS&rateType=OVERALL"
*
* @param key 限流器 Key
* @param rate 令牌数量
* @param interval 时间间隔
* @param unit 时间单位
* @param rateType 限流类型
* @return 执行结果
*/
@PutMapping("/rate-limiter/rate")
public Map<String, Object> rateLimiterSetRate(@RequestParam String key,
@RequestParam Long rate,
@RequestParam(defaultValue = "1") Long interval,
@RequestParam(defaultValue = "SECONDS") RateIntervalUnit unit,
@RequestParam(defaultValue = "OVERALL") RateType rateType) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(rate) && rate > 0, "rate必须大于0");
Assert.isTrue(ObjectUtil.isNotNull(interval) && interval > 0, "interval必须大于0");
redissonService.rateLimiterSetRate(key, rateType, rate, interval, unit);
return ok(true);
}
/**
* 尝试获取限流令牌。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/rate-limiter/try-acquire?key=rate:sms&permits=1"
*
* @param key 限流器 Key
* @param permits 令牌数量
* @return 是否获取成功
*/
@PostMapping("/rate-limiter/try-acquire")
public Map<String, Object> rateLimiterTryAcquire(@RequestParam String key,
@RequestParam(defaultValue = "1") Long permits) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(permits) && permits > 0, "permits必须大于0");
boolean success = redissonService.rateLimiterTryAcquire(key, permits);
return ok(success);
}
/**
* 等待获取限流令牌。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/rate-limiter/try-acquire-wait?key=rate:sms&timeoutSeconds=3"
*
* @param key 限流器 Key
* @param timeoutSeconds 等待秒数
* @return 是否获取成功
*/
@PostMapping("/rate-limiter/try-acquire-wait")
public Map<String, Object> rateLimiterTryAcquireWait(@RequestParam String key,
@RequestParam(defaultValue = "3") Long timeoutSeconds) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(timeoutSeconds) && timeoutSeconds >= 0, "timeoutSeconds不能小于0");
boolean success = redissonService.rateLimiterTryAcquire(key, timeoutSeconds, TimeUnit.SECONDS);
return ok(success);
}
/**
* 删除限流器。
* <p>
* curl -X DELETE "http://localhost:8080/redisson/distributed/rate-limiter?key=rate:sms"
*
* @param key 限流器 Key
* @return 是否删除成功
*/
@DeleteMapping("/rate-limiter")
public Map<String, Object> rateLimiterDelete(@RequestParam String key) {
checkKey(key);
boolean success = redissonService.rateLimiterDelete(key);
return ok(success);
}
// -------------------------------------------------------------------------
// BloomFilter
// -------------------------------------------------------------------------
/**
* 初始化布隆过滤器。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/bloom/init?key=bloom:user&expectedInsertions=100000&falseProbability=0.01"
*
* @param key Redis 键
* @param expectedInsertions 预计插入量
* @param falseProbability 误判率
* @return 执行结果
*/
@PostMapping("/bloom/init")
public Map<String, Object> bloomInit(@RequestParam String key,
@RequestParam(defaultValue = "100000") Long expectedInsertions,
@RequestParam(defaultValue = "0.01") Double falseProbability) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(expectedInsertions) && expectedInsertions > 0, "expectedInsertions必须大于0");
Assert.isTrue(ObjectUtil.isNotNull(falseProbability) && falseProbability > 0 && falseProbability < 1,
"falseProbability必须在0到1之间");
redissonService.bloomInit(key, expectedInsertions, falseProbability);
return ok(true);
}
/**
* 添加布隆过滤器元素。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/bloom/add?key=bloom:user" \
* -H "Content-Type: application/json" \
* -d '"user:1"'
*
* @param key Redis 键
* @param value 元素
* @return 是否新增
*/
@PostMapping("/bloom/add")
public Map<String, Object> bloomAdd(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
Assert.notNull(value, "value不能为空");
boolean success = redissonService.bloomAdd(key, value);
return ok(success);
}
/**
* 批量添加布隆过滤器元素。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/bloom/add-all?key=bloom:user" \
* -H "Content-Type: application/json" \
* -d '["user:1","user:2","user:3"]'
*
* @param key Redis 键
* @param values 元素集合
* @return 新增数量
*/
@PostMapping("/bloom/add-all")
public Map<String, Object> bloomAddAll(@RequestParam String key,
@RequestBody List<Object> values) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(values), "values不能为空");
long count = redissonService.bloomAddAll(key, values);
return ok(count);
}
/**
* 判断布隆过滤器是否可能包含元素。
* <p>
* curl -X POST "http://localhost:8080/redisson/distributed/bloom/contains?key=bloom:user" \
* -H "Content-Type: application/json" \
* -d '"user:1"'
*
* @param key Redis 键
* @param value 元素
* @return 是否可能包含
*/
@PostMapping("/bloom/contains")
public Map<String, Object> bloomContains(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
Assert.notNull(value, "value不能为空");
boolean exists = redissonService.bloomContains(key, value);
return ok(exists);
}
/**
* 判断布隆过滤器是否存在。
* <p>
* curl "http://localhost:8080/redisson/distributed/bloom/exists?key=bloom:user"
*
* @param key Redis 键
* @return 是否存在
*/
@GetMapping("/bloom/exists")
public Map<String, Object> bloomExists(@RequestParam String key) {
checkKey(key);
boolean exists = redissonService.bloomExists(key);
return ok(exists);
}
/**
* 获取布隆过滤器信息。
* <p>
* curl "http://localhost:8080/redisson/distributed/bloom/info?key=bloom:user"
*
* @param key Redis 键
* @return 布隆过滤器信息
*/
@GetMapping("/bloom/info")
public Map<String, Object> bloomInfo(@RequestParam String key) {
checkKey(key);
Map<String, Object> data = new LinkedHashMap<>();
data.put("exists", redissonService.bloomExists(key));
data.put("expectedInsertions", redissonService.bloomGetExpectedInsertions(key));
data.put("falseProbability", redissonService.bloomGetFalseProbability(key));
RBloomFilter<Object> bloomFilter = redissonService.getBloomFilter(key);
if (bloomFilter.isExists()) {
data.put("count", bloomFilter.count());
data.put("size", bloomFilter.getSize());
data.put("hashIterations", bloomFilter.getHashIterations());
}
return ok(data);
}
/**
* 删除布隆过滤器。
* <p>
* curl -X DELETE "http://localhost:8080/redisson/distributed/bloom?key=bloom:user"
*
* @param key Redis 键
* @return 是否删除成功
*/
@DeleteMapping("/bloom")
public Map<String, Object> bloomDelete(@RequestParam String key) {
checkKey(key);
boolean success = redissonService.bloomDelete(key);
return ok(success);
}
/**
* 执行带锁任务。
*
* @param lockKey 锁 Key
* @param lock 锁对象
* @param waitSeconds 最大等待秒数
* @param leaseSeconds 锁持有秒数
* @param businessMillis 模拟业务耗时
* @return 是否执行成功
*/
private boolean executeWithRLock(String lockKey,
RLock lock,
long waitSeconds,
long leaseSeconds,
long businessMillis) {
boolean locked = false;
try {
locked = leaseSeconds <= 0
? lock.tryLock(waitSeconds, TimeUnit.SECONDS)
: lock.tryLock(waitSeconds, leaseSeconds, TimeUnit.SECONDS);
if (!locked) {
log.warn("获取分布式锁失败,lockKey={}", lockKey);
return false;
}
log.info("获取分布式锁成功,lockKey={},businessMillis={}", lockKey, businessMillis);
ThreadUtil.sleep(businessMillis);
return true;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.error("获取分布式锁被中断,lockKey={}", lockKey, e);
return false;
} finally {
unlockSafely(lockKey, lock, locked);
}
}
/**
* 安全释放锁。
*
* @param lockKey 锁 Key
* @param lock 锁对象
* @param locked 是否已获取锁
*/
private void unlockSafely(String lockKey, RLock lock, boolean locked) {
if (!locked || ObjectUtil.isNull(lock)) {
return;
}
if (!lock.isHeldByCurrentThread()) {
log.warn("当前线程未持有分布式锁,跳过释放,lockKey={}", lockKey);
return;
}
try {
lock.unlock();
log.info("释放分布式锁成功,lockKey={}", lockKey);
} catch (Exception e) {
log.error("释放分布式锁异常,lockKey={}", lockKey, e);
}
}
/**
* 校验 Redis Key。
*
* @param key Redis 键
*/
private void checkKey(String key) {
Assert.isTrue(StrUtil.isNotBlank(key), "Redis Key不能为空");
}
/**
* 校验锁时间参数。
*
* @param waitSeconds 等待秒数
* @param leaseSeconds 持有秒数
*/
private void checkLockTime(Long waitSeconds, Long leaseSeconds) {
Assert.isTrue(ObjectUtil.isNotNull(waitSeconds) && waitSeconds >= 0, "waitSeconds不能小于0");
Assert.notNull(leaseSeconds, "leaseSeconds不能为空");
}
/**
* 校验模拟业务耗时。
*
* @param businessMillis 模拟业务耗时
*/
private void checkBusinessMillis(Long businessMillis) {
Assert.isTrue(ObjectUtil.isNotNull(businessMillis) && businessMillis >= 0, "businessMillis不能小于0");
}
/**
* 成功响应。
*
* @param data 响应数据
* @return 响应 Map
*/
private Map<String, Object> ok(Object data) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("code", 0);
result.put("message", "操作成功");
result.put("data", data);
return result;
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
消息能力测试控制器
用于演示 RedissonService 的队列、延迟队列、发布订阅、模式订阅、可靠消息对象入口、Stream 消息流等能力。
package local.ateng.java.redis.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import local.ateng.java.redis.service.RedissonService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.*;
import org.redisson.api.listener.PatternMessageListener;
import org.redisson.api.stream.StreamReadArgs;
import org.redisson.api.stream.StreamReadGroupArgs;
import org.springframework.web.bind.annotation.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
/**
* Redisson 消息能力测试控制器
* 用于演示 RedissonService 的队列、延迟队列、发布订阅、模式订阅、可靠消息对象入口、Stream 消息流等能力。
*
* @author Ateng
* @since 2026-04-26
*/
@Slf4j
@RestController
@RequestMapping("/redisson/message")
@RequiredArgsConstructor
public class RedissonMessageController {
private final RedissonService redissonService;
private final Map<String, List<Integer>> topicListenerMap = new ConcurrentHashMap<>();
private final Map<String, List<Integer>> patternTopicListenerMap = new ConcurrentHashMap<>();
// -------------------------------------------------------------------------
// Queue / BlockingQueue / DelayedQueue
// -------------------------------------------------------------------------
/**
* 普通队列入队。
*
* curl -X POST "http://localhost:8080/redisson/message/queue/enqueue?queueKey=queue:test" \
* -H "Content-Type: application/json" \
* -d '{"id":1,"name":"Ateng"}'
*
* @param queueKey 队列 Key
* @param value 元素
* @return 是否入队成功
*/
@PostMapping("/queue/enqueue")
public Map<String, Object> enqueue(@RequestParam String queueKey,
@RequestBody Object value) {
checkKey(queueKey);
boolean success = redissonService.enqueue(queueKey, value);
log.info("普通队列入队成功,queueKey={},success={}", queueKey, success);
return ok(success);
}
/**
* 普通队列出队。
*
* curl -X POST "http://localhost:8080/redisson/message/queue/dequeue?queueKey=queue:test"
*
* @param queueKey 队列 Key
* @return 出队元素
*/
@PostMapping("/queue/dequeue")
public Map<String, Object> dequeue(@RequestParam String queueKey) {
checkKey(queueKey);
Object value = redissonService.dequeue(queueKey);
return ok(value);
}
/**
* 阻塞队列入队。
*
* curl -X POST "http://localhost:8080/redisson/message/blocking-queue/enqueue?queueKey=queue:blocking:test" \
* -H "Content-Type: application/json" \
* -d '"message-1"'
*
* @param queueKey 队列 Key
* @param value 元素
* @return 执行结果
* @throws InterruptedException 线程中断时抛出
*/
@PostMapping("/blocking-queue/enqueue")
public Map<String, Object> enqueueBlocking(@RequestParam String queueKey,
@RequestBody Object value) throws InterruptedException {
checkKey(queueKey);
redissonService.enqueueBlocking(queueKey, value);
log.info("阻塞队列入队成功,queueKey={}", queueKey);
return ok(true);
}
/**
* 阻塞队列超时入队。
*
* curl -X POST "http://localhost:8080/redisson/message/blocking-queue/enqueue-timeout?queueKey=queue:blocking:test&timeoutSeconds=3" \
* -H "Content-Type: application/json" \
* -d '"message-1"'
*
* @param queueKey 队列 Key
* @param timeoutSeconds 超时秒数
* @param value 元素
* @return 是否入队成功
* @throws InterruptedException 线程中断时抛出
*/
@PostMapping("/blocking-queue/enqueue-timeout")
public Map<String, Object> enqueueBlockingTimeout(@RequestParam String queueKey,
@RequestParam(defaultValue = "3") Long timeoutSeconds,
@RequestBody Object value) throws InterruptedException {
checkKey(queueKey);
Assert.isTrue(ObjectUtil.isNotNull(timeoutSeconds) && timeoutSeconds >= 0, "timeoutSeconds不能小于0");
boolean success = redissonService.enqueueBlocking(queueKey, value, timeoutSeconds, TimeUnit.SECONDS);
return ok(success);
}
/**
* 阻塞队列超时出队。
*
* curl -X POST "http://localhost:8080/redisson/message/blocking-queue/dequeue?queueKey=queue:blocking:test&timeoutSeconds=10"
*
* @param queueKey 队列 Key
* @param timeoutSeconds 超时秒数
* @return 出队元素
* @throws InterruptedException 线程中断时抛出
*/
@PostMapping("/blocking-queue/dequeue")
public Map<String, Object> dequeueBlocking(@RequestParam String queueKey,
@RequestParam(defaultValue = "10") Long timeoutSeconds) throws InterruptedException {
checkKey(queueKey);
Assert.isTrue(ObjectUtil.isNotNull(timeoutSeconds) && timeoutSeconds >= 0, "timeoutSeconds不能小于0");
Object value = redissonService.dequeueBlocking(queueKey, timeoutSeconds, TimeUnit.SECONDS);
return ok(value);
}
/**
* 获取队列长度。
*
* curl "http://localhost:8080/redisson/message/queue/size?queueKey=queue:test"
*
* @param queueKey 队列 Key
* @return 队列长度
*/
@GetMapping("/queue/size")
public Map<String, Object> queueSize(@RequestParam String queueKey) {
checkKey(queueKey);
long size = redissonService.queueSize(queueKey);
return ok(size);
}
/**
* 判断队列是否为空。
*
* curl "http://localhost:8080/redisson/message/queue/empty?queueKey=queue:test"
*
* @param queueKey 队列 Key
* @return 是否为空
*/
@GetMapping("/queue/empty")
public Map<String, Object> isQueueEmpty(@RequestParam String queueKey) {
checkKey(queueKey);
boolean empty = redissonService.isQueueEmpty(queueKey);
return ok(empty);
}
/**
* 清空队列。
*
* curl -X DELETE "http://localhost:8080/redisson/message/queue/clear?queueKey=queue:test"
*
* @param queueKey 队列 Key
* @return 执行结果
*/
@DeleteMapping("/queue/clear")
public Map<String, Object> clearQueue(@RequestParam String queueKey) {
checkKey(queueKey);
redissonService.clearQueue(queueKey);
log.info("队列清空成功,queueKey={}", queueKey);
return ok(true);
}
/**
* 删除队列元素。
*
* curl -X POST "http://localhost:8080/redisson/message/queue/remove?queueKey=queue:test" \
* -H "Content-Type: application/json" \
* -d '"message-1"'
*
* @param queueKey 队列 Key
* @param value 元素
* @return 是否删除成功
*/
@PostMapping("/queue/remove")
public Map<String, Object> removeFromQueue(@RequestParam String queueKey,
@RequestBody Object value) {
checkKey(queueKey);
boolean success = redissonService.removeFromQueue(queueKey, value);
return ok(success);
}
/**
* 添加延迟队列任务。
*
* curl -X POST "http://localhost:8080/redisson/message/delayed-queue/enqueue?queueKey=queue:delay:test&delaySeconds=10" \
* -H "Content-Type: application/json" \
* -d '{"taskId":1,"type":"timeout-close-order"}'
*
* @param queueKey 队列 Key
* @param delaySeconds 延迟秒数
* @param value 元素
* @return 执行结果
*/
@PostMapping("/delayed-queue/enqueue")
public Map<String, Object> enqueueDelayed(@RequestParam String queueKey,
@RequestParam(defaultValue = "10") Long delaySeconds,
@RequestBody Object value) {
checkKey(queueKey);
Assert.isTrue(ObjectUtil.isNotNull(delaySeconds) && delaySeconds >= 0, "delaySeconds不能小于0");
redissonService.enqueueDelayed(queueKey, value, delaySeconds, TimeUnit.SECONDS);
log.info("延迟队列任务写入成功,queueKey={},delaySeconds={}", queueKey, delaySeconds);
return ok(true);
}
/**
* 获取延迟队列对象信息。
*
* curl "http://localhost:8080/redisson/message/delayed-queue/info?queueKey=queue:delay:test"
*
* @param queueKey 队列 Key
* @return 延迟队列信息
*/
@GetMapping("/delayed-queue/info")
public Map<String, Object> delayedQueueInfo(@RequestParam String queueKey) {
checkKey(queueKey);
RDelayedQueue<Object> delayedQueue = redissonService.getDelayedQueue(queueKey);
Map<String, Object> data = new LinkedHashMap<>();
data.put("className", delayedQueue.getClass().getName());
data.put("queueKey", queueKey);
return ok(data);
}
// -------------------------------------------------------------------------
// Deque / RingBuffer / PriorityQueue / ReliableQueue
// -------------------------------------------------------------------------
/**
* 阻塞双端队列左侧入队。
*
* curl -X POST "http://localhost:8080/redisson/message/blocking-deque/left-push?key=deque:blocking:test" \
* -H "Content-Type: application/json" \
* -d '"A"'
*
* @param key Redis 键
* @param value 元素
* @return 执行结果
*/
@PostMapping("/blocking-deque/left-push")
public Map<String, Object> blockingDequeLeftPush(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
RBlockingDeque<Object> deque = redissonService.getBlockingDeque(key);
deque.offerFirst(value);
return ok(true);
}
/**
* 阻塞双端队列右侧入队。
*
* curl -X POST "http://localhost:8080/redisson/message/blocking-deque/right-push?key=deque:blocking:test" \
* -H "Content-Type: application/json" \
* -d '"B"'
*
* @param key Redis 键
* @param value 元素
* @return 执行结果
*/
@PostMapping("/blocking-deque/right-push")
public Map<String, Object> blockingDequeRightPush(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
RBlockingDeque<Object> deque = redissonService.getBlockingDeque(key);
deque.offerLast(value);
return ok(true);
}
/**
* 阻塞双端队列左侧出队。
*
* curl -X POST "http://localhost:8080/redisson/message/blocking-deque/left-pop?key=deque:blocking:test&timeoutSeconds=5"
*
* @param key Redis 键
* @param timeoutSeconds 等待秒数
* @return 元素
* @throws InterruptedException 线程中断时抛出
*/
@PostMapping("/blocking-deque/left-pop")
public Map<String, Object> blockingDequeLeftPop(@RequestParam String key,
@RequestParam(defaultValue = "5") Long timeoutSeconds) throws InterruptedException {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(timeoutSeconds) && timeoutSeconds >= 0, "timeoutSeconds不能小于0");
RBlockingDeque<Object> deque = redissonService.getBlockingDeque(key);
Object value = deque.pollFirst(timeoutSeconds, TimeUnit.SECONDS);
return ok(value);
}
/**
* 初始化环形缓冲队列容量。
*
* curl -X POST "http://localhost:8080/redisson/message/ring-buffer/init?key=ring:test&capacity=10"
*
* @param key Redis 键
* @param capacity 容量
* @return 是否初始化成功
*/
@PostMapping("/ring-buffer/init")
public Map<String, Object> ringBufferInit(@RequestParam String key,
@RequestParam Integer capacity) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(capacity) && capacity > 0, "capacity必须大于0");
RRingBuffer<Object> ringBuffer = redissonService.getRingBuffer(key);
boolean success = ringBuffer.trySetCapacity(capacity);
return ok(success);
}
/**
* 环形缓冲队列添加元素。
*
* curl -X POST "http://localhost:8080/redisson/message/ring-buffer/add?key=ring:test" \
* -H "Content-Type: application/json" \
* -d '"A"'
*
* @param key Redis 键
* @param value 元素
* @return 是否添加成功
*/
@PostMapping("/ring-buffer/add")
public Map<String, Object> ringBufferAdd(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
RRingBuffer<Object> ringBuffer = redissonService.getRingBuffer(key);
boolean success = ringBuffer.add(value);
return ok(success);
}
/**
* 获取环形缓冲队列信息。
*
* curl "http://localhost:8080/redisson/message/ring-buffer/info?key=ring:test"
*
* @param key Redis 键
* @return 信息
*/
@GetMapping("/ring-buffer/info")
public Map<String, Object> ringBufferInfo(@RequestParam String key) {
checkKey(key);
RRingBuffer<Object> ringBuffer = redissonService.getRingBuffer(key);
Map<String, Object> data = new LinkedHashMap<>();
data.put("size", ringBuffer.size());
data.put("capacity", ringBuffer.capacity());
data.put("remainingCapacity", ringBuffer.remainingCapacity());
data.put("values", ringBuffer.readAll());
return ok(data);
}
/**
* 优先级队列添加元素。
* 注意:元素需要实现 Comparable,否则会在运行时排序失败。
*
* curl -X POST "http://localhost:8080/redisson/message/priority-queue/add?key=priority:test" \
* -H "Content-Type: application/json" \
* -d '"A"'
*
* @param key Redis 键
* @param value 元素
* @return 是否添加成功
*/
@PostMapping("/priority-queue/add")
public Map<String, Object> priorityQueueAdd(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
RPriorityQueue<Object> priorityQueue = redissonService.getPriorityQueue(key);
boolean success = priorityQueue.offer(value);
return ok(success);
}
/**
* 优先级队列弹出元素。
*
* curl -X POST "http://localhost:8080/redisson/message/priority-queue/poll?key=priority:test"
*
* @param key Redis 键
* @return 元素
*/
@PostMapping("/priority-queue/poll")
public Map<String, Object> priorityQueuePoll(@RequestParam String key) {
checkKey(key);
RPriorityQueue<Object> priorityQueue = redissonService.getPriorityQueue(key);
Object value = priorityQueue.poll();
return ok(value);
}
/**
* 获取可靠队列对象信息。
*
* curl "http://localhost:8080/redisson/message/reliable-queue/info?key=reliable:test"
*
* @param key Redis 键
* @return 可靠队列信息
*/
@GetMapping("/reliable-queue/info")
public Map<String, Object> reliableQueueInfo(@RequestParam String key) {
checkKey(key);
RReliableQueue<Object> reliableQueue = redissonService.getReliableQueue(key);
Map<String, Object> data = new LinkedHashMap<>();
data.put("className", reliableQueue.getClass().getName());
data.put("key", key);
data.put("exists", reliableQueue.isExists());
return ok(data);
}
// -------------------------------------------------------------------------
// Topic / PatternTopic / ReliableTopic
// -------------------------------------------------------------------------
/**
* 发布消息。
*
* curl -X POST "http://localhost:8080/redisson/message/topic/publish?channel=topic:test" \
* -H "Content-Type: application/json" \
* -d '{"type":"notice","content":"hello"}'
*
* @param channel 频道
* @param message 消息
* @return 接收客户端数量
*/
@PostMapping("/topic/publish")
public Map<String, Object> publish(@RequestParam String channel,
@RequestBody Object message) {
checkKey(channel);
long receivers = redissonService.publish(channel, message);
log.info("Redis Topic 消息发布成功,channel={},receivers={}", channel, receivers);
return ok(receivers);
}
/**
* 订阅频道。
*
* curl -X POST "http://localhost:8080/redisson/message/topic/subscribe?channel=topic:test"
*
* @param channel 频道
* @return 监听器 ID
*/
@PostMapping("/topic/subscribe")
public Map<String, Object> subscribe(@RequestParam String channel) {
checkKey(channel);
Consumer<Object> consumer = message -> log.info("收到 Redis Topic 消息,channel={},message={}", channel, message);
int listenerId = redissonService.subscribe(channel, consumer);
topicListenerMap.computeIfAbsent(channel, item -> new CopyOnWriteArrayList<>()).add(listenerId);
Map<String, Object> data = new LinkedHashMap<>();
data.put("channel", channel);
data.put("listenerId", listenerId);
return ok(data);
}
/**
* 取消指定频道监听器。
*
* curl -X DELETE "http://localhost:8080/redisson/message/topic/unsubscribe?channel=topic:test&listenerId=1"
*
* @param channel 频道
* @param listenerId 监听器 ID
* @return 执行结果
*/
@DeleteMapping("/topic/unsubscribe")
public Map<String, Object> unsubscribe(@RequestParam String channel,
@RequestParam Integer listenerId) {
checkKey(channel);
Assert.notNull(listenerId, "listenerId不能为空");
redissonService.unsubscribe(channel, listenerId);
removeListenerId(topicListenerMap, channel, listenerId);
return ok(true);
}
/**
* 取消频道全部监听器。
*
* curl -X DELETE "http://localhost:8080/redisson/message/topic/unsubscribe-all?channel=topic:test"
*
* @param channel 频道
* @return 执行结果
*/
@DeleteMapping("/topic/unsubscribe-all")
public Map<String, Object> unsubscribeAll(@RequestParam String channel) {
checkKey(channel);
redissonService.unsubscribe(channel);
topicListenerMap.remove(channel);
return ok(true);
}
/**
* 查看当前 Controller 记录的 Topic 监听器。
*
* curl "http://localhost:8080/redisson/message/topic/listeners"
*
* @return 监听器记录
*/
@GetMapping("/topic/listeners")
public Map<String, Object> topicListeners() {
return ok(topicListenerMap);
}
/**
* 订阅模式频道。
*
* curl -X POST "http://localhost:8080/redisson/message/pattern-topic/subscribe?pattern=topic:*"
*
* @param pattern 频道通配符
* @return 监听器 ID
*/
@PostMapping("/pattern-topic/subscribe")
public Map<String, Object> patternSubscribe(@RequestParam String pattern) {
checkKey(pattern);
RPatternTopic patternTopic = redissonService.getPatternTopic(pattern);
PatternMessageListener<Object> listener = (patternText, channel, message) ->
log.info("收到 Redis PatternTopic 消息,pattern={},channel={},message={}", patternText, channel, message);
int listenerId = patternTopic.addListener(Object.class, listener);
patternTopicListenerMap.computeIfAbsent(pattern, item -> new CopyOnWriteArrayList<>()).add(listenerId);
Map<String, Object> data = new LinkedHashMap<>();
data.put("pattern", pattern);
data.put("listenerId", listenerId);
return ok(data);
}
/**
* 取消模式频道监听器。
*
* curl -X DELETE "http://localhost:8080/redisson/message/pattern-topic/unsubscribe?pattern=topic:*&listenerId=1"
*
* @param pattern 频道通配符
* @param listenerId 监听器 ID
* @return 执行结果
*/
@DeleteMapping("/pattern-topic/unsubscribe")
public Map<String, Object> patternUnsubscribe(@RequestParam String pattern,
@RequestParam Integer listenerId) {
checkKey(pattern);
Assert.notNull(listenerId, "listenerId不能为空");
redissonService.getPatternTopic(pattern).removeListener(listenerId);
removeListenerId(patternTopicListenerMap, pattern, listenerId);
return ok(true);
}
/**
* 获取可靠主题对象信息。
*
* curl "http://localhost:8080/redisson/message/reliable-topic/info?topic=reliable-topic:test"
*
* @param topic 主题
* @return 可靠主题信息
*/
@GetMapping("/reliable-topic/info")
public Map<String, Object> reliableTopicInfo(@RequestParam String topic) {
checkKey(topic);
RReliableTopic reliableTopic = redissonService.getReliableTopic(topic);
Map<String, Object> data = new LinkedHashMap<>();
data.put("className", reliableTopic.getClass().getName());
data.put("topic", topic);
return ok(data);
}
// -------------------------------------------------------------------------
// Stream
// -------------------------------------------------------------------------
/**
* 添加 Stream 消息。
*
* curl -X POST "http://localhost:8080/redisson/message/stream/add?streamKey=stream:test" \
* -H "Content-Type: application/json" \
* -d '{"userId":"1001","event":"login","time":"2026-04-26 10:00:00"}'
*
* @param streamKey Stream Key
* @param entries 消息字段
* @return 消息 ID
*/
@PostMapping("/stream/add")
public Map<String, Object> streamAdd(@RequestParam String streamKey,
@RequestBody Map<Object, Object> entries) {
checkKey(streamKey);
Assert.isTrue(CollUtil.isNotEmpty(entries), "entries不能为空");
StreamMessageId id = redissonService.streamAdd(streamKey, entries);
log.info("Redis Stream 消息写入成功,streamKey={},id={}", streamKey, id);
return ok(idToMap(id));
}
/**
* 读取 Stream 消息。
*
* curl "http://localhost:8080/redisson/message/stream/read?streamKey=stream:test&id=0-0&count=10"
*
* @param streamKey Stream Key
* @param id 起始 ID,格式如 0-0
* @param count 数量
* @return 消息 Map
*/
@GetMapping("/stream/read")
public Map<String, Object> streamRead(@RequestParam String streamKey,
@RequestParam(defaultValue = "0-0") String id,
@RequestParam(defaultValue = "10") Integer count) {
checkKey(streamKey);
Assert.isTrue(ObjectUtil.isNotNull(count) && count > 0, "count必须大于0");
StreamMessageId startId = parseStreamMessageId(id);
StreamReadArgs args = StreamReadArgs.greaterThan(startId).count(count);
Map<StreamMessageId, Map<Object, Object>> data = redissonService.streamRead(streamKey, args);
return ok(streamMapToResponse(data));
}
/**
* 创建 Stream 消费组。
*
* curl -X POST "http://localhost:8080/redisson/message/stream/group/create?streamKey=stream:test&groupName=group:test&id=0-0"
*
* @param streamKey Stream Key
* @param groupName 消费组
* @param id 起始 ID,格式如 0-0
* @return 执行结果
*/
@PostMapping("/stream/group/create")
public Map<String, Object> streamCreateGroup(@RequestParam String streamKey,
@RequestParam String groupName,
@RequestParam(defaultValue = "0-0") String id) {
checkKey(streamKey);
checkKey(groupName);
StreamMessageId startId = parseStreamMessageId(id);
redissonService.streamCreateGroup(streamKey, groupName, startId);
return ok(true);
}
/**
* 读取消费组未投递的新消息。
*
* curl "http://localhost:8080/redisson/message/stream/group/read-new?streamKey=stream:test&groupName=group:test&consumerName=consumer:1&count=10"
*
* @param streamKey Stream Key
* @param groupName 消费组
* @param consumerName 消费者
* @param count 数量
* @return 消息 Map
*/
@GetMapping("/stream/group/read-new")
public Map<String, Object> streamReadGroupNew(@RequestParam String streamKey,
@RequestParam String groupName,
@RequestParam String consumerName,
@RequestParam(defaultValue = "10") Integer count) {
checkKey(streamKey);
checkKey(groupName);
checkKey(consumerName);
Assert.isTrue(ObjectUtil.isNotNull(count) && count > 0, "count必须大于0");
StreamReadGroupArgs args = StreamReadGroupArgs.neverDelivered().count(count);
Map<StreamMessageId, Map<Object, Object>> data = redissonService.streamReadGroup(
streamKey,
groupName,
consumerName,
args
);
return ok(streamMapToResponse(data));
}
/**
* 确认 Stream 消息。
*
* curl -X POST "http://localhost:8080/redisson/message/stream/ack?streamKey=stream:test&groupName=group:test&ids=1714096800000-0"
*
* @param streamKey Stream Key
* @param groupName 消费组
* @param ids 消息 ID 集合
* @return 确认数量
*/
@PostMapping("/stream/ack")
public Map<String, Object> streamAck(@RequestParam String streamKey,
@RequestParam String groupName,
@RequestParam List<String> ids) {
checkKey(streamKey);
checkKey(groupName);
Assert.isTrue(CollUtil.isNotEmpty(ids), "ids不能为空");
StreamMessageId[] messageIds = ids.stream()
.map(this::parseStreamMessageId)
.toArray(StreamMessageId[]::new);
long count = redissonService.streamAck(streamKey, groupName, messageIds);
return ok(count);
}
/**
* 删除 Stream 消息。
*
* curl -X DELETE "http://localhost:8080/redisson/message/stream/remove?streamKey=stream:test&ids=1714096800000-0"
*
* @param streamKey Stream Key
* @param ids 消息 ID 集合
* @return 删除数量
*/
@DeleteMapping("/stream/remove")
public Map<String, Object> streamRemove(@RequestParam String streamKey,
@RequestParam List<String> ids) {
checkKey(streamKey);
Assert.isTrue(CollUtil.isNotEmpty(ids), "ids不能为空");
StreamMessageId[] messageIds = ids.stream()
.map(this::parseStreamMessageId)
.toArray(StreamMessageId[]::new);
long count = redissonService.streamRemove(streamKey, messageIds);
return ok(count);
}
/**
* 获取 Stream 长度。
*
* curl "http://localhost:8080/redisson/message/stream/size?streamKey=stream:test"
*
* @param streamKey Stream Key
* @return 长度
*/
@GetMapping("/stream/size")
public Map<String, Object> streamSize(@RequestParam String streamKey) {
checkKey(streamKey);
long size = redissonService.streamSize(streamKey);
return ok(size);
}
/**
* 模拟延迟队列消费者。
* 调用后会阻塞等待一条消息,适合本地测试,不建议生产接口直接暴露。
*
* curl "http://localhost:8080/redisson/message/debug/delayed-queue/take?queueKey=queue:delay:test&timeoutSeconds=30"
*
* @param queueKey 队列 Key
* @param timeoutSeconds 等待秒数
* @return 消费到的消息
* @throws InterruptedException 线程中断时抛出
*/
@GetMapping("/debug/delayed-queue/take")
public Map<String, Object> debugDelayedQueueTake(@RequestParam String queueKey,
@RequestParam(defaultValue = "30") Long timeoutSeconds) throws InterruptedException {
checkKey(queueKey);
Assert.isTrue(ObjectUtil.isNotNull(timeoutSeconds) && timeoutSeconds >= 0, "timeoutSeconds不能小于0");
Object value = redissonService.getBlockingQueue(queueKey).poll(timeoutSeconds, TimeUnit.SECONDS);
return ok(value);
}
/**
* 模拟阻塞消费者。
* 调用后会阻塞等待一条消息,适合本地测试,不建议生产接口直接暴露。
*
* curl "http://localhost:8080/redisson/message/debug/blocking-queue/take?queueKey=queue:blocking:test&timeoutSeconds=30"
*
* @param queueKey 队列 Key
* @param timeoutSeconds 等待秒数
* @return 消费到的消息
* @throws InterruptedException 线程中断时抛出
*/
@GetMapping("/debug/blocking-queue/take")
public Map<String, Object> debugBlockingQueueTake(@RequestParam String queueKey,
@RequestParam(defaultValue = "30") Long timeoutSeconds) throws InterruptedException {
checkKey(queueKey);
Assert.isTrue(ObjectUtil.isNotNull(timeoutSeconds) && timeoutSeconds >= 0, "timeoutSeconds不能小于0");
Object value = redissonService.dequeueBlocking(queueKey, timeoutSeconds, TimeUnit.SECONDS);
return ok(value);
}
/**
* 校验 Redis Key。
*
* @param key Redis 键
*/
private void checkKey(String key) {
Assert.isTrue(StrUtil.isNotBlank(key), "Redis Key不能为空");
}
/**
* 移除监听器 ID。
*
* @param listenerMap 监听器 Map
* @param key 监听 Key
* @param listenerId 监听器 ID
*/
private void removeListenerId(Map<String, List<Integer>> listenerMap, String key, Integer listenerId) {
List<Integer> listenerIds = listenerMap.get(key);
if (CollUtil.isEmpty(listenerIds)) {
return;
}
listenerIds.remove(listenerId);
if (CollUtil.isEmpty(listenerIds)) {
listenerMap.remove(key);
}
}
/**
* 解析 Stream 消息 ID。
*
* @param id ID 文本,格式为 0-0
* @return StreamMessageId
*/
private StreamMessageId parseStreamMessageId(String id) {
Assert.isTrue(StrUtil.isNotBlank(id), "Stream消息ID不能为空");
List<String> parts = StrUtil.split(id, "-");
Assert.isTrue(parts.size() == 2, "Stream消息ID格式错误,正确格式如 0-0");
long first = Long.parseLong(parts.get(0));
long second = Long.parseLong(parts.get(1));
return new StreamMessageId(first, second);
}
/**
* StreamMessageId 转换为 Map。
*
* @param id StreamMessageId
* @return Map
*/
private Map<String, Object> idToMap(StreamMessageId id) {
if (ObjectUtil.isNull(id)) {
return null;
}
Map<String, Object> data = new LinkedHashMap<>();
data.put("id", id.toString());
return data;
}
/**
* Stream 消息转换为响应结构。
*
* @param streamMap Stream 消息 Map
* @return 响应 Map
*/
private Map<String, Object> streamMapToResponse(Map<StreamMessageId, Map<Object, Object>> streamMap) {
if (CollUtil.isEmpty(streamMap)) {
return new LinkedHashMap<>();
}
Map<String, Object> result = new LinkedHashMap<>();
streamMap.forEach((id, body) -> result.put(id.toString(), body));
return result;
}
/**
* 成功响应。
*
* @param data 响应数据
* @return 响应 Map
*/
private Map<String, Object> ok(Object data) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("code", 0);
result.put("message", "操作成功");
result.put("data", data);
return result;
}
/**
* Stream 添加请求体。
*
* @author Ateng
* @since 2026-04-26
*/
@Data
public static class StreamAddRequest {
/**
* Stream Key。
*/
private String streamKey;
/**
* 消息字段。
*/
private Map<Object, Object> entries;
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
场景能力测试控制器
用于演示 RedissonService 的 BitSet 签到、HyperLogLog UV、Geo 地理位置、分布式 Session 等能力。
package local.ateng.java.redis.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import local.ateng.java.redis.service.RedissonService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.GeoPosition;
import org.redisson.api.GeoUnit;
import org.redisson.api.geo.GeoSearchArgs;
import org.springframework.web.bind.annotation.*;
import java.time.Duration;
import java.time.LocalDate;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Redisson 场景能力测试控制器
* 用于演示 RedissonService 的 BitSet 签到、HyperLogLog UV、Geo 地理位置、分布式 Session 等能力。
*
* @author Ateng
* @since 2026-04-26
*/
@Slf4j
@RestController
@RequestMapping("/redisson/scene")
@RequiredArgsConstructor
public class RedissonSceneController {
private final RedissonService redissonService;
// -------------------------------------------------------------------------
// BitSet / 签到
// -------------------------------------------------------------------------
/**
* 设置位图指定位置。
*
* curl -X POST "http://localhost:8080/redisson/scene/bit/set?key=bit:test&index=1&value=true"
*
* @param key Redis 键
* @param index 位索引
* @param value 位值
* @return 执行结果
*/
@PostMapping("/bit/set")
public Map<String, Object> bitSet(@RequestParam String key,
@RequestParam Long index,
@RequestParam Boolean value) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(index) && index >= 0, "index不能小于0");
Assert.notNull(value, "value不能为空");
redissonService.bitSet(key, index, value);
log.info("BitSet 设置成功,key={},index={},value={}", key, index, value);
return ok(true);
}
/**
* 获取位图指定位置。
*
* curl "http://localhost:8080/redisson/scene/bit/get?key=bit:test&index=1"
*
* @param key Redis 键
* @param index 位索引
* @return 位值
*/
@GetMapping("/bit/get")
public Map<String, Object> bitGet(@RequestParam String key,
@RequestParam Long index) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(index) && index >= 0, "index不能小于0");
boolean value = redissonService.bitGet(key, index);
return ok(value);
}
/**
* 获取位图 true 数量。
*
* curl "http://localhost:8080/redisson/scene/bit/count?key=bit:test"
*
* @param key Redis 键
* @return 数量
*/
@GetMapping("/bit/count")
public Map<String, Object> bitCount(@RequestParam String key) {
checkKey(key);
long count = redissonService.bitCount(key);
return ok(count);
}
/**
* 清空位图。
*
* curl -X DELETE "http://localhost:8080/redisson/scene/bit/clear?key=bit:test"
*
* @param key Redis 键
* @return 执行结果
*/
@DeleteMapping("/bit/clear")
public Map<String, Object> bitClear(@RequestParam String key) {
checkKey(key);
redissonService.bitClear(key);
log.info("BitSet 清空成功,key={}", key);
return ok(true);
}
/**
* 用户签到。
*
* curl -X POST "http://localhost:8080/redisson/scene/sign/do?keyPrefix=sign&userId=1001&date=2026-04-26"
*
* @param keyPrefix 业务 key 前缀
* @param userId 用户 ID
* @param date 日期,不传默认今天
* @return 执行结果
*/
@PostMapping("/sign/do")
public Map<String, Object> sign(@RequestParam(defaultValue = "sign") String keyPrefix,
@RequestParam String userId,
@RequestParam(required = false) String date) {
checkKey(keyPrefix);
checkKey(userId);
LocalDate targetDate = parseDate(date);
redissonService.sign(keyPrefix, userId, targetDate);
log.info("用户签到成功,keyPrefix={},userId={},date={}", keyPrefix, userId, targetDate);
return ok(true);
}
/**
* 判断用户是否签到。
*
* curl "http://localhost:8080/redisson/scene/sign/check?keyPrefix=sign&userId=1001&date=2026-04-26"
*
* @param keyPrefix 业务 key 前缀
* @param userId 用户 ID
* @param date 日期,不传默认今天
* @return 是否签到
*/
@GetMapping("/sign/check")
public Map<String, Object> isSigned(@RequestParam(defaultValue = "sign") String keyPrefix,
@RequestParam String userId,
@RequestParam(required = false) String date) {
checkKey(keyPrefix);
checkKey(userId);
LocalDate targetDate = parseDate(date);
boolean signed = redissonService.isSigned(keyPrefix, userId, targetDate);
return ok(signed);
}
/**
* 获取用户指定年份签到天数。
*
* curl "http://localhost:8080/redisson/scene/sign/count?keyPrefix=sign&userId=1001&year=2026"
*
* @param keyPrefix 业务 key 前缀
* @param userId 用户 ID
* @param year 年份,不传默认当前年
* @return 签到天数
*/
@GetMapping("/sign/count")
public Map<String, Object> getSignCount(@RequestParam(defaultValue = "sign") String keyPrefix,
@RequestParam String userId,
@RequestParam(required = false) Integer year) {
checkKey(keyPrefix);
checkKey(userId);
int targetYear = ObjectUtil.defaultIfNull(year, LocalDate.now().getYear());
long count = redissonService.getSignCount(keyPrefix, userId, targetYear);
return ok(count);
}
/**
* 获取用户连续签到天数。
*
* curl "http://localhost:8080/redisson/scene/sign/continuous?keyPrefix=sign&userId=1001&date=2026-04-26"
*
* @param keyPrefix 业务 key 前缀
* @param userId 用户 ID
* @param date 日期,不传默认今天
* @return 连续签到天数
*/
@GetMapping("/sign/continuous")
public Map<String, Object> getContinuousSignCount(@RequestParam(defaultValue = "sign") String keyPrefix,
@RequestParam String userId,
@RequestParam(required = false) String date) {
checkKey(keyPrefix);
checkKey(userId);
LocalDate targetDate = parseDate(date);
int count = redissonService.getContinuousSignCount(keyPrefix, userId, targetDate);
return ok(count);
}
// -------------------------------------------------------------------------
// HyperLogLog / UV
// -------------------------------------------------------------------------
/**
* 添加 HyperLogLog 元素。
*
* curl -X POST "http://localhost:8080/redisson/scene/hll/add?key=hll:test" \
* -H "Content-Type: application/json" \
* -d '"user:1"'
*
* @param key Redis 键
* @param value 元素
* @return 是否改变基数估算
*/
@PostMapping("/hll/add")
public Map<String, Object> hllAdd(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
Assert.notNull(value, "value不能为空");
boolean success = redissonService.hllAdd(key, value);
return ok(success);
}
/**
* 批量添加 HyperLogLog 元素。
*
* curl -X POST "http://localhost:8080/redisson/scene/hll/add-all?key=hll:test" \
* -H "Content-Type: application/json" \
* -d '["user:1","user:2","user:3"]'
*
* @param key Redis 键
* @param values 元素集合
* @return 是否改变基数估算
*/
@PostMapping("/hll/add-all")
public Map<String, Object> hllAddAll(@RequestParam String key,
@RequestBody List<Object> values) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(values), "values不能为空");
boolean success = redissonService.hllAddAll(key, values);
return ok(success);
}
/**
* 获取 HyperLogLog 基数估算。
*
* curl "http://localhost:8080/redisson/scene/hll/count?key=hll:test"
*
* @param key Redis 键
* @return 基数估算
*/
@GetMapping("/hll/count")
public Map<String, Object> hllCount(@RequestParam String key) {
checkKey(key);
long count = redissonService.hllCount(key);
return ok(count);
}
/**
* 合并 HyperLogLog。
*
* curl -X POST "http://localhost:8080/redisson/scene/hll/merge?destKey=hll:merge&keys=hll:a&keys=hll:b"
*
* @param destKey 目标 Key
* @param keys 源 Key 集合
* @return 合并后基数估算
*/
@PostMapping("/hll/merge")
public Map<String, Object> hllMerge(@RequestParam String destKey,
@RequestParam List<String> keys) {
checkKey(destKey);
Assert.isTrue(CollUtil.isNotEmpty(keys), "keys不能为空");
long count = redissonService.hllMerge(destKey, keys.toArray(new String[0]));
return ok(count);
}
/**
* 记录 UV。
*
* curl -X POST "http://localhost:8080/redisson/scene/uv/record?keyPrefix=uv&bizKey=home&userFlag=user:1&date=2026-04-26"
*
* @param keyPrefix 业务 key 前缀
* @param bizKey 业务标识
* @param userFlag 用户唯一标识
* @param date 日期,不传默认今天
* @return 是否改变基数估算
*/
@PostMapping("/uv/record")
public Map<String, Object> uvRecord(@RequestParam(defaultValue = "uv") String keyPrefix,
@RequestParam String bizKey,
@RequestParam String userFlag,
@RequestParam(required = false) String date) {
checkKey(keyPrefix);
checkKey(bizKey);
checkKey(userFlag);
LocalDate targetDate = parseDate(date);
boolean success = redissonService.uvRecord(keyPrefix, bizKey, userFlag, targetDate);
log.info("UV 记录成功,keyPrefix={},bizKey={},userFlag={},date={}",
keyPrefix, bizKey, userFlag, targetDate);
return ok(success);
}
/**
* 获取 UV。
*
* curl "http://localhost:8080/redisson/scene/uv/count?keyPrefix=uv&bizKey=home&date=2026-04-26"
*
* @param keyPrefix 业务 key 前缀
* @param bizKey 业务标识
* @param date 日期,不传默认今天
* @return UV 数量
*/
@GetMapping("/uv/count")
public Map<String, Object> uvCount(@RequestParam(defaultValue = "uv") String keyPrefix,
@RequestParam String bizKey,
@RequestParam(required = false) String date) {
checkKey(keyPrefix);
checkKey(bizKey);
LocalDate targetDate = parseDate(date);
long count = redissonService.uvCount(keyPrefix, bizKey, targetDate);
return ok(count);
}
// -------------------------------------------------------------------------
// Geo / LBS
// -------------------------------------------------------------------------
/**
* 添加地理位置。
*
* curl -X POST "http://localhost:8080/redisson/scene/geo/add?key=geo:store&member=store:1&longitude=116.397128&latitude=39.916527"
*
* @param key Redis 键
* @param member 成员
* @param longitude 经度
* @param latitude 纬度
* @return 添加数量
*/
@PostMapping("/geo/add")
public Map<String, Object> geoAdd(@RequestParam String key,
@RequestParam String member,
@RequestParam Double longitude,
@RequestParam Double latitude) {
checkKey(key);
checkKey(member);
checkGeoCoordinate(longitude, latitude);
long count = redissonService.geoAdd(key, longitude, latitude, member);
log.info("Geo 添加成功,key={},member={},longitude={},latitude={}", key, member, longitude, latitude);
return ok(count);
}
/**
* 成员不存在时添加地理位置。
*
* curl -X POST "http://localhost:8080/redisson/scene/geo/try-add?key=geo:store&member=store:2&longitude=116.407128&latitude=39.926527"
*
* @param key Redis 键
* @param member 成员
* @param longitude 经度
* @param latitude 纬度
* @return 是否添加成功
*/
@PostMapping("/geo/try-add")
public Map<String, Object> geoTryAdd(@RequestParam String key,
@RequestParam String member,
@RequestParam Double longitude,
@RequestParam Double latitude) {
checkKey(key);
checkKey(member);
checkGeoCoordinate(longitude, latitude);
boolean success = redissonService.geoTryAdd(key, longitude, latitude, member);
return ok(success);
}
/**
* 计算两个成员距离。
*
* curl "http://localhost:8080/redisson/scene/geo/distance?key=geo:store&member1=store:1&member2=store:2&unit=KILOMETERS"
*
* @param key Redis 键
* @param member1 成员 1
* @param member2 成员 2
* @param unit 距离单位
* @return 距离
*/
@GetMapping("/geo/distance")
public Map<String, Object> geoDistance(@RequestParam String key,
@RequestParam String member1,
@RequestParam String member2,
@RequestParam(defaultValue = "KILOMETERS") GeoUnit unit) {
checkKey(key);
checkKey(member1);
checkKey(member2);
Assert.notNull(unit, "unit不能为空");
Double distance = redissonService.geoDistance(key, member1, member2, unit);
return ok(distance);
}
/**
* 查询成员 GeoHash。
*
* curl "http://localhost:8080/redisson/scene/geo/hash?key=geo:store&members=store:1&members=store:2"
*
* @param key Redis 键
* @param members 成员集合
* @return GeoHash Map
*/
@GetMapping("/geo/hash")
public Map<String, Object> geoHash(@RequestParam String key,
@RequestParam List<String> members) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(members), "members不能为空");
Map<Object, String> data = redissonService.geoHash(key, members.toArray());
return ok(data);
}
/**
* 查询成员坐标。
*
* curl "http://localhost:8080/redisson/scene/geo/position?key=geo:store&members=store:1&members=store:2"
*
* @param key Redis 键
* @param members 成员集合
* @return 坐标 Map
*/
@GetMapping("/geo/position")
public Map<String, Object> geoPosition(@RequestParam String key,
@RequestParam List<String> members) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(members), "members不能为空");
Map<Object, GeoPosition> data = redissonService.geoPosition(key, members.toArray());
return ok(data);
}
/**
* 根据坐标查询附近成员。
*
* curl "http://localhost:8080/redisson/scene/geo/search?key=geo:store&longitude=116.397128&latitude=39.916527&radius=5&unit=KILOMETERS"
*
* @param key Redis 键
* @param longitude 经度
* @param latitude 纬度
* @param radius 半径
* @param unit 距离单位
* @return 附近成员
*/
@GetMapping("/geo/search")
public Map<String, Object> geoSearch(@RequestParam String key,
@RequestParam Double longitude,
@RequestParam Double latitude,
@RequestParam Double radius,
@RequestParam(defaultValue = "KILOMETERS") GeoUnit unit) {
checkKey(key);
checkGeoCoordinate(longitude, latitude);
Assert.isTrue(ObjectUtil.isNotNull(radius) && radius > 0, "radius必须大于0");
Assert.notNull(unit, "unit不能为空");
GeoSearchArgs args = GeoSearchArgs.from(longitude, latitude)
.radius(radius, unit);
List<Object> data = redissonService.geoSearch(key, args);
return ok(data);
}
/**
* 根据坐标查询附近成员及距离。
*
* curl "http://localhost:8080/redisson/scene/geo/search-with-distance?key=geo:store&longitude=116.397128&latitude=39.916527&radius=5&unit=KILOMETERS"
*
* @param key Redis 键
* @param longitude 经度
* @param latitude 纬度
* @param radius 半径
* @param unit 距离单位
* @return 成员距离 Map
*/
@GetMapping("/geo/search-with-distance")
public Map<String, Object> geoSearchWithDistance(@RequestParam String key,
@RequestParam Double longitude,
@RequestParam Double latitude,
@RequestParam Double radius,
@RequestParam(defaultValue = "KILOMETERS") GeoUnit unit) {
checkKey(key);
checkGeoCoordinate(longitude, latitude);
Assert.isTrue(ObjectUtil.isNotNull(radius) && radius > 0, "radius必须大于0");
Assert.notNull(unit, "unit不能为空");
GeoSearchArgs args = GeoSearchArgs.from(longitude, latitude)
.radius(radius, unit);
Map<Object, Double> data = redissonService.geoSearchWithDistance(key, args);
return ok(data);
}
/**
* 根据坐标查询附近成员及坐标。
*
* curl "http://localhost:8080/redisson/scene/geo/search-with-position?key=geo:store&longitude=116.397128&latitude=39.916527&radius=5&unit=KILOMETERS"
*
* @param key Redis 键
* @param longitude 经度
* @param latitude 纬度
* @param radius 半径
* @param unit 距离单位
* @return 成员坐标 Map
*/
@GetMapping("/geo/search-with-position")
public Map<String, Object> geoSearchWithPosition(@RequestParam String key,
@RequestParam Double longitude,
@RequestParam Double latitude,
@RequestParam Double radius,
@RequestParam(defaultValue = "KILOMETERS") GeoUnit unit) {
checkKey(key);
checkGeoCoordinate(longitude, latitude);
Assert.isTrue(ObjectUtil.isNotNull(radius) && radius > 0, "radius必须大于0");
Assert.notNull(unit, "unit不能为空");
GeoSearchArgs args = GeoSearchArgs.from(longitude, latitude)
.radius(radius, unit);
Map<Object, GeoPosition> data = redissonService.geoSearchWithPosition(key, args);
return ok(data);
}
/**
* 删除地理位置成员。
*
* curl -X DELETE "http://localhost:8080/redisson/scene/geo/remove?key=geo:store&members=store:1&members=store:2"
*
* @param key Redis 键
* @param members 成员集合
* @return 删除数量
*/
@DeleteMapping("/geo/remove")
public Map<String, Object> geoRemove(@RequestParam String key,
@RequestParam List<String> members) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(members), "members不能为空");
long count = redissonService.geoRemove(key, members.toArray());
log.info("Geo 成员删除成功,key={},members={},count={}", key, members, count);
return ok(count);
}
// -------------------------------------------------------------------------
// Session
// -------------------------------------------------------------------------
/**
* 创建或覆盖会话。
*
* curl -X POST "http://localhost:8080/redisson/scene/session/set?keyPrefix=session&token=token001&ttlSeconds=1800" \
* -H "Content-Type: application/json" \
* -d '{"userId":1001,"username":"ateng"}'
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @param ttlSeconds 过期秒数
* @param session 会话对象
* @return 执行结果
*/
@PostMapping("/session/set")
public Map<String, Object> sessionSet(@RequestParam(defaultValue = "session") String keyPrefix,
@RequestParam String token,
@RequestParam(defaultValue = "1800") Long ttlSeconds,
@RequestBody Map<String, Object> session) {
checkKey(keyPrefix);
checkKey(token);
Assert.isTrue(ObjectUtil.isNotNull(ttlSeconds) && ttlSeconds > 0, "ttlSeconds必须大于0");
Assert.isTrue(CollUtil.isNotEmpty(session), "session不能为空");
redissonService.sessionSet(keyPrefix, token, session, Duration.ofSeconds(ttlSeconds));
log.info("Session 写入成功,keyPrefix={},token={},ttlSeconds={}", keyPrefix, token, ttlSeconds);
return ok(true);
}
/**
* 获取会话。
*
* curl "http://localhost:8080/redisson/scene/session/get?keyPrefix=session&token=token001"
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @return 会话对象
*/
@GetMapping("/session/get")
public Map<String, Object> sessionGet(@RequestParam(defaultValue = "session") String keyPrefix,
@RequestParam String token) {
checkKey(keyPrefix);
checkKey(token);
Object session = redissonService.sessionGet(keyPrefix, token, Object.class);
return ok(session);
}
/**
* 刷新会话过期时间。
*
* curl -X PUT "http://localhost:8080/redisson/scene/session/refresh?keyPrefix=session&token=token001&ttlSeconds=1800"
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @param ttlSeconds 过期秒数
* @return 是否刷新成功
*/
@PutMapping("/session/refresh")
public Map<String, Object> sessionRefresh(@RequestParam(defaultValue = "session") String keyPrefix,
@RequestParam String token,
@RequestParam(defaultValue = "1800") Long ttlSeconds) {
checkKey(keyPrefix);
checkKey(token);
Assert.isTrue(ObjectUtil.isNotNull(ttlSeconds) && ttlSeconds > 0, "ttlSeconds必须大于0");
boolean success = redissonService.sessionRefresh(keyPrefix, token, Duration.ofSeconds(ttlSeconds));
return ok(success);
}
/**
* 删除会话。
*
* curl -X DELETE "http://localhost:8080/redisson/scene/session/delete?keyPrefix=session&token=token001"
*
* @param keyPrefix 会话 key 前缀
* @param token Token
* @return 是否删除成功
*/
@DeleteMapping("/session/delete")
public Map<String, Object> sessionDelete(@RequestParam(defaultValue = "session") String keyPrefix,
@RequestParam String token) {
checkKey(keyPrefix);
checkKey(token);
boolean success = redissonService.sessionDelete(keyPrefix, token);
log.info("Session 删除完成,keyPrefix={},token={},success={}", keyPrefix, token, success);
return ok(success);
}
/**
* 校验 Redis Key。
*
* @param key Redis 键
*/
private void checkKey(String key) {
Assert.isTrue(StrUtil.isNotBlank(key), "Redis Key不能为空");
}
/**
* 解析日期。
*
* @param date 日期文本
* @return 日期
*/
private LocalDate parseDate(String date) {
if (StrUtil.isBlank(date)) {
return LocalDate.now();
}
return LocalDate.parse(date);
}
/**
* 校验 Geo 坐标。
*
* @param longitude 经度
* @param latitude 纬度
*/
private void checkGeoCoordinate(Double longitude, Double latitude) {
Assert.notNull(longitude, "longitude不能为空");
Assert.notNull(latitude, "latitude不能为空");
Assert.isTrue(longitude >= -180D && longitude <= 180D, "经度必须在 -180 到 180 之间");
Assert.isTrue(latitude >= -90D && latitude <= 90D, "纬度必须在 -90 到 90 之间");
}
/**
* 成功响应。
*
* @param data 响应数据
* @return 响应 Map
*/
private Map<String, Object> ok(Object data) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("code", 0);
result.put("message", "操作成功");
result.put("data", data);
return result;
}
/**
* Geo 批量添加请求体。
*
* @author Ateng
* @since 2026-04-26
*/
@Data
public static class GeoAddBatchRequest {
/**
* Redis 键。
*/
private String key;
/**
* 地理位置集合。
*/
private List<GeoItem> items;
}
/**
* Geo 位置项。
*
* @author Ateng
* @since 2026-04-26
*/
@Data
public static class GeoItem {
/**
* 成员。
*/
private String member;
/**
* 经度。
*/
private Double longitude;
/**
* 纬度。
*/
private Double latitude;
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
高级能力测试控制器
用于演示 RedissonService 的 Lua 脚本、脚本缓存、批处理、事务等高级能力。
package local.ateng.java.redis.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import local.ateng.java.redis.service.RedissonService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.BatchResult;
import org.redisson.api.RBatch;
import org.redisson.api.RBucket;
import org.redisson.api.RScript;
import org.redisson.api.RTransaction;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Redisson 高级能力测试控制器
* 用于演示 RedissonService 的 Lua 脚本、脚本缓存、批处理、事务等高级能力。
*
* @author Ateng
* @since 2026-04-26
*/
@Slf4j
@RestController
@RequestMapping("/redisson/advanced")
@RequiredArgsConstructor
public class RedissonAdvancedController {
private final RedissonService redissonService;
// -------------------------------------------------------------------------
// Lua / Script
// -------------------------------------------------------------------------
/**
* 执行 Lua 脚本。
*
* curl -X POST "http://localhost:8080/redisson/advanced/lua/eval" \
* -H "Content-Type: application/json" \
* -d '{"script":"return redis.call(\"get\", KEYS[1])","mode":"READ_ONLY","returnType":"VALUE","keys":["lua:test"],"args":[]}'
*
* @param request Lua 执行请求
* @return 执行结果
*/
@PostMapping("/lua/eval")
public Map<String, Object> eval(@RequestBody LuaEvalRequest request) {
checkLuaEvalRequest(request);
Object result = redissonService.eval(
request.getScript(),
request.getMode(),
request.getReturnType(),
safeKeys(request.getKeys()),
safeArgs(request.getArgs())
);
return ok(result);
}
/**
* 执行 Lua 脚本并按 Object 返回。
*
* curl -X POST "http://localhost:8080/redisson/advanced/lua/eval-value" \
* -H "Content-Type: application/json" \
* -d '{"script":"redis.call(\"set\", KEYS[1], ARGV[1]); return redis.call(\"get\", KEYS[1])","keys":["lua:test"],"args":["hello"]}'
*
* @param request Lua 执行请求
* @return 执行结果
*/
@PostMapping("/lua/eval-value")
public Map<String, Object> evalValue(@RequestBody LuaSimpleEvalRequest request) {
checkLuaSimpleEvalRequest(request);
Object result = redissonService.eval(
request.getScript(),
Object.class,
safeKeys(request.getKeys()),
safeArgs(request.getArgs())
);
return ok(result);
}
/**
* 执行 Lua 脚本但不关心返回值。
*
* curl -X POST "http://localhost:8080/redisson/advanced/lua/eval-no-result" \
* -H "Content-Type: application/json" \
* -d '{"script":"redis.call(\"set\", KEYS[1], ARGV[1])","keys":["lua:test"],"args":["hello"]}'
*
* @param request Lua 执行请求
* @return 执行结果
*/
@PostMapping("/lua/eval-no-result")
public Map<String, Object> evalNoResult(@RequestBody LuaSimpleEvalRequest request) {
checkLuaSimpleEvalRequest(request);
redissonService.evalNoResult(
request.getScript(),
safeKeys(request.getKeys()),
safeArgs(request.getArgs())
);
return ok(true);
}
/**
* 加载 Lua 脚本。
*
* curl -X POST "http://localhost:8080/redisson/advanced/lua/load" \
* -H "Content-Type: text/plain" \
* -d 'return redis.call("get", KEYS[1])'
*
* @param script Lua 脚本
* @return SHA1
*/
@PostMapping("/lua/load")
public Map<String, Object> loadScript(@RequestBody String script) {
Assert.isTrue(StrUtil.isNotBlank(script), "Lua脚本不能为空");
String sha1 = redissonService.loadScript(script);
log.info("Lua 脚本加载成功,sha1={}", sha1);
return ok(sha1);
}
/**
* 根据 SHA1 执行 Lua 脚本。
*
* curl -X POST "http://localhost:8080/redisson/advanced/lua/eval-sha" \
* -H "Content-Type: application/json" \
* -d '{"sha1":"xxxx","keys":["lua:test"],"args":[]}'
*
* @param request Lua SHA 执行请求
* @return 执行结果
*/
@PostMapping("/lua/eval-sha")
public Map<String, Object> evalBySha(@RequestBody LuaEvalShaRequest request) {
Assert.notNull(request, "请求体不能为空");
Assert.isTrue(StrUtil.isNotBlank(request.getSha1()), "sha1不能为空");
Object result = redissonService.evalBySha(
request.getSha1(),
Object.class,
safeKeys(request.getKeys()),
safeArgs(request.getArgs())
);
return ok(result);
}
/**
* 使用 Lua 实现原子设置并返回旧值。
*
* curl -X POST "http://localhost:8080/redisson/advanced/lua/get-and-set?key=lua:atomic&value=newValue"
*
* @param key Redis 键
* @param value 新值
* @return 旧值
*/
@PostMapping("/lua/get-and-set")
public Map<String, Object> luaGetAndSet(@RequestParam String key,
@RequestParam String value) {
checkKey(key);
Assert.notNull(value, "value不能为空");
String script = """
local oldValue = redis.call('get', KEYS[1])
redis.call('set', KEYS[1], ARGV[1])
return oldValue
""";
Object oldValue = redissonService.eval(
script,
RScript.Mode.READ_WRITE,
RScript.ReturnType.VALUE,
List.of(key),
value
);
return ok(oldValue);
}
/**
* 使用 Lua 实现存在则递增,不存在则初始化。
*
* curl -X POST "http://localhost:8080/redisson/advanced/lua/increment-or-init?key=lua:counter&delta=2&initialValue=100"
*
* @param key Redis 键
* @param delta 增量
* @param initialValue 初始值
* @return 最新值
*/
@PostMapping("/lua/increment-or-init")
public Map<String, Object> luaIncrementOrInit(@RequestParam String key,
@RequestParam(defaultValue = "1") Long delta,
@RequestParam(defaultValue = "0") Long initialValue) {
checkKey(key);
Assert.notNull(delta, "delta不能为空");
Assert.notNull(initialValue, "initialValue不能为空");
String script = """
if redis.call('exists', KEYS[1]) == 1 then
return redis.call('incrby', KEYS[1], ARGV[1])
else
redis.call('set', KEYS[1], ARGV[2])
return tonumber(ARGV[2])
end
""";
Object value = redissonService.eval(
script,
RScript.Mode.READ_WRITE,
RScript.ReturnType.INTEGER,
List.of(key),
delta,
initialValue
);
return ok(value);
}
// -------------------------------------------------------------------------
// Batch
// -------------------------------------------------------------------------
/**
* 批量设置 Bucket 值。
*
* curl -X POST "http://localhost:8080/redisson/advanced/batch/bucket-set" \
* -H "Content-Type: application/json" \
* -d '{"ttlSeconds":300,"items":[{"key":"batch:user:1","value":{"id":1,"name":"Ateng"}},{"key":"batch:user:2","value":{"id":2,"name":"Tom"}}]}'
*
* @param request 批量设置请求
* @return 批处理结果
*/
@PostMapping("/batch/bucket-set")
public Map<String, Object> batchBucketSet(@RequestBody BatchBucketSetRequest request) {
Assert.notNull(request, "请求体不能为空");
Assert.isTrue(CollUtil.isNotEmpty(request.getItems()), "items不能为空");
RBatch batch = redissonService.createBatch();
for (BucketItem item : request.getItems()) {
checkBucketItem(item);
if (ObjectUtil.isNotNull(request.getTtlSeconds()) && request.getTtlSeconds() > 0) {
batch.getBucket(item.getKey()).setAsync(item.getValue(), request.getTtlSeconds(), TimeUnit.SECONDS);
} else {
batch.getBucket(item.getKey()).setAsync(item.getValue());
}
}
BatchResult<?> result = batch.execute();
log.info("批量设置 Bucket 完成,size={}", request.getItems().size());
return ok(batchResultToMap(result));
}
/**
* 批量获取 Bucket 值。
*
* curl -X POST "http://localhost:8080/redisson/advanced/batch/bucket-get" \
* -H "Content-Type: application/json" \
* -d '{"keys":["batch:user:1","batch:user:2"]}'
*
* @param request 批量获取请求
* @return 批处理结果
*/
@PostMapping("/batch/bucket-get")
public Map<String, Object> batchBucketGet(@RequestBody BatchBucketGetRequest request) {
Assert.notNull(request, "请求体不能为空");
Assert.isTrue(CollUtil.isNotEmpty(request.getKeys()), "keys不能为空");
RBatch batch = redissonService.createBatch();
for (String key : request.getKeys()) {
checkKey(key);
batch.getBucket(key).getAsync();
}
BatchResult<?> result = batch.execute();
return ok(batchResultToMap(result));
}
/**
* 批量删除 Key。
*
* curl -X POST "http://localhost:8080/redisson/advanced/batch/key-delete" \
* -H "Content-Type: application/json" \
* -d '{"keys":["batch:user:1","batch:user:2"]}'
*
* @param request 批量删除请求
* @return 批处理结果
*/
@PostMapping("/batch/key-delete")
public Map<String, Object> batchKeyDelete(@RequestBody BatchBucketGetRequest request) {
Assert.notNull(request, "请求体不能为空");
Assert.isTrue(CollUtil.isNotEmpty(request.getKeys()), "keys不能为空");
RBatch batch = redissonService.createBatch();
for (String key : request.getKeys()) {
checkKey(key);
batch.getKeys().deleteAsync(key);
}
BatchResult<?> result = batch.execute();
log.info("批量删除 Key 完成,keys={}", request.getKeys());
return ok(batchResultToMap(result));
}
/**
* 批量计数器递增。
*
* curl -X POST "http://localhost:8080/redisson/advanced/batch/atomic-long-increment" \
* -H "Content-Type: application/json" \
* -d '{"items":[{"key":"batch:count:1","delta":1},{"key":"batch:count:2","delta":10}]}'
*
* @param request 批量递增请求
* @return 批处理结果
*/
@PostMapping("/batch/atomic-long-increment")
public Map<String, Object> batchAtomicLongIncrement(@RequestBody BatchAtomicLongIncrementRequest request) {
Assert.notNull(request, "请求体不能为空");
Assert.isTrue(CollUtil.isNotEmpty(request.getItems()), "items不能为空");
RBatch batch = redissonService.createBatch();
for (AtomicLongIncrementItem item : request.getItems()) {
Assert.notNull(item, "item不能为空");
checkKey(item.getKey());
Assert.notNull(item.getDelta(), "delta不能为空");
batch.getAtomicLong(item.getKey()).addAndGetAsync(item.getDelta());
}
BatchResult<?> result = batch.execute();
return ok(batchResultToMap(result));
}
// -------------------------------------------------------------------------
// Transaction
// -------------------------------------------------------------------------
/**
* 事务设置 Bucket 值。
*
* curl -X POST "http://localhost:8080/redisson/advanced/transaction/bucket-set" \
* -H "Content-Type: application/json" \
* -d '{"items":[{"key":"tx:user:1","value":{"id":1,"name":"Ateng"}},{"key":"tx:user:2","value":{"id":2,"name":"Tom"}}]}'
*
* @param request 事务设置请求
* @return 执行结果
*/
@PostMapping("/transaction/bucket-set")
public Map<String, Object> transactionBucketSet(@RequestBody BatchBucketSetRequest request) {
Assert.notNull(request, "请求体不能为空");
Assert.isTrue(CollUtil.isNotEmpty(request.getItems()), "items不能为空");
RTransaction transaction = redissonService.createTransaction();
try {
for (BucketItem item : request.getItems()) {
checkBucketItem(item);
RBucket<Object> bucket = transaction.getBucket(item.getKey());
if (ObjectUtil.isNotNull(request.getTtlSeconds()) && request.getTtlSeconds() > 0) {
bucket.set(item.getValue(), request.getTtlSeconds(), TimeUnit.SECONDS);
} else {
bucket.set(item.getValue());
}
}
transaction.commit();
log.info("事务设置 Bucket 成功,size={}", request.getItems().size());
return ok(true);
} catch (Exception e) {
rollbackSafely(transaction);
log.error("事务设置 Bucket 失败,已回滚", e);
throw e;
}
}
/**
* Lua 原子转账示例。
* 使用 Redis 数值 Key 模拟账户余额,from 扣减,to 增加。
*
* curl -X POST "http://localhost:8080/redisson/advanced/transaction/transfer?fromKey=account:1001&toKey=account:1002&amount=10"
*
* @param fromKey 转出账户 Key
* @param toKey 转入账户 Key
* @param amount 金额
* @return 执行结果
*/
@PostMapping("/transaction/transfer")
public Map<String, Object> transactionTransfer(@RequestParam String fromKey,
@RequestParam String toKey,
@RequestParam Long amount) {
checkKey(fromKey);
checkKey(toKey);
Assert.isTrue(ObjectUtil.isNotNull(amount) && amount > 0, "amount必须大于0");
String script = """
local amount = tonumber(ARGV[1])
if amount == nil or amount <= 0 then
return redis.error_reply('amount invalid')
end
local fromBalance = tonumber(redis.call('get', KEYS[1]) or '0')
if fromBalance < amount then
return redis.error_reply('balance not enough')
end
local latestFrom = redis.call('decrby', KEYS[1], amount)
local latestTo = redis.call('incrby', KEYS[2], amount)
return tostring(latestFrom) .. ':' .. tostring(latestTo)
""";
Object result = redissonService.eval(
script,
RScript.Mode.READ_WRITE,
RScript.ReturnType.VALUE,
List.of(fromKey, toKey),
amount
);
String resultText = String.valueOf(result);
List<String> parts = StrUtil.split(resultText, ":");
Assert.isTrue(parts.size() == 2, "Lua转账结果格式异常");
long latestFrom = Long.parseLong(parts.get(0));
long latestTo = Long.parseLong(parts.get(1));
Map<String, Object> data = new LinkedHashMap<>();
data.put("fromKey", fromKey);
data.put("toKey", toKey);
data.put("amount", amount);
data.put("fromBalance", latestFrom);
data.put("toBalance", latestTo);
log.info("Lua 原子转账成功,fromKey={},toKey={},amount={}", fromKey, toKey, amount);
return ok(data);
}
/**
* 初始化事务转账账户余额。
*
* curl -X POST "http://localhost:8080/redisson/advanced/transaction/account/init?accountKey=account:1001&balance=100"
*
* @param accountKey 账户 Key
* @param balance 余额
* @return 执行结果
*/
@PostMapping("/transaction/account/init")
public Map<String, Object> initAccount(@RequestParam String accountKey,
@RequestParam Long balance) {
checkKey(accountKey);
Assert.isTrue(ObjectUtil.isNotNull(balance) && balance >= 0, "balance不能小于0");
redissonService.setAtomicLong(accountKey, balance);
log.info("账户余额初始化成功,accountKey={},balance={}", accountKey, balance);
return ok(true);
}
/**
* 查询事务转账账户余额。
*
* curl "http://localhost:8080/redisson/advanced/transaction/account/balance?accountKey=account:1001"
*
* @param accountKey 账户 Key
* @return 余额
*/
@GetMapping("/transaction/account/balance")
public Map<String, Object> accountBalance(@RequestParam String accountKey) {
checkKey(accountKey);
long balance = redissonService.getAtomicLongValue(accountKey);
return ok(balance);
}
/**
* 获取批处理和事务对象信息。
*
* curl "http://localhost:8080/redisson/advanced/info"
*
* @return 对象信息
*/
@GetMapping("/info")
public Map<String, Object> info() {
Map<String, Object> data = new LinkedHashMap<>();
data.put("scriptClass", redissonService.getScript().getClass().getName());
data.put("batchClass", redissonService.createBatch().getClass().getName());
data.put("transactionClass", redissonService.createTransaction().getClass().getName());
return ok(data);
}
/**
* 校验 Redis Key。
*
* @param key Redis 键
*/
private void checkKey(String key) {
Assert.isTrue(StrUtil.isNotBlank(key), "Redis Key不能为空");
}
/**
* 校验 Bucket 项。
*
* @param item Bucket 项
*/
private void checkBucketItem(BucketItem item) {
Assert.notNull(item, "item不能为空");
checkKey(item.getKey());
Assert.notNull(item.getValue(), "value不能为空");
}
/**
* 获取安全 KEYS。
*
* @param keys KEYS
* @return KEYS
*/
private List<Object> safeKeys(List<Object> keys) {
if (CollUtil.isEmpty(keys)) {
return Collections.emptyList();
}
return keys;
}
/**
* 获取安全 ARGV。
*
* @param args ARGV
* @return ARGV 数组
*/
private Object[] safeArgs(List<Object> args) {
if (CollUtil.isEmpty(args)) {
return new Object[0];
}
return args.toArray();
}
/**
* 转换批处理结果。
*
* @param result 批处理结果
* @return 响应数据
*/
private Map<String, Object> batchResultToMap(BatchResult<?> result) {
Map<String, Object> data = new LinkedHashMap<>();
data.put("responses", ObjectUtil.isNull(result) ? Collections.emptyList() : result.getResponses());
return data;
}
/**
* 安全回滚事务。
*
* @param transaction 事务对象
*/
private void rollbackSafely(RTransaction transaction) {
if (ObjectUtil.isNull(transaction)) {
return;
}
try {
transaction.rollback();
} catch (Exception e) {
log.error("Redis 事务回滚异常", e);
}
}
/**
* 成功响应。
*
* @param data 响应数据
* @return 响应 Map
*/
private Map<String, Object> ok(Object data) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("code", 0);
result.put("message", "操作成功");
result.put("data", data);
return result;
}
/**
* Lua 完整执行请求。
*
* @author Ateng
* @since 2026-04-26
*/
@Data
public static class LuaEvalRequest {
/**
* Lua 脚本。
*/
private String script;
/**
* 执行模式。
*/
private RScript.Mode mode = RScript.Mode.READ_WRITE;
/**
* 返回类型。
*/
private RScript.ReturnType returnType = RScript.ReturnType.VALUE;
/**
* KEYS 参数。
*/
private List<Object> keys;
/**
* ARGV 参数。
*/
private List<Object> args;
}
/**
* Lua 简单执行请求。
*
* @author Ateng
* @since 2026-04-26
*/
@Data
public static class LuaSimpleEvalRequest {
/**
* Lua 脚本。
*/
private String script;
/**
* KEYS 参数。
*/
private List<Object> keys;
/**
* ARGV 参数。
*/
private List<Object> args;
}
/**
* Lua SHA 执行请求。
*
* @author Ateng
* @since 2026-04-26
*/
@Data
public static class LuaEvalShaRequest {
/**
* 脚本 SHA1。
*/
private String sha1;
/**
* KEYS 参数。
*/
private List<Object> keys;
/**
* ARGV 参数。
*/
private List<Object> args;
}
/**
* 批量 Bucket 设置请求。
*
* @author Ateng
* @since 2026-04-26
*/
@Data
public static class BatchBucketSetRequest {
/**
* 过期秒数。
*/
private Long ttlSeconds;
/**
* Bucket 项集合。
*/
private List<BucketItem> items;
}
/**
* Bucket 项。
*
* @author Ateng
* @since 2026-04-26
*/
@Data
public static class BucketItem {
/**
* Redis 键。
*/
private String key;
/**
* Redis 值。
*/
private Object value;
}
/**
* 批量 Bucket 获取请求。
*
* @author Ateng
* @since 2026-04-26
*/
@Data
public static class BatchBucketGetRequest {
/**
* Redis 键集合。
*/
private List<String> keys;
}
/**
* 批量 AtomicLong 递增请求。
*
* @author Ateng
* @since 2026-04-26
*/
@Data
public static class BatchAtomicLongIncrementRequest {
/**
* 递增项集合。
*/
private List<AtomicLongIncrementItem> items;
}
/**
* AtomicLong 递增项。
*
* @author Ateng
* @since 2026-04-26
*/
@Data
public static class AtomicLongIncrementItem {
/**
* Redis 键。
*/
private String key;
/**
* 增量。
*/
private Long delta;
}
/**
* 校验 Lua 完整执行请求。
*
* @param request Lua 请求
*/
private void checkLuaEvalRequest(LuaEvalRequest request) {
Assert.notNull(request, "请求体不能为空");
Assert.isTrue(StrUtil.isNotBlank(request.getScript()), "Lua脚本不能为空");
Assert.notNull(request.getMode(), "mode不能为空");
Assert.notNull(request.getReturnType(), "returnType不能为空");
}
/**
* 校验 Lua 简单执行请求。
*
* @param request Lua 请求
*/
private void checkLuaSimpleEvalRequest(LuaSimpleEvalRequest request) {
Assert.notNull(request, "请求体不能为空");
Assert.isTrue(StrUtil.isNotBlank(request.getScript()), "Lua脚本不能为空");
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
增强数据结构测试控制器
用于演示 RedissonService 的 LocalCachedMap、JsonBucket、BinaryStream、Multimap、SortedSet、LexSortedSet、Adder、有界队列和优先级队列能力。
package local.ateng.java.redis.controller;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import local.ateng.java.redis.service.RedissonService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.LocalCachedMapOptions;
import org.redisson.codec.JsonCodec;
import org.springframework.web.bind.annotation.*;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Redisson 增强数据结构测试控制器
* 用于演示 RedissonService 的 LocalCachedMap、JsonBucket、BinaryStream、Multimap、SortedSet、LexSortedSet、Adder、有界队列和优先级队列能力。
*
* @author Ateng
* @since 2026-04-27
*/
@Slf4j
@RestController
@RequestMapping("/redisson/enhanced")
@RequiredArgsConstructor
public class RedissonEnhancedDataController {
private final RedissonService redissonService;
/**
* JSON 编解码器。
* 建议在配置类中注入 JsonCodec Bean,例如 JsonJacksonCodec。
*/
private final JsonCodec jsonCodec;
// -------------------------------------------------------------------------
// LocalCachedMap
// -------------------------------------------------------------------------
/**
* 设置 LocalCachedMap 字段值。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/local-cache/put?key=lc:user&field=user:1" \
* -H "Content-Type: application/json" \
* -d '{"id":1,"name":"Ateng"}'
*
* @param key Redis 键
* @param field 字段
* @param value 字段值
* @return 执行结果
*/
@PostMapping("/local-cache/put")
public Map<String, Object> lcPut(@RequestParam String key,
@RequestParam String field,
@RequestBody Object value) {
checkKey(key);
checkKey(field);
redissonService.lcPut(key, field, value, localCachedMapOptions());
log.info("LocalCachedMap 写入成功,key={},field={}", key, field);
return ok(true);
}
/**
* 字段不存在时设置 LocalCachedMap 字段值。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/local-cache/put-if-absent?key=lc:user&field=user:1" \
* -H "Content-Type: application/json" \
* -d '{"id":1,"name":"Ateng"}'
*
* @param key Redis 键
* @param field 字段
* @param value 字段值
* @return 是否设置成功
*/
@PostMapping("/local-cache/put-if-absent")
public Map<String, Object> lcPutIfAbsent(@RequestParam String key,
@RequestParam String field,
@RequestBody Object value) {
checkKey(key);
checkKey(field);
boolean success = redissonService.lcPutIfAbsent(key, field, value, localCachedMapOptions());
return ok(success);
}
/**
* 获取 LocalCachedMap 字段值。
*
* curl "http://localhost:8080/redisson/enhanced/local-cache/get?key=lc:user&field=user:1"
*
* @param key Redis 键
* @param field 字段
* @return 字段值
*/
@GetMapping("/local-cache/get")
public Map<String, Object> lcGet(@RequestParam String key,
@RequestParam String field) {
checkKey(key);
checkKey(field);
Object value = redissonService.lcGet(key, field, Object.class, localCachedMapOptions());
return ok(value);
}
/**
* 删除 LocalCachedMap 字段。
*
* curl -X DELETE "http://localhost:8080/redisson/enhanced/local-cache/remove?key=lc:user&field=user:1"
*
* @param key Redis 键
* @param field 字段
* @return 删除前的值
*/
@DeleteMapping("/local-cache/remove")
public Map<String, Object> lcRemove(@RequestParam String key,
@RequestParam String field) {
checkKey(key);
checkKey(field);
Object oldValue = redissonService.lcRemove(key, field, localCachedMapOptions());
return ok(oldValue);
}
/**
* 判断 LocalCachedMap 字段是否存在。
*
* curl "http://localhost:8080/redisson/enhanced/local-cache/contains?key=lc:user&field=user:1"
*
* @param key Redis 键
* @param field 字段
* @return 是否存在
*/
@GetMapping("/local-cache/contains")
public Map<String, Object> lcContainsKey(@RequestParam String key,
@RequestParam String field) {
checkKey(key);
checkKey(field);
boolean exists = redissonService.lcContainsKey(key, field, localCachedMapOptions());
return ok(exists);
}
/**
* 获取 LocalCachedMap 大小。
*
* curl "http://localhost:8080/redisson/enhanced/local-cache/size?key=lc:user"
*
* @param key Redis 键
* @return 大小
*/
@GetMapping("/local-cache/size")
public Map<String, Object> lcSize(@RequestParam String key) {
checkKey(key);
int size = redissonService.lcSize(key, localCachedMapOptions());
return ok(size);
}
/**
* 清空 LocalCachedMap 远端和本地数据。
*
* curl -X DELETE "http://localhost:8080/redisson/enhanced/local-cache/clear?key=lc:user"
*
* @param key Redis 键
* @return 执行结果
*/
@DeleteMapping("/local-cache/clear")
public Map<String, Object> lcClear(@RequestParam String key) {
checkKey(key);
redissonService.lcClear(key, localCachedMapOptions());
log.info("LocalCachedMap 清空成功,key={}", key);
return ok(true);
}
/**
* 仅清空当前 JVM 的 LocalCachedMap 本地缓存。
*
* curl -X DELETE "http://localhost:8080/redisson/enhanced/local-cache/clear-local?key=lc:user"
*
* @param key Redis 键
* @return 执行结果
*/
@DeleteMapping("/local-cache/clear-local")
public Map<String, Object> lcClearLocalCache(@RequestParam String key) {
checkKey(key);
redissonService.lcClearLocalCache(key, localCachedMapOptions());
log.info("LocalCachedMap 本地缓存清空成功,key={}", key);
return ok(true);
}
// -------------------------------------------------------------------------
// JsonBucket / RedisJSON
// -------------------------------------------------------------------------
/**
* 设置完整 JSON 文档。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/json/set?key=json:user:1&ttlSeconds=300" \
* -H "Content-Type: application/json" \
* -d '{"id":1,"name":"Ateng","tags":["java","redis"]}'
*
* @param key Redis 键
* @param ttlSeconds 过期秒数,可选
* @param value JSON 对象
* @return 执行结果
*/
@PostMapping("/json/set")
public Map<String, Object> jsonSet(@RequestParam String key,
@RequestParam(required = false) Long ttlSeconds,
@RequestBody Object value) {
checkKey(key);
if (ObjectUtil.isNotNull(ttlSeconds) && ttlSeconds > 0) {
redissonService.jsonSet(key, value, Duration.ofSeconds(ttlSeconds), jsonCodec);
} else {
redissonService.jsonSet(key, value, jsonCodec);
}
log.info("JSON 文档写入成功,key={},ttlSeconds={}", key, ttlSeconds);
return ok(true);
}
/**
* 获取完整 JSON 文档。
*
* curl "http://localhost:8080/redisson/enhanced/json/get?key=json:user:1"
*
* @param key Redis 键
* @return JSON 对象
*/
@GetMapping("/json/get")
public Map<String, Object> jsonGet(@RequestParam String key) {
checkKey(key);
Object value = redissonService.jsonGet(key, jsonCodec);
return ok(value);
}
/**
* 根据 JSONPath 获取局部内容。
*
* curl "http://localhost:8080/redisson/enhanced/json/path-get?key=json:user:1&path=$.name"
*
* @param key Redis 键
* @param path JSONPath
* @return JSONPath 对应值
*/
@GetMapping("/json/path-get")
public Map<String, Object> jsonPathGet(@RequestParam String key,
@RequestParam String path) {
checkKey(key);
checkKey(path);
Object value = redissonService.jsonGet(key, path, jsonCodec);
return ok(value);
}
/**
* 根据 JSONPath 设置局部内容。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/json/path-set?key=json:user:1&path=$.name" \
* -H "Content-Type: application/json" \
* -d '"Ateng Updated"'
*
* @param key Redis 键
* @param path JSONPath
* @param value 值
* @return 执行结果
*/
@PostMapping("/json/path-set")
public Map<String, Object> jsonPathSet(@RequestParam String key,
@RequestParam String path,
@RequestBody Object value) {
checkKey(key);
checkKey(path);
redissonService.jsonSet(key, path, value, jsonCodec);
return ok(true);
}
/**
* JSONPath 不存在时设置局部内容。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/json/path-set-if-absent?key=json:user:1&path=$.email" \
* -H "Content-Type: application/json" \
* -d '"ateng@example.com"'
*
* @param key Redis 键
* @param path JSONPath
* @param value 值
* @return 是否设置成功
*/
@PostMapping("/json/path-set-if-absent")
public Map<String, Object> jsonSetIfAbsent(@RequestParam String key,
@RequestParam String path,
@RequestBody Object value) {
checkKey(key);
checkKey(path);
boolean success = redissonService.jsonSetIfAbsent(key, path, value, jsonCodec);
return ok(success);
}
/**
* JSONPath 存在时设置局部内容。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/json/path-set-if-exists?key=json:user:1&path=$.name" \
* -H "Content-Type: application/json" \
* -d '"Ateng Updated"'
*
* @param key Redis 键
* @param path JSONPath
* @param value 值
* @return 是否设置成功
*/
@PostMapping("/json/path-set-if-exists")
public Map<String, Object> jsonSetIfExists(@RequestParam String key,
@RequestParam String path,
@RequestBody Object value) {
checkKey(key);
checkKey(path);
boolean success = redissonService.jsonSetIfExists(key, path, value, jsonCodec);
return ok(success);
}
/**
* 删除 JSONPath 对应内容。
*
* curl -X DELETE "http://localhost:8080/redisson/enhanced/json/path-delete?key=json:user:1&path=$.email"
*
* @param key Redis 键
* @param path JSONPath
* @return 删除数量
*/
@DeleteMapping("/json/path-delete")
public Map<String, Object> jsonDelete(@RequestParam String key,
@RequestParam String path) {
checkKey(key);
checkKey(path);
long count = redissonService.jsonDelete(key, path, jsonCodec);
return ok(count);
}
/**
* 向 JSON 数组追加元素。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/json/array-append?key=json:user:1&path=$.tags" \
* -H "Content-Type: application/json" \
* -d '["springboot","redisson"]'
*
* @param key Redis 键
* @param path JSONPath
* @param values 追加元素集合
* @return 追加后数组长度
*/
@PostMapping("/json/array-append")
public Map<String, Object> jsonArrayAppend(@RequestParam String key,
@RequestParam String path,
@RequestBody List<Object> values) {
checkKey(key);
checkKey(path);
Assert.isTrue(CollUtil.isNotEmpty(values), "values不能为空");
long length = redissonService.jsonArrayAppend(key, path, jsonCodec, values.toArray());
return ok(length);
}
/**
* 获取 JSON 对象字段名。
*
* curl "http://localhost:8080/redisson/enhanced/json/keys?key=json:user:1"
*
* @param key Redis 键
* @return 字段名集合
*/
@GetMapping("/json/keys")
public Map<String, Object> jsonKeys(@RequestParam String key) {
checkKey(key);
List<String> keys = redissonService.jsonKeys(key, jsonCodec);
return ok(keys);
}
/**
* 清空 JSON 文档。
*
* curl -X DELETE "http://localhost:8080/redisson/enhanced/json/clear?key=json:user:1"
*
* @param key Redis 键
* @return 执行结果
*/
@DeleteMapping("/json/clear")
public Map<String, Object> jsonClear(@RequestParam String key) {
checkKey(key);
redissonService.jsonClear(key, jsonCodec);
return ok(true);
}
// -------------------------------------------------------------------------
// BinaryStream
// -------------------------------------------------------------------------
/**
* 写入文本到二进制流。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/binary/write-text?key=binary:test" \
* -H "Content-Type: text/plain" \
* -d 'hello redisson'
*
* @param key Redis 键
* @param text 文本内容
* @return 执行结果
*/
@PostMapping("/binary/write-text")
public Map<String, Object> binaryWriteText(@RequestParam String key,
@RequestBody String text) {
checkKey(key);
Assert.notNull(text, "text不能为空");
redissonService.binaryWrite(key, text.getBytes(StandardCharsets.UTF_8));
log.info("二进制文本写入成功,key={},length={}", key, text.length());
return ok(true);
}
/**
* 写入 Base64 二进制数据。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/binary/write-base64?key=binary:test" \
* -H "Content-Type: text/plain" \
* -d 'aGVsbG8='
*
* @param key Redis 键
* @param base64Text Base64 文本
* @return 执行结果
*/
@PostMapping("/binary/write-base64")
public Map<String, Object> binaryWriteBase64(@RequestParam String key,
@RequestBody String base64Text) {
checkKey(key);
Assert.isTrue(StrUtil.isNotBlank(base64Text), "base64Text不能为空");
redissonService.binaryWrite(key, Base64.decode(base64Text));
return ok(true);
}
/**
* 读取二进制数据并按 UTF-8 文本返回。
*
* curl "http://localhost:8080/redisson/enhanced/binary/read-text?key=binary:test"
*
* @param key Redis 键
* @return 文本内容
*/
@GetMapping("/binary/read-text")
public Map<String, Object> binaryReadText(@RequestParam String key) {
checkKey(key);
byte[] data = redissonService.binaryReadAll(key);
return ok(new String(data, StandardCharsets.UTF_8));
}
/**
* 读取二进制数据并按 Base64 返回。
*
* curl "http://localhost:8080/redisson/enhanced/binary/read-base64?key=binary:test"
*
* @param key Redis 键
* @return Base64 文本
*/
@GetMapping("/binary/read-base64")
public Map<String, Object> binaryReadBase64(@RequestParam String key) {
checkKey(key);
byte[] data = redissonService.binaryReadAll(key);
return ok(Base64.encode(data));
}
/**
* 获取二进制数据大小。
*
* curl "http://localhost:8080/redisson/enhanced/binary/size?key=binary:test"
*
* @param key Redis 键
* @return 字节大小
*/
@GetMapping("/binary/size")
public Map<String, Object> binarySize(@RequestParam String key) {
checkKey(key);
long size = redissonService.binarySize(key);
return ok(size);
}
/**
* 删除二进制数据。
*
* curl -X DELETE "http://localhost:8080/redisson/enhanced/binary/delete?key=binary:test"
*
* @param key Redis 键
* @return 是否删除成功
*/
@DeleteMapping("/binary/delete")
public Map<String, Object> binaryDelete(@RequestParam String key) {
checkKey(key);
boolean success = redissonService.binaryDelete(key);
return ok(success);
}
// -------------------------------------------------------------------------
// SetMultimap / ListMultimap
// -------------------------------------------------------------------------
/**
* 向 SetMultimap 添加值。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/set-multimap/put?key=smm:user-role&mapKey=user:1" \
* -H "Content-Type: application/json" \
* -d '"admin"'
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 是否新增
*/
@PostMapping("/set-multimap/put")
public Map<String, Object> smmPut(@RequestParam String key,
@RequestParam String mapKey,
@RequestBody Object mapValue) {
checkKey(key);
checkKey(mapKey);
boolean success = redissonService.smmPut(key, mapKey, mapValue);
return ok(success);
}
/**
* 获取 SetMultimap 字段值集合。
*
* curl "http://localhost:8080/redisson/enhanced/set-multimap/get?key=smm:user-role&mapKey=user:1"
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 值集合
*/
@GetMapping("/set-multimap/get")
public Map<String, Object> smmGet(@RequestParam String key,
@RequestParam String mapKey) {
checkKey(key);
checkKey(mapKey);
Set<Object> data = redissonService.smmGet(key, mapKey);
return ok(data);
}
/**
* 删除 SetMultimap 字段的指定值。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/set-multimap/remove?key=smm:user-role&mapKey=user:1" \
* -H "Content-Type: application/json" \
* -d '"admin"'
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 是否删除成功
*/
@PostMapping("/set-multimap/remove")
public Map<String, Object> smmRemove(@RequestParam String key,
@RequestParam String mapKey,
@RequestBody Object mapValue) {
checkKey(key);
checkKey(mapKey);
boolean success = redissonService.smmRemove(key, mapKey, mapValue);
return ok(success);
}
/**
* 删除 SetMultimap 字段的全部值。
*
* curl -X DELETE "http://localhost:8080/redisson/enhanced/set-multimap/remove-all?key=smm:user-role&mapKey=user:1"
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 删除的值集合
*/
@DeleteMapping("/set-multimap/remove-all")
public Map<String, Object> smmRemoveAll(@RequestParam String key,
@RequestParam String mapKey) {
checkKey(key);
checkKey(mapKey);
Set<Object> data = redissonService.smmRemoveAll(key, mapKey);
return ok(data);
}
/**
* 判断 SetMultimap 是否包含字段和值。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/set-multimap/contains-entry?key=smm:user-role&mapKey=user:1" \
* -H "Content-Type: application/json" \
* -d '"admin"'
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 是否包含
*/
@PostMapping("/set-multimap/contains-entry")
public Map<String, Object> smmContainsEntry(@RequestParam String key,
@RequestParam String mapKey,
@RequestBody Object mapValue) {
checkKey(key);
checkKey(mapKey);
boolean exists = redissonService.smmContainsEntry(key, mapKey, mapValue);
return ok(exists);
}
/**
* 获取 SetMultimap 总值数量。
*
* curl "http://localhost:8080/redisson/enhanced/set-multimap/size?key=smm:user-role"
*
* @param key Redis 键
* @return 总值数量
*/
@GetMapping("/set-multimap/size")
public Map<String, Object> smmSize(@RequestParam String key) {
checkKey(key);
int size = redissonService.smmSize(key);
return ok(size);
}
/**
* 清空 SetMultimap。
*
* curl -X DELETE "http://localhost:8080/redisson/enhanced/set-multimap/clear?key=smm:user-role"
*
* @param key Redis 键
* @return 执行结果
*/
@DeleteMapping("/set-multimap/clear")
public Map<String, Object> smmClear(@RequestParam String key) {
checkKey(key);
redissonService.smmClear(key);
return ok(true);
}
/**
* 向 ListMultimap 添加值。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/list-multimap/put?key=lmm:user-tag&mapKey=user:1" \
* -H "Content-Type: application/json" \
* -d '"java"'
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 是否新增
*/
@PostMapping("/list-multimap/put")
public Map<String, Object> lmmPut(@RequestParam String key,
@RequestParam String mapKey,
@RequestBody Object mapValue) {
checkKey(key);
checkKey(mapKey);
boolean success = redissonService.lmmPut(key, mapKey, mapValue);
return ok(success);
}
/**
* 获取 ListMultimap 字段值列表。
*
* curl "http://localhost:8080/redisson/enhanced/list-multimap/get?key=lmm:user-tag&mapKey=user:1"
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 值列表
*/
@GetMapping("/list-multimap/get")
public Map<String, Object> lmmGet(@RequestParam String key,
@RequestParam String mapKey) {
checkKey(key);
checkKey(mapKey);
List<Object> data = redissonService.lmmGet(key, mapKey);
return ok(data);
}
/**
* 删除 ListMultimap 字段的指定值。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/list-multimap/remove?key=lmm:user-tag&mapKey=user:1" \
* -H "Content-Type: application/json" \
* -d '"java"'
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 是否删除成功
*/
@PostMapping("/list-multimap/remove")
public Map<String, Object> lmmRemove(@RequestParam String key,
@RequestParam String mapKey,
@RequestBody Object mapValue) {
checkKey(key);
checkKey(mapKey);
boolean success = redissonService.lmmRemove(key, mapKey, mapValue);
return ok(success);
}
/**
* 删除 ListMultimap 字段的全部值。
*
* curl -X DELETE "http://localhost:8080/redisson/enhanced/list-multimap/remove-all?key=lmm:user-tag&mapKey=user:1"
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @return 删除的值列表
*/
@DeleteMapping("/list-multimap/remove-all")
public Map<String, Object> lmmRemoveAll(@RequestParam String key,
@RequestParam String mapKey) {
checkKey(key);
checkKey(mapKey);
List<Object> data = redissonService.lmmRemoveAll(key, mapKey);
return ok(data);
}
/**
* 判断 ListMultimap 是否包含字段和值。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/list-multimap/contains-entry?key=lmm:user-tag&mapKey=user:1" \
* -H "Content-Type: application/json" \
* -d '"java"'
*
* @param key Redis 键
* @param mapKey Multimap 字段
* @param mapValue Multimap 值
* @return 是否包含
*/
@PostMapping("/list-multimap/contains-entry")
public Map<String, Object> lmmContainsEntry(@RequestParam String key,
@RequestParam String mapKey,
@RequestBody Object mapValue) {
checkKey(key);
checkKey(mapKey);
boolean exists = redissonService.lmmContainsEntry(key, mapKey, mapValue);
return ok(exists);
}
/**
* 获取 ListMultimap 总值数量。
*
* curl "http://localhost:8080/redisson/enhanced/list-multimap/size?key=lmm:user-tag"
*
* @param key Redis 键
* @return 总值数量
*/
@GetMapping("/list-multimap/size")
public Map<String, Object> lmmSize(@RequestParam String key) {
checkKey(key);
int size = redissonService.lmmSize(key);
return ok(size);
}
/**
* 清空 ListMultimap。
*
* curl -X DELETE "http://localhost:8080/redisson/enhanced/list-multimap/clear?key=lmm:user-tag"
*
* @param key Redis 键
* @return 执行结果
*/
@DeleteMapping("/list-multimap/clear")
public Map<String, Object> lmmClear(@RequestParam String key) {
checkKey(key);
redissonService.lmmClear(key);
return ok(true);
}
// -------------------------------------------------------------------------
// SortedSet / LexSortedSet
// -------------------------------------------------------------------------
/**
* 添加自然排序集合元素。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/sorted-set/add?key=sorted:test" \
* -H "Content-Type: application/json" \
* -d '"B"'
*
* @param key Redis 键
* @param value 元素,建议实现 Comparable
* @return 是否新增
*/
@PostMapping("/sorted-set/add")
public Map<String, Object> sortedSetAdd(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
Assert.notNull(value, "value不能为空");
boolean success = redissonService.sortedSetAdd(key, value);
return ok(success);
}
/**
* 批量添加自然排序集合元素。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/sorted-set/add-all?key=sorted:test" \
* -H "Content-Type: application/json" \
* -d '["A","B","C"]'
*
* @param key Redis 键
* @param values 元素集合
* @return 是否有新增
*/
@PostMapping("/sorted-set/add-all")
public Map<String, Object> sortedSetAddAll(@RequestParam String key,
@RequestBody List<Object> values) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(values), "values不能为空");
boolean success = redissonService.sortedSetAddAll(key, values);
return ok(success);
}
/**
* 获取自然排序集合全部元素。
*
* curl "http://localhost:8080/redisson/enhanced/sorted-set/read-all?key=sorted:test"
*
* @param key Redis 键
* @return 元素集合
*/
@GetMapping("/sorted-set/read-all")
public Map<String, Object> sortedSetReadAll(@RequestParam String key) {
checkKey(key);
Collection<Object> data = redissonService.sortedSetReadAll(key);
return ok(data);
}
/**
* 删除自然排序集合元素。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/sorted-set/remove?key=sorted:test" \
* -H "Content-Type: application/json" \
* -d '["A","B"]'
*
* @param key Redis 键
* @param values 元素集合
* @return 是否删除成功
*/
@PostMapping("/sorted-set/remove")
public Map<String, Object> sortedSetRemove(@RequestParam String key,
@RequestBody List<Object> values) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(values), "values不能为空");
boolean success = redissonService.sortedSetRemove(key, values.toArray());
return ok(success);
}
/**
* 获取自然排序集合大小。
*
* curl "http://localhost:8080/redisson/enhanced/sorted-set/size?key=sorted:test"
*
* @param key Redis 键
* @return 大小
*/
@GetMapping("/sorted-set/size")
public Map<String, Object> sortedSetSize(@RequestParam String key) {
checkKey(key);
int size = redissonService.sortedSetSize(key);
return ok(size);
}
/**
* 清空自然排序集合。
*
* curl -X DELETE "http://localhost:8080/redisson/enhanced/sorted-set/clear?key=sorted:test"
*
* @param key Redis 键
* @return 执行结果
*/
@DeleteMapping("/sorted-set/clear")
public Map<String, Object> sortedSetClear(@RequestParam String key) {
checkKey(key);
redissonService.sortedSetClear(key);
return ok(true);
}
/**
* 添加字典序集合元素。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/lex/add?key=lex:test&value=apple"
*
* @param key Redis 键
* @param value 元素
* @return 是否新增
*/
@PostMapping("/lex/add")
public Map<String, Object> lexAdd(@RequestParam String key,
@RequestParam String value) {
checkKey(key);
checkKey(value);
boolean success = redissonService.lexAdd(key, value);
return ok(success);
}
/**
* 批量添加字典序集合元素。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/lex/add-all?key=lex:test" \
* -H "Content-Type: application/json" \
* -d '["apple","banana","cat"]'
*
* @param key Redis 键
* @param values 元素集合
* @return 是否有新增
*/
@PostMapping("/lex/add-all")
public Map<String, Object> lexAddAll(@RequestParam String key,
@RequestBody List<String> values) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(values), "values不能为空");
boolean success = redissonService.lexAddAll(key, values);
return ok(success);
}
/**
* 获取字典序集合全部元素。
*
* curl "http://localhost:8080/redisson/enhanced/lex/read-all?key=lex:test"
*
* @param key Redis 键
* @return 元素集合
*/
@GetMapping("/lex/read-all")
public Map<String, Object> lexReadAll(@RequestParam String key) {
checkKey(key);
Collection<String> data = redissonService.lexReadAll(key);
return ok(data);
}
/**
* 获取大于等于指定元素的字典序集合。
*
* curl "http://localhost:8080/redisson/enhanced/lex/range-tail?key=lex:test&from=banana"
*
* @param key Redis 键
* @param from 开始元素
* @return 元素集合
*/
@GetMapping("/lex/range-tail")
public Map<String, Object> lexRangeTail(@RequestParam String key,
@RequestParam String from) {
checkKey(key);
checkKey(from);
Collection<String> data = redissonService.lexRangeTail(key, from);
return ok(data);
}
/**
* 获取小于等于指定元素的字典序集合。
*
* curl "http://localhost:8080/redisson/enhanced/lex/range-head?key=lex:test&to=banana"
*
* @param key Redis 键
* @param to 结束元素
* @return 元素集合
*/
@GetMapping("/lex/range-head")
public Map<String, Object> lexRangeHead(@RequestParam String key,
@RequestParam String to) {
checkKey(key);
checkKey(to);
Collection<String> data = redissonService.lexRangeHead(key, to);
return ok(data);
}
/**
* 删除字典序集合元素。
*
* curl -X DELETE "http://localhost:8080/redisson/enhanced/lex/remove?key=lex:test&values=apple&values=banana"
*
* @param key Redis 键
* @param values 元素集合
* @return 是否删除成功
*/
@DeleteMapping("/lex/remove")
public Map<String, Object> lexRemove(@RequestParam String key,
@RequestParam List<String> values) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(values), "values不能为空");
boolean success = redissonService.lexRemove(key, values.toArray(new String[0]));
return ok(success);
}
/**
* 获取字典序集合大小。
*
* curl "http://localhost:8080/redisson/enhanced/lex/size?key=lex:test"
*
* @param key Redis 键
* @return 大小
*/
@GetMapping("/lex/size")
public Map<String, Object> lexSize(@RequestParam String key) {
checkKey(key);
int size = redissonService.lexSize(key);
return ok(size);
}
/**
* 清空字典序集合。
*
* curl -X DELETE "http://localhost:8080/redisson/enhanced/lex/clear?key=lex:test"
*
* @param key Redis 键
* @return 执行结果
*/
@DeleteMapping("/lex/clear")
public Map<String, Object> lexClear(@RequestParam String key) {
checkKey(key);
redissonService.lexClear(key);
return ok(true);
}
// -------------------------------------------------------------------------
// LongAdder / DoubleAdder
// -------------------------------------------------------------------------
/**
* LongAdder 增加。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/long-adder/add?key=adder:pv&delta=1"
*
* @param key Redis 键
* @param delta 增量
* @return 执行结果
*/
@PostMapping("/long-adder/add")
public Map<String, Object> longAdderAdd(@RequestParam String key,
@RequestParam(defaultValue = "1") Long delta) {
checkKey(key);
Assert.notNull(delta, "delta不能为空");
redissonService.longAdderAdd(key, delta);
return ok(true);
}
/**
* LongAdder 求和。
*
* curl "http://localhost:8080/redisson/enhanced/long-adder/sum?key=adder:pv"
*
* @param key Redis 键
* @return 当前总和
*/
@GetMapping("/long-adder/sum")
public Map<String, Object> longAdderSum(@RequestParam String key) {
checkKey(key);
long value = redissonService.longAdderSum(key);
return ok(value);
}
/**
* LongAdder 重置。
*
* curl -X PUT "http://localhost:8080/redisson/enhanced/long-adder/reset?key=adder:pv"
*
* @param key Redis 键
* @return 执行结果
*/
@PutMapping("/long-adder/reset")
public Map<String, Object> longAdderReset(@RequestParam String key) {
checkKey(key);
redissonService.longAdderReset(key);
return ok(true);
}
/**
* LongAdder 求和后重置。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/long-adder/sum-then-reset?key=adder:pv"
*
* @param key Redis 键
* @return 重置前总和
*/
@PostMapping("/long-adder/sum-then-reset")
public Map<String, Object> longAdderSumThenReset(@RequestParam String key) {
checkKey(key);
long value = redissonService.longAdderSumThenReset(key);
return ok(value);
}
/**
* DoubleAdder 增加。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/double-adder/add?key=adder:score&delta=1.5"
*
* @param key Redis 键
* @param delta 增量
* @return 执行结果
*/
@PostMapping("/double-adder/add")
public Map<String, Object> doubleAdderAdd(@RequestParam String key,
@RequestParam(defaultValue = "1") Double delta) {
checkKey(key);
Assert.notNull(delta, "delta不能为空");
redissonService.doubleAdderAdd(key, delta);
return ok(true);
}
/**
* DoubleAdder 求和。
*
* curl "http://localhost:8080/redisson/enhanced/double-adder/sum?key=adder:score"
*
* @param key Redis 键
* @return 当前总和
*/
@GetMapping("/double-adder/sum")
public Map<String, Object> doubleAdderSum(@RequestParam String key) {
checkKey(key);
double value = redissonService.doubleAdderSum(key);
return ok(value);
}
/**
* DoubleAdder 重置。
*
* curl -X PUT "http://localhost:8080/redisson/enhanced/double-adder/reset?key=adder:score"
*
* @param key Redis 键
* @return 执行结果
*/
@PutMapping("/double-adder/reset")
public Map<String, Object> doubleAdderReset(@RequestParam String key) {
checkKey(key);
redissonService.doubleAdderReset(key);
return ok(true);
}
/**
* DoubleAdder 求和后重置。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/double-adder/sum-then-reset?key=adder:score"
*
* @param key Redis 键
* @return 重置前总和
*/
@PostMapping("/double-adder/sum-then-reset")
public Map<String, Object> doubleAdderSumThenReset(@RequestParam String key) {
checkKey(key);
double value = redissonService.doubleAdderSumThenReset(key);
return ok(value);
}
// -------------------------------------------------------------------------
// Bounded / Priority Queue
// -------------------------------------------------------------------------
/**
* 初始化有界阻塞队列容量。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/bounded-queue/init?key=bq:test&capacity=10"
*
* @param key Redis 键
* @param capacity 容量
* @return 是否初始化成功
*/
@PostMapping("/bounded-queue/init")
public Map<String, Object> boundedQueueTrySetCapacity(@RequestParam String key,
@RequestParam Integer capacity) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(capacity) && capacity > 0, "capacity必须大于0");
boolean success = redissonService.boundedQueueTrySetCapacity(key, capacity);
return ok(success);
}
/**
* 有界阻塞队列入队。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/bounded-queue/offer?key=bq:test" \
* -H "Content-Type: application/json" \
* -d '"A"'
*
* @param key Redis 键
* @param value 元素
* @return 是否入队成功
*/
@PostMapping("/bounded-queue/offer")
public Map<String, Object> boundedQueueOffer(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
boolean success = redissonService.boundedQueueOffer(key, value);
return ok(success);
}
/**
* 有界阻塞队列超时入队。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/bounded-queue/offer-timeout?key=bq:test&timeoutSeconds=3" \
* -H "Content-Type: application/json" \
* -d '"A"'
*
* @param key Redis 键
* @param timeoutSeconds 等待秒数
* @param value 元素
* @return 是否入队成功
* @throws InterruptedException 线程中断时抛出
*/
@PostMapping("/bounded-queue/offer-timeout")
public Map<String, Object> boundedQueueOfferTimeout(@RequestParam String key,
@RequestParam(defaultValue = "3") Long timeoutSeconds,
@RequestBody Object value) throws InterruptedException {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(timeoutSeconds) && timeoutSeconds >= 0, "timeoutSeconds不能小于0");
boolean success = redissonService.boundedQueueOffer(key, value, timeoutSeconds, TimeUnit.SECONDS);
return ok(success);
}
/**
* 有界阻塞队列出队。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/bounded-queue/poll?key=bq:test"
*
* @param key Redis 键
* @return 元素
*/
@PostMapping("/bounded-queue/poll")
public Map<String, Object> boundedQueuePoll(@RequestParam String key) {
checkKey(key);
Object value = redissonService.boundedQueuePoll(key);
return ok(value);
}
/**
* 有界阻塞队列超时出队。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/bounded-queue/poll-timeout?key=bq:test&timeoutSeconds=3"
*
* @param key Redis 键
* @param timeoutSeconds 等待秒数
* @return 元素
* @throws InterruptedException 线程中断时抛出
*/
@PostMapping("/bounded-queue/poll-timeout")
public Map<String, Object> boundedQueuePollTimeout(@RequestParam String key,
@RequestParam(defaultValue = "3") Long timeoutSeconds) throws InterruptedException {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(timeoutSeconds) && timeoutSeconds >= 0, "timeoutSeconds不能小于0");
Object value = redissonService.boundedQueuePoll(key, timeoutSeconds, TimeUnit.SECONDS);
return ok(value);
}
/**
* 获取有界阻塞队列信息。
*
* curl "http://localhost:8080/redisson/enhanced/bounded-queue/info?key=bq:test"
*
* @param key Redis 键
* @return 队列信息
*/
@GetMapping("/bounded-queue/info")
public Map<String, Object> boundedQueueInfo(@RequestParam String key) {
checkKey(key);
Map<String, Object> data = new LinkedHashMap<>();
data.put("size", redissonService.boundedQueueSize(key));
data.put("remainingCapacity", redissonService.boundedQueueRemainingCapacity(key));
return ok(data);
}
/**
* 优先级队列入队。
* 注意:元素建议使用 String、Integer 等可比较类型,或自定义实现 Comparable。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/priority-queue/offer?key=pq:test" \
* -H "Content-Type: application/json" \
* -d '"A"'
*
* @param key Redis 键
* @param value 元素
* @return 是否入队成功
*/
@PostMapping("/priority-queue/offer")
public Map<String, Object> priorityQueueOffer(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
boolean success = redissonService.priorityQueueOffer(key, value);
return ok(success);
}
/**
* 优先级队列出队。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/priority-queue/poll?key=pq:test"
*
* @param key Redis 键
* @return 元素
*/
@PostMapping("/priority-queue/poll")
public Map<String, Object> priorityQueuePoll(@RequestParam String key) {
checkKey(key);
Object value = redissonService.priorityQueuePoll(key);
return ok(value);
}
/**
* 优先级双端队列头部入队。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/priority-deque/offer-first?key=pdq:test" \
* -H "Content-Type: application/json" \
* -d '"A"'
*
* @param key Redis 键
* @param value 元素
* @return 是否入队成功
*/
@PostMapping("/priority-deque/offer-first")
public Map<String, Object> priorityDequeOfferFirst(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
boolean success = redissonService.priorityDequeOfferFirst(key, value);
return ok(success);
}
/**
* 优先级双端队列尾部入队。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/priority-deque/offer-last?key=pdq:test" \
* -H "Content-Type: application/json" \
* -d '"B"'
*
* @param key Redis 键
* @param value 元素
* @return 是否入队成功
*/
@PostMapping("/priority-deque/offer-last")
public Map<String, Object> priorityDequeOfferLast(@RequestParam String key,
@RequestBody Object value) {
checkKey(key);
boolean success = redissonService.priorityDequeOfferLast(key, value);
return ok(success);
}
/**
* 优先级双端队列头部出队。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/priority-deque/poll-first?key=pdq:test"
*
* @param key Redis 键
* @return 元素
*/
@PostMapping("/priority-deque/poll-first")
public Map<String, Object> priorityDequePollFirst(@RequestParam String key) {
checkKey(key);
Object value = redissonService.priorityDequePollFirst(key);
return ok(value);
}
/**
* 优先级双端队列尾部出队。
*
* curl -X POST "http://localhost:8080/redisson/enhanced/priority-deque/poll-last?key=pdq:test"
*
* @param key Redis 键
* @return 元素
*/
@PostMapping("/priority-deque/poll-last")
public Map<String, Object> priorityDequePollLast(@RequestParam String key) {
checkKey(key);
Object value = redissonService.priorityDequePollLast(key);
return ok(value);
}
/**
* 构建 LocalCachedMap 默认配置。
*
* @return LocalCachedMapOptions
*/
private LocalCachedMapOptions<Object, Object> localCachedMapOptions() {
return LocalCachedMapOptions.<Object, Object>defaults();
}
/**
* 校验 Redis Key。
*
* @param key Redis 键
*/
private void checkKey(String key) {
Assert.isTrue(StrUtil.isNotBlank(key), "Redis Key不能为空");
}
/**
* 成功响应。
*
* @param data 响应数据
* @return 响应 Map
*/
private Map<String, Object> ok(Object data) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("code", 0);
result.put("message", "操作成功");
result.put("data", data);
return result;
}
/**
* JSONPath 设置请求体。
*
* @author Ateng
* @since 2026-04-27
*/
@Data
public static class JsonPathSetRequest {
/**
* JSONPath。
*/
private String path;
/**
* 值。
*/
private Object value;
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
高级消息与服务测试控制器
用于演示 RedissonService 的可靠队列、Stream 高级治理、分布式执行器、远程服务、LiveObject 和对象监听能力。
package local.ateng.java.redis.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import local.ateng.java.redis.service.RedissonService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.*;
import org.redisson.api.queue.*;
import org.redisson.api.stream.StreamTrimArgs;
import org.springframework.web.bind.annotation.*;
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Redisson 高级消息与服务测试控制器
* 用于演示 RedissonService 的可靠队列、Stream 高级治理、分布式执行器、远程服务、LiveObject 和对象监听能力。
*
* @author Ateng
* @since 2026-04-27
*/
@Slf4j
@RestController
@RequestMapping("/redisson/advanced-message")
@RequiredArgsConstructor
public class RedissonAdvancedMessageController {
private final RedissonService redissonService;
// -------------------------------------------------------------------------
// ReliableQueue / 可靠队列
// -------------------------------------------------------------------------
/**
* 初始化可靠队列配置。
* <p>
* curl -X POST "http://localhost:8080/redisson/advanced-message/reliable-queue/config/init?key=rq:test&deliveryLimit=5&visibilitySeconds=30&ttlSeconds=3600&delaySeconds=0&maxSize=1000"
*
* @param key 队列 key
* @param deliveryLimit 最大投递次数
* @param visibilitySeconds 消息可见性超时秒数
* @param ttlSeconds 消息 TTL 秒数,0 表示不限制
* @param delaySeconds 默认延迟秒数,0 表示不延迟
* @param maxSize 最大队列大小,0 表示不限制
* @return 是否初始化成功
*/
@PostMapping("/reliable-queue/config/init")
public Map<String, Object> reliableQueueConfigInit(@RequestParam String key,
@RequestParam(defaultValue = "10") Integer deliveryLimit,
@RequestParam(defaultValue = "30") Long visibilitySeconds,
@RequestParam(defaultValue = "0") Long ttlSeconds,
@RequestParam(defaultValue = "0") Long delaySeconds,
@RequestParam(defaultValue = "0") Integer maxSize) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(deliveryLimit) && deliveryLimit > 0, "deliveryLimit必须大于0");
Assert.isTrue(ObjectUtil.isNotNull(visibilitySeconds) && visibilitySeconds >= 0, "visibilitySeconds不能小于0");
Assert.isTrue(ObjectUtil.isNotNull(ttlSeconds) && ttlSeconds >= 0, "ttlSeconds不能小于0");
Assert.isTrue(ObjectUtil.isNotNull(delaySeconds) && delaySeconds >= 0, "delaySeconds不能小于0");
Assert.isTrue(ObjectUtil.isNotNull(maxSize) && maxSize >= 0, "maxSize不能小于0");
QueueConfig config = QueueConfig.defaults()
.deliveryLimit(deliveryLimit)
.visibility(Duration.ofSeconds(visibilitySeconds))
.timeToLive(Duration.ofSeconds(ttlSeconds))
.delay(Duration.ofSeconds(delaySeconds))
.maxSize(maxSize);
boolean success = redissonService.reliableQueueSetConfigIfAbsent(key, config);
return ok(success);
}
/**
* 覆盖可靠队列配置。
* <p>
* curl -X PUT "http://localhost:8080/redisson/advanced-message/reliable-queue/config?key=rq:test&deliveryLimit=5&visibilitySeconds=30&ttlSeconds=3600&delaySeconds=0&maxSize=1000"
*
* @param key 队列 key
* @param deliveryLimit 最大投递次数
* @param visibilitySeconds 消息可见性超时秒数
* @param ttlSeconds 消息 TTL 秒数,0 表示不限制
* @param delaySeconds 默认延迟秒数,0 表示不延迟
* @param maxSize 最大队列大小,0 表示不限制
* @return 执行结果
*/
@PutMapping("/reliable-queue/config")
public Map<String, Object> reliableQueueConfigSet(@RequestParam String key,
@RequestParam(defaultValue = "10") Integer deliveryLimit,
@RequestParam(defaultValue = "30") Long visibilitySeconds,
@RequestParam(defaultValue = "0") Long ttlSeconds,
@RequestParam(defaultValue = "0") Long delaySeconds,
@RequestParam(defaultValue = "0") Integer maxSize) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(deliveryLimit) && deliveryLimit > 0, "deliveryLimit必须大于0");
QueueConfig config = QueueConfig.defaults()
.deliveryLimit(deliveryLimit)
.visibility(Duration.ofSeconds(visibilitySeconds))
.timeToLive(Duration.ofSeconds(ttlSeconds))
.delay(Duration.ofSeconds(delaySeconds))
.maxSize(maxSize);
redissonService.reliableQueueSetConfig(key, config);
return ok(true);
}
/**
* 添加可靠队列消息。
* <p>
* curl -X POST "http://localhost:8080/redisson/advanced-message/reliable-queue/add?key=rq:test&delaySeconds=0&ttlSeconds=3600&deliveryLimit=5&priority=0" \
* -H "Content-Type: application/json" \
* -d '{"id":1,"name":"Ateng"}'
*
* @param key 队列 key
* @param delaySeconds 消息延迟秒数
* @param ttlSeconds 消息 TTL 秒数,0 表示不限制
* @param deliveryLimit 最大投递次数
* @param priority 优先级
* @param deduplicationId 去重 ID,可选
* @param payload 消息体
* @return 消息信息
*/
@PostMapping("/reliable-queue/add")
public Map<String, Object> reliableQueueAdd(@RequestParam String key,
@RequestParam(defaultValue = "0") Long delaySeconds,
@RequestParam(defaultValue = "0") Long ttlSeconds,
@RequestParam(defaultValue = "10") Integer deliveryLimit,
@RequestParam(defaultValue = "0") Integer priority,
@RequestParam(required = false) String deduplicationId,
@RequestBody Object payload) {
checkKey(key);
Assert.notNull(payload, "payload不能为空");
MessageArgs<Object> messageArgs = MessageArgs.payload(payload)
.deliveryLimit(deliveryLimit)
.priority(priority);
if (ObjectUtil.isNotNull(delaySeconds) && delaySeconds > 0) {
messageArgs.delay(Duration.ofSeconds(delaySeconds));
}
if (ObjectUtil.isNotNull(ttlSeconds) && ttlSeconds > 0) {
messageArgs.timeToLive(Duration.ofSeconds(ttlSeconds));
}
if (StrUtil.isNotBlank(deduplicationId)) {
messageArgs.deduplicationById(deduplicationId, Duration.ofHours(1));
}
Message<Object> message = redissonService.reliableQueueAdd(key, QueueAddArgs.messages(messageArgs));
return ok(messageToMap(message));
}
/**
* 批量添加可靠队列消息。
* <p>
* curl -X POST "http://localhost:8080/redisson/advanced-message/reliable-queue/add-many?key=rq:test" \
* -H "Content-Type: application/json" \
* -d '["A","B","C"]'
*
* @param key 队列 key
* @param payloads 消息体集合
* @return 消息信息集合
*/
@PostMapping("/reliable-queue/add-many")
public Map<String, Object> reliableQueueAddMany(@RequestParam String key,
@RequestBody List<Object> payloads) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(payloads), "payloads不能为空");
@SuppressWarnings("unchecked")
MessageArgs<Object>[] args = payloads.stream()
.filter(ObjectUtil::isNotNull)
.map(MessageArgs::payload)
.toArray(MessageArgs[]::new);
if (ArrayUtil.isEmpty(args)) {
return ok(List.of());
}
List<Message<Object>> messages = redissonService.reliableQueueAddMany(key, QueueAddArgs.messages(args));
return ok(messages.stream().map(this::messageToMap).toList());
}
/**
* 拉取一条可靠队列消息。
* <p>
* curl -X POST "http://localhost:8080/redisson/advanced-message/reliable-queue/poll?key=rq:test&ackMode=MANUAL&visibilitySeconds=30&timeoutSeconds=3"
*
* @param key 队列 key
* @param ackMode 确认模式,MANUAL 或 AUTO
* @param visibilitySeconds 可见性超时秒数
* @param timeoutSeconds 长轮询等待秒数
* @return 消息信息
*/
@PostMapping("/reliable-queue/poll")
public Map<String, Object> reliableQueuePoll(@RequestParam String key,
@RequestParam(defaultValue = "MANUAL") AcknowledgeMode ackMode,
@RequestParam(defaultValue = "30") Long visibilitySeconds,
@RequestParam(defaultValue = "0") Long timeoutSeconds) {
checkKey(key);
Assert.notNull(ackMode, "ackMode不能为空");
QueuePollArgs args = QueuePollArgs.defaults()
.acknowledgeMode(ackMode)
.visibility(Duration.ofSeconds(visibilitySeconds));
if (ObjectUtil.isNotNull(timeoutSeconds) && timeoutSeconds > 0) {
args.timeout(Duration.ofSeconds(timeoutSeconds));
}
Message<Object> message = redissonService.reliableQueuePoll(key, args);
return ok(messageToMap(message));
}
/**
* 批量拉取可靠队列消息。
* <p>
* curl -X POST "http://localhost:8080/redisson/advanced-message/reliable-queue/poll-many?key=rq:test&count=10&ackMode=MANUAL&visibilitySeconds=30&timeoutSeconds=3"
*
* @param key 队列 key
* @param count 拉取数量
* @param ackMode 确认模式
* @param visibilitySeconds 可见性超时秒数
* @param timeoutSeconds 长轮询等待秒数
* @return 消息信息集合
*/
@PostMapping("/reliable-queue/poll-many")
public Map<String, Object> reliableQueuePollMany(@RequestParam String key,
@RequestParam(defaultValue = "10") Integer count,
@RequestParam(defaultValue = "MANUAL") AcknowledgeMode ackMode,
@RequestParam(defaultValue = "30") Long visibilitySeconds,
@RequestParam(defaultValue = "0") Long timeoutSeconds) {
checkKey(key);
Assert.isTrue(ObjectUtil.isNotNull(count) && count > 0, "count必须大于0");
QueuePollArgs args = QueuePollArgs.defaults()
.acknowledgeMode(ackMode)
.visibility(Duration.ofSeconds(visibilitySeconds))
.count(count);
if (ObjectUtil.isNotNull(timeoutSeconds) && timeoutSeconds > 0) {
args.timeout(Duration.ofSeconds(timeoutSeconds));
}
List<Message<Object>> messages = redissonService.reliableQueuePollMany(key, args);
return ok(messages.stream().map(this::messageToMap).toList());
}
/**
* 确认可靠队列消息。
* <p>
* curl -X POST "http://localhost:8080/redisson/advanced-message/reliable-queue/ack?key=rq:test&ids=xxx&ids=yyy"
*
* @param key 队列 key
* @param ids 消息 ID 集合
* @return 执行结果
*/
@PostMapping("/reliable-queue/ack")
public Map<String, Object> reliableQueueAck(@RequestParam String key,
@RequestParam List<String> ids) {
checkKey(key);
Assert.isTrue(CollUtil.isNotEmpty(ids), "ids不能为空");
redissonService.reliableQueueAck(key, QueueAckArgs.ids(ids.toArray(new String[0])));
return ok(true);
}
/**
* 拒绝可靠队列消息。
* <p>
* curl -X POST "http://localhost:8080/redisson/advanced-message/reliable-queue/nack/rejected?key=rq:test&id=xxx"
*
* @param key 队列 key
* @param id 消息 ID
* @return 执行结果
*/
@PostMapping("/reliable-queue/nack/rejected")
public Map<String, Object> reliableQueueNackRejected(@RequestParam String key,
@RequestParam String id) {
checkKey(key);
checkKey(id);
redissonService.reliableQueueNack(key, QueueNegativeAckArgs.rejected(id));
return ok(true);
}
/**
* 根据 ID 获取可靠队列消息。
* <p>
* curl "http://localhost:8080/redisson/advanced-message/reliable-queue/get?key=rq:test&id=xxx"
*
* @param key 队列 key
* @param id 消息 ID
* @return 消息信息
*/
@GetMapping("/reliable-queue/get")
public Map<String, Object> reliableQueueGet(@RequestParam String key,
@RequestParam String id) {
checkKey(key);
checkKey(id);
Message<Object> message = redissonService.reliableQueueGet(key, id);
return ok(messageToMap(message));
}
/**
* 获取可靠队列所有可拉取消息。
* <p>
* curl "http://localhost:8080/redisson/advanced-message/reliable-queue/list-all?key=rq:test"
*
* @param key 队列 key
* @return 消息集合
*/
@GetMapping("/reliable-queue/list-all")
public Map<String, Object> reliableQueueListAll(@RequestParam String key) {
checkKey(key);
List<Message<Object>> messages = redissonService.reliableQueueListAll(key);
return ok(messages.stream().map(this::messageToMap).toList());
}
/**
* 获取可靠队列统计信息。
* <p>
* curl "http://localhost:8080/redisson/advanced-message/reliable-queue/info?key=rq:test"
*
* @param key 队列 key
* @return 统计信息
*/
@GetMapping("/reliable-queue/info")
public Map<String, Object> reliableQueueInfo(@RequestParam String key) {
checkKey(key);
Map<String, Object> data = new LinkedHashMap<>();
data.put("size", redissonService.reliableQueueSize(key));
data.put("delayedSize", redissonService.reliableQueueDelayedSize(key));
data.put("unacknowledgedSize", redissonService.reliableQueueUnacknowledgedSize(key));
data.put("deadLetterSources", redissonService.reliableQueueDeadLetterSources(key));
return ok(data);
}
/**
* 清空可靠队列。
* <p>
* curl -X DELETE "http://localhost:8080/redisson/advanced-message/reliable-queue/clear?key=rq:test"
*
* @param key 队列 key
* @return 是否清空成功
*/
@DeleteMapping("/reliable-queue/clear")
public Map<String, Object> reliableQueueClear(@RequestParam String key) {
checkKey(key);
boolean success = redissonService.reliableQueueClear(key);
return ok(success);
}
/**
* 禁用可靠队列操作。
* <p>
* curl -X PUT "http://localhost:8080/redisson/advanced-message/reliable-queue/operation/disable?key=rq:test&operation=ADD"
*
* @param key 队列 key
* @param operation 队列操作
* @return 执行结果
*/
@PutMapping("/reliable-queue/operation/disable")
public Map<String, Object> reliableQueueDisableOperation(@RequestParam String key,
@RequestParam QueueOperation operation) {
checkKey(key);
Assert.notNull(operation, "operation不能为空");
redissonService.reliableQueueDisableOperation(key, operation);
return ok(true);
}
/**
* 启用可靠队列操作。
* <p>
* curl -X PUT "http://localhost:8080/redisson/advanced-message/reliable-queue/operation/enable?key=rq:test&operation=ADD"
*
* @param key 队列 key
* @param operation 队列操作
* @return 执行结果
*/
@PutMapping("/reliable-queue/operation/enable")
public Map<String, Object> reliableQueueEnableOperation(@RequestParam String key,
@RequestParam QueueOperation operation) {
checkKey(key);
Assert.notNull(operation, "operation不能为空");
redissonService.reliableQueueEnableOperation(key, operation);
return ok(true);
}
// -------------------------------------------------------------------------
// Stream / 高级消费治理
// -------------------------------------------------------------------------
/**
* 获取 Stream 详细信息。
* <p>
* curl "http://localhost:8080/redisson/advanced-message/stream/info?streamKey=stream:test"
*
* @param streamKey Stream key
* @return Stream 信息
*/
@GetMapping("/stream/info")
public Map<String, Object> streamInfo(@RequestParam String streamKey) {
checkKey(streamKey);
StreamInfo<Object, Object> info = redissonService.streamInfo(streamKey);
return ok(info);
}
/**
* 获取 Stream 消费组列表。
* <p>
* curl "http://localhost:8080/redisson/advanced-message/stream/groups?streamKey=stream:test"
*
* @param streamKey Stream key
* @return 消费组列表
*/
@GetMapping("/stream/groups")
public Map<String, Object> streamListGroups(@RequestParam String streamKey) {
checkKey(streamKey);
List<StreamGroup> groups = redissonService.streamListGroups(streamKey);
return ok(groups);
}
/**
* 获取 Stream 消费者列表。
* <p>
* curl "http://localhost:8080/redisson/advanced-message/stream/consumers?streamKey=stream:test&groupName=group:test"
*
* @param streamKey Stream key
* @param groupName 消费组
* @return 消费者列表
*/
@GetMapping("/stream/consumers")
public Map<String, Object> streamListConsumers(@RequestParam String streamKey,
@RequestParam String groupName) {
checkKey(streamKey);
checkKey(groupName);
List<StreamConsumer> consumers = redissonService.streamListConsumers(streamKey, groupName);
return ok(consumers);
}
/**
* 创建 Stream 消费者。
* <p>
* curl -X POST "http://localhost:8080/redisson/advanced-message/stream/consumer/create?streamKey=stream:test&groupName=group:test&consumerName=consumer:1"
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 消费者
* @return 执行结果
*/
@PostMapping("/stream/consumer/create")
public Map<String, Object> streamCreateConsumer(@RequestParam String streamKey,
@RequestParam String groupName,
@RequestParam String consumerName) {
checkKey(streamKey);
checkKey(groupName);
checkKey(consumerName);
redissonService.streamCreateConsumer(streamKey, groupName, consumerName);
return ok(true);
}
/**
* 删除 Stream 消费者。
* <p>
* curl -X DELETE "http://localhost:8080/redisson/advanced-message/stream/consumer/remove?streamKey=stream:test&groupName=group:test&consumerName=consumer:1"
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 消费者
* @return 待处理消息数量
*/
@DeleteMapping("/stream/consumer/remove")
public Map<String, Object> streamRemoveConsumer(@RequestParam String streamKey,
@RequestParam String groupName,
@RequestParam String consumerName) {
checkKey(streamKey);
checkKey(groupName);
checkKey(consumerName);
long pendingCount = redissonService.streamRemoveConsumer(streamKey, groupName, consumerName);
return ok(pendingCount);
}
/**
* 删除 Stream 消费组。
* <p>
* curl -X DELETE "http://localhost:8080/redisson/advanced-message/stream/group/remove?streamKey=stream:test&groupName=group:test"
*
* @param streamKey Stream key
* @param groupName 消费组
* @return 执行结果
*/
@DeleteMapping("/stream/group/remove")
public Map<String, Object> streamRemoveGroup(@RequestParam String streamKey,
@RequestParam String groupName) {
checkKey(streamKey);
checkKey(groupName);
redissonService.streamRemoveGroup(streamKey, groupName);
return ok(true);
}
/**
* 更新 Stream 消费组读取起始 ID。
* <p>
* curl -X PUT "http://localhost:8080/redisson/advanced-message/stream/group/message-id?streamKey=stream:test&groupName=group:test&id=0-0"
*
* @param streamKey Stream key
* @param groupName 消费组
* @param id 消息 ID
* @return 执行结果
*/
@PutMapping("/stream/group/message-id")
public Map<String, Object> streamUpdateGroupMessageId(@RequestParam String streamKey,
@RequestParam String groupName,
@RequestParam String id) {
checkKey(streamKey);
checkKey(groupName);
redissonService.streamUpdateGroupMessageId(streamKey, groupName, parseStreamMessageId(id));
return ok(true);
}
/**
* 获取 Stream 待处理消息概要。
* <p>
* curl "http://localhost:8080/redisson/advanced-message/stream/pending/info?streamKey=stream:test&groupName=group:test"
*
* @param streamKey Stream key
* @param groupName 消费组
* @return 待处理概要
*/
@GetMapping("/stream/pending/info")
public Map<String, Object> streamPendingInfo(@RequestParam String streamKey,
@RequestParam String groupName) {
checkKey(streamKey);
checkKey(groupName);
PendingResult result = redissonService.streamPendingInfo(streamKey, groupName);
return ok(result);
}
/**
* 获取 Stream 待处理消息列表。
* <p>
* curl "http://localhost:8080/redisson/advanced-message/stream/pending/list?streamKey=stream:test&groupName=group:test&startId=0-0&endId=9999999999999-0&count=10"
*
* @param streamKey Stream key
* @param groupName 消费组
* @param startId 开始 ID
* @param endId 结束 ID
* @param count 数量
* @return 待处理消息列表
*/
@GetMapping("/stream/pending/list")
public Map<String, Object> streamListPending(@RequestParam String streamKey,
@RequestParam String groupName,
@RequestParam(defaultValue = "0-0") String startId,
@RequestParam(defaultValue = "9999999999999-0") String endId,
@RequestParam(defaultValue = "10") Integer count) {
checkKey(streamKey);
checkKey(groupName);
List<PendingEntry> result = redissonService.streamListPending(
streamKey,
groupName,
parseStreamMessageId(startId),
parseStreamMessageId(endId),
count
);
return ok(result);
}
/**
* 按范围读取 Stream 消息。
* <p>
* curl "http://localhost:8080/redisson/advanced-message/stream/range?streamKey=stream:test&startId=0-0&endId=9999999999999-0&count=10"
*
* @param streamKey Stream key
* @param startId 开始 ID
* @param endId 结束 ID
* @param count 数量
* @return 消息 Map
*/
@GetMapping("/stream/range")
public Map<String, Object> streamRange(@RequestParam String streamKey,
@RequestParam(defaultValue = "0-0") String startId,
@RequestParam(defaultValue = "9999999999999-0") String endId,
@RequestParam(required = false) Integer count) {
checkKey(streamKey);
Map<StreamMessageId, Map<Object, Object>> data;
if (ObjectUtil.isNotNull(count) && count > 0) {
data = redissonService.streamRange(streamKey, parseStreamMessageId(startId), parseStreamMessageId(endId), count);
} else {
data = redissonService.streamRange(streamKey, parseStreamMessageId(startId), parseStreamMessageId(endId));
}
return ok(streamMapToResponse(data));
}
/**
* 按范围倒序读取 Stream 消息。
* <p>
* curl "http://localhost:8080/redisson/advanced-message/stream/range-reversed?streamKey=stream:test&startId=9999999999999-0&endId=0-0&count=10"
*
* @param streamKey Stream key
* @param startId 开始 ID
* @param endId 结束 ID
* @param count 数量
* @return 消息 Map
*/
@GetMapping("/stream/range-reversed")
public Map<String, Object> streamRangeReversed(@RequestParam String streamKey,
@RequestParam(defaultValue = "9999999999999-0") String startId,
@RequestParam(defaultValue = "0-0") String endId,
@RequestParam(required = false) Integer count) {
checkKey(streamKey);
Map<StreamMessageId, Map<Object, Object>> data;
if (ObjectUtil.isNotNull(count) && count > 0) {
data = redissonService.streamRangeReversed(streamKey, parseStreamMessageId(startId), parseStreamMessageId(endId), count);
} else {
data = redissonService.streamRangeReversed(streamKey, parseStreamMessageId(startId), parseStreamMessageId(endId));
}
return ok(streamMapToResponse(data));
}
/**
* 转移待处理 Stream 消息所有权。
* <p>
* curl -X POST "http://localhost:8080/redisson/advanced-message/stream/claim?streamKey=stream:test&groupName=group:test&consumerName=consumer:2&idleSeconds=60&ids=1714096800000-0"
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 新消费者
* @param idleSeconds 最小空闲秒数
* @param ids 消息 ID 集合
* @return 转移后的消息
*/
@PostMapping("/stream/claim")
public Map<String, Object> streamClaim(@RequestParam String streamKey,
@RequestParam String groupName,
@RequestParam String consumerName,
@RequestParam(defaultValue = "60") Long idleSeconds,
@RequestParam List<String> ids) {
checkKey(streamKey);
checkKey(groupName);
checkKey(consumerName);
Assert.isTrue(CollUtil.isNotEmpty(ids), "ids不能为空");
StreamMessageId[] messageIds = ids.stream()
.map(this::parseStreamMessageId)
.toArray(StreamMessageId[]::new);
Map<StreamMessageId, Map<Object, Object>> data = redissonService.streamClaim(
streamKey,
groupName,
consumerName,
idleSeconds,
TimeUnit.SECONDS,
messageIds
);
return ok(streamMapToResponse(data));
}
/**
* 自动转移待处理 Stream 消息所有权。
* <p>
* curl -X POST "http://localhost:8080/redisson/advanced-message/stream/auto-claim?streamKey=stream:test&groupName=group:test&consumerName=consumer:2&idleSeconds=60&startId=0-0&count=10"
*
* @param streamKey Stream key
* @param groupName 消费组
* @param consumerName 新消费者
* @param idleSeconds 最小空闲秒数
* @param startId 起始 ID
* @param count 数量
* @return 自动转移结果
*/
@PostMapping("/stream/auto-claim")
public Map<String, Object> streamAutoClaim(@RequestParam String streamKey,
@RequestParam String groupName,
@RequestParam String consumerName,
@RequestParam(defaultValue = "60") Long idleSeconds,
@RequestParam(defaultValue = "0-0") String startId,
@RequestParam(defaultValue = "10") Integer count) {
checkKey(streamKey);
checkKey(groupName);
checkKey(consumerName);
AutoClaimResult<Object, Object> result = redissonService.streamAutoClaim(
streamKey,
groupName,
consumerName,
idleSeconds,
TimeUnit.SECONDS,
parseStreamMessageId(startId),
count
);
return ok(result);
}
/**
* 按最大长度裁剪 Stream。
* <p>
* curl -X DELETE "http://localhost:8080/redisson/advanced-message/stream/trim/max-size?streamKey=stream:test&maxSize=1000"
*
* @param streamKey Stream key
* @param maxSize 最大长度
* @return 裁剪数量
*/
@DeleteMapping("/stream/trim/max-size")
public Map<String, Object> streamTrimMaxSize(@RequestParam String streamKey,
@RequestParam Integer maxSize) {
checkKey(streamKey);
Assert.isTrue(ObjectUtil.isNotNull(maxSize) && maxSize > 0, "maxSize必须大于0");
long count = redissonService.streamTrim(streamKey, StreamTrimArgs.maxLen(maxSize).noLimit());
return ok(count);
}
// -------------------------------------------------------------------------
// Executor / Scheduler / RemoteService / LiveObject
// -------------------------------------------------------------------------
/**
* 获取分布式执行器信息。
* <p>
* curl "http://localhost:8080/redisson/advanced-message/executor/info?name=executor:test"
*
* @param name 执行器名称
* @return 执行器信息
*/
@GetMapping("/executor/info")
public Map<String, Object> executorInfo(@RequestParam String name) {
checkKey(name);
RExecutorService executorService = redissonService.getExecutorService(name);
RScheduledExecutorService scheduledExecutorService = redissonService.getScheduledExecutorService(name);
Map<String, Object> data = new LinkedHashMap<>();
data.put("executorClass", executorService.getClass().getName());
data.put("scheduledExecutorClass", scheduledExecutorService.getClass().getName());
data.put("shutdown", executorService.isShutdown());
data.put("terminated", executorService.isTerminated());
return ok(data);
}
/**
* 关闭分布式执行器。
* <p>
* curl -X POST "http://localhost:8080/redisson/advanced-message/executor/shutdown?name=executor:test"
*
* @param name 执行器名称
* @return 执行结果
*/
@PostMapping("/executor/shutdown")
public Map<String, Object> executorShutdown(@RequestParam String name) {
checkKey(name);
redissonService.executorShutdown(name);
return ok(true);
}
/**
* 获取远程服务对象信息。
* <p>
* curl "http://localhost:8080/redisson/advanced-message/remote-service/info"
*
* @return 远程服务信息
*/
@GetMapping("/remote-service/info")
public Map<String, Object> remoteServiceInfo() {
RRemoteService remoteService = redissonService.getRemoteService();
Map<String, Object> data = new LinkedHashMap<>();
data.put("className", remoteService.getClass().getName());
return ok(data);
}
/**
* 获取指定名称远程服务对象信息。
* <p>
* curl "http://localhost:8080/redisson/advanced-message/remote-service/named-info?name=remote:test"
*
* @param name 服务名称
* @return 远程服务信息
*/
@GetMapping("/remote-service/named-info")
public Map<String, Object> namedRemoteServiceInfo(@RequestParam String name) {
checkKey(name);
RRemoteService remoteService = redissonService.getRemoteService(name);
Map<String, Object> data = new LinkedHashMap<>();
data.put("name", name);
data.put("className", remoteService.getClass().getName());
return ok(data);
}
/**
* 获取 LiveObject 服务对象信息。
* <p>
* curl "http://localhost:8080/redisson/advanced-message/live-object/info"
*
* @return LiveObject 服务信息
*/
@GetMapping("/live-object/info")
public Map<String, Object> liveObjectInfo() {
RLiveObjectService liveObjectService = redissonService.getLiveObjectService();
Map<String, Object> data = new LinkedHashMap<>();
data.put("className", liveObjectService.getClass().getName());
return ok(data);
}
// -------------------------------------------------------------------------
// Object Listener / 对象监听入口
// -------------------------------------------------------------------------
/**
* 添加 Bucket 删除事件监听器。
* 注意:该接口只是本地测试监听器注册,服务重启后监听器会丢失。
* <p>
* curl -X POST "http://localhost:8080/redisson/advanced-message/listener/bucket/delete?key=bucket:test"
*
* @param key Redis 键
* @return 监听器 ID
*/
@PostMapping("/listener/bucket/delete")
public Map<String, Object> addBucketDeleteListener(@RequestParam String key) {
checkKey(key);
DeletedObjectListener listener = name -> log.info("监听到 Bucket 删除事件,key={},name={}", key, name);
int listenerId = redissonService.addBucketListener(key, listener);
Map<String, Object> data = new LinkedHashMap<>();
data.put("key", key);
data.put("listenerId", listenerId);
return ok(data);
}
/**
* 移除 Bucket 监听器。
* <p>
* curl -X DELETE "http://localhost:8080/redisson/advanced-message/listener/bucket?key=bucket:test&listenerId=1"
*
* @param key Redis 键
* @param listenerId 监听器 ID
* @return 执行结果
*/
@DeleteMapping("/listener/bucket")
public Map<String, Object> removeBucketListener(@RequestParam String key,
@RequestParam Integer listenerId) {
checkKey(key);
Assert.notNull(listenerId, "listenerId不能为空");
redissonService.removeBucketListener(key, listenerId);
return ok(true);
}
/**
* 添加 Queue 对象监听器。
* <p>
* curl -X POST "http://localhost:8080/redisson/advanced-message/listener/queue?key=queue:test"
*
* @param key Redis 键
* @return 监听器 ID
*/
@PostMapping("/listener/queue")
public Map<String, Object> addQueueListener(@RequestParam String key) {
checkKey(key);
DeletedObjectListener listener = name -> log.info("监听到 Bucket 删除事件,key={},name={}", key, name);
int listenerId = redissonService.addQueueListener(key, listener);
Map<String, Object> data = new LinkedHashMap<>();
data.put("key", key);
data.put("listenerId", listenerId);
return ok(data);
}
/**
* 移除 Queue 对象监听器。
* <p>
* curl -X DELETE "http://localhost:8080/redisson/advanced-message/listener/queue?key=queue:test&listenerId=1"
*
* @param key Redis 键
* @param listenerId 监听器 ID
* @return 执行结果
*/
@DeleteMapping("/listener/queue")
public Map<String, Object> removeQueueListener(@RequestParam String key,
@RequestParam Integer listenerId) {
checkKey(key);
Assert.notNull(listenerId, "listenerId不能为空");
redissonService.removeQueueListener(key, listenerId);
return ok(true);
}
/**
* 转换可靠队列消息。
*
* @param message 可靠队列消息
* @return Map
*/
private Map<String, Object> messageToMap(Message<Object> message) {
if (ObjectUtil.isNull(message)) {
return null;
}
Map<String, Object> data = new LinkedHashMap<>();
data.put("id", message.getId());
data.put("payload", message.getPayload());
data.put("headers", message.getHeaders());
return data;
}
/**
* 解析 Stream 消息 ID。
*
* @param id ID 文本,格式为 0-0
* @return StreamMessageId
*/
private StreamMessageId parseStreamMessageId(String id) {
Assert.isTrue(StrUtil.isNotBlank(id), "Stream消息ID不能为空");
List<String> parts = StrUtil.split(id, "-");
Assert.isTrue(parts.size() == 2, "Stream消息ID格式错误,正确格式如 0-0");
long first = Long.parseLong(parts.get(0));
long second = Long.parseLong(parts.get(1));
return new StreamMessageId(first, second);
}
/**
* Stream 消息转换为响应结构。
*
* @param streamMap Stream 消息 Map
* @return 响应 Map
*/
private Map<String, Object> streamMapToResponse(Map<StreamMessageId, Map<Object, Object>> streamMap) {
if (CollUtil.isEmpty(streamMap)) {
return new LinkedHashMap<>();
}
Map<String, Object> result = new LinkedHashMap<>();
streamMap.forEach((id, body) -> result.put(id.toString(), body));
return result;
}
/**
* 校验 Redis Key。
*
* @param key Redis 键
*/
private void checkKey(String key) {
Assert.isTrue(StrUtil.isNotBlank(key), "Redis Key不能为空");
}
/**
* 成功响应。
*
* @param data 响应数据
* @return 响应 Map
*/
private Map<String, Object> ok(Object data) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("code", 0);
result.put("message", "操作成功");
result.put("data", data);
return result;
}
/**
* 可靠队列添加请求体。
*
* @author Ateng
* @since 2026-04-27
*/
@Data
public static class ReliableQueueAddRequest {
/**
* 消息体。
*/
private Object payload;
/**
* 延迟秒数。
*/
private Long delaySeconds;
/**
* 消息 TTL 秒数。
*/
private Long ttlSeconds;
/**
* 最大投递次数。
*/
private Integer deliveryLimit;
/**
* 优先级。
*/
private Integer priority;
/**
* 去重 ID。
*/
private String deduplicationId;
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037