Class RedisCaptchaStore
- All Implemented Interfaces:
CaptchaStore
Thread-safe Redis implementation of CaptchaStore using functional interfaces for Redis operations, allowing integration with any Redis client.
使用函数式接口进行Redis操作的线程安全Redis验证码存储实现, 允许与任何Redis客户端集成。
Features | 主要功能:
- Pluggable Redis client integration via functional interfaces - 通过函数式接口的可插拔 Redis 客户端集成
- Configurable key prefix for namespace isolation - 可配置键前缀用于命名空间隔离
- Automatic TTL-based expiration via Redis - 通过 Redis 自动基于 TTL 过期
- Support for Spring Data Redis, Jedis, Lettuce, etc. - 支持 Spring Data Redis、Jedis、Lettuce 等
Usage Example | 使用示例:
// With Spring Data Redis
StringRedisTemplate redisTemplate = ...;
RedisCaptchaStore store = RedisCaptchaStore.builder()
.keyPrefix("captcha:")
.setter((key, value, ttl) ->
redisTemplate.opsForValue().set(key, value, ttl))
.getter(key ->
redisTemplate.opsForValue().get(key))
.deleter(key ->
redisTemplate.delete(key))
.build();
// With Jedis (Redis 6.2+ for atomic GETDEL)
JedisPool pool = ...;
RedisCaptchaStore store = RedisCaptchaStore.builder()
.keyPrefix("captcha:")
.setter((key, value, ttl) -> {
try (Jedis jedis = pool.getResource()) {
jedis.setex(key, ttl.toSeconds(), value);
}
})
.getter(key -> {
try (Jedis jedis = pool.getResource()) {
return jedis.get(key);
}
})
.deleter(key -> {
try (Jedis jedis = pool.getResource()) {
jedis.del(key);
}
})
.getAndRemoveCommand(key -> {
try (Jedis jedis = pool.getResource()) {
return jedis.getDel(key);
}
})
.build();
Security | 安全性:
- Thread-safe: Yes (delegates to thread-safe Redis operations) - 线程安全: 是(委托给线程安全的 Redis 操作)
- Null-safe: No (setter, getter, deleter must be non-null) - 空值安全: 否(setter、getter、deleter 不能为空)
- Since:
- JDK 25, opencode-base-captcha V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for RedisCaptchaStore.static interfaceFunctional interface for Redis SET with TTL operation. -
Method Summary
Modifier and TypeMethodDescriptionstatic RedisCaptchaStore.Builderbuilder()Creates a new builder.voidclearAll()Clears all CAPTCHAs.voidClears all expired CAPTCHAs.booleanChecks if a CAPTCHA exists.Retrieves a CAPTCHA answer.getAndRemove(String id) Atomically retrieves and removes the CAPTCHA answer for the given id.Gets the key prefix.voidRemoves a CAPTCHA.intsize()Gets the current size.voidStores a CAPTCHA answer.
-
Method Details
-
builder
Creates a new builder. 创建新的构建器。- Returns:
- the builder | 构建器
-
store
Description copied from interface:CaptchaStoreStores a CAPTCHA answer. 存储验证码答案。- Specified by:
storein interfaceCaptchaStore- Parameters:
id- the CAPTCHA ID | 验证码 IDanswer- the answer | 答案ttl- the time to live | 存活时间
-
get
Description copied from interface:CaptchaStoreRetrieves a CAPTCHA answer. 检索验证码答案。- Specified by:
getin interfaceCaptchaStore- Parameters:
id- the CAPTCHA ID | 验证码 ID- Returns:
- the answer if present | 答案(如果存在)
-
getAndRemove
Atomically retrieves and removes the CAPTCHA answer for the given id. 原子地获取并移除给定id的验证码答案。If a
getAndRemoveCommandwas provided via the builder, it will be used to perform an atomic get-and-delete operation (e.g., RedisGETDEL). Otherwise, falls back to a non-atomic GET followed by DEL.如果通过构建器提供了
getAndRemoveCommand,则使用它执行原子的获取并删除操作 (例如 RedisGETDEL)。否则回退到非原子的先GET后DEL操作。Warning | 警告: Without
getAndRemoveCommand, the fallback GET+DEL is not atomic — a concurrent caller may also read the same value before the DEL executes, allowing double-validation of a CAPTCHA.如果未配置
getAndRemoveCommand,回退的 GET+DEL 不是原子的 —— 并发调用者可能在 DEL 执行之前也读取到相同的值,导致验证码被重复验证。- Specified by:
getAndRemovein interfaceCaptchaStore- Parameters:
id- the CAPTCHA id | 验证码id- Returns:
- the answer if present | 答案(如果存在)
-
remove
Description copied from interface:CaptchaStoreRemoves a CAPTCHA. 删除验证码。- Specified by:
removein interfaceCaptchaStore- Parameters:
id- the CAPTCHA ID | 验证码 ID
-
exists
Description copied from interface:CaptchaStoreChecks if a CAPTCHA exists. 检查验证码是否存在。- Specified by:
existsin interfaceCaptchaStore- Parameters:
id- the CAPTCHA ID | 验证码 ID- Returns:
- true if exists | 如果存在返回 true
-
clearExpired
public void clearExpired()Description copied from interface:CaptchaStoreClears all expired CAPTCHAs. 清除所有过期的验证码。- Specified by:
clearExpiredin interfaceCaptchaStore
-
clearAll
public void clearAll()Description copied from interface:CaptchaStoreClears all CAPTCHAs. 清除所有验证码。- Specified by:
clearAllin interfaceCaptchaStore
-
size
public int size()Description copied from interface:CaptchaStoreGets the current size. 获取当前大小。- Specified by:
sizein interfaceCaptchaStore- Returns:
- the size | 大小
-
getKeyPrefix
-