Class RedisCaptchaStore
java.lang.Object
cloud.opencode.base.captcha.store.RedisCaptchaStore
- All Implemented Interfaces:
CaptchaStore
Redis Captcha Store - Redis-based CAPTCHA storage
Redis验证码存储 - 基于Redis的验证码存储
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
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);
}
})
.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 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) Retrieves and removes a CAPTCHA answer.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
Description copied from interface:CaptchaStoreRetrieves and removes a CAPTCHA answer. 检索并删除验证码答案。- 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
-