Class RedisCaptchaStore

java.lang.Object
cloud.opencode.base.captcha.store.RedisCaptchaStore
All Implemented Interfaces:
CaptchaStore

public final class RedisCaptchaStore extends Object implements 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 (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:
  • Method Details

    • builder

      public static RedisCaptchaStore.Builder builder()
      Creates a new builder. 创建新的构建器。
      Returns:
      the builder | 构建器
    • store

      public void store(String id, String answer, Duration ttl)
      Description copied from interface: CaptchaStore
      Stores a CAPTCHA answer. 存储验证码答案。
      Specified by:
      store in interface CaptchaStore
      Parameters:
      id - the CAPTCHA ID | 验证码 ID
      answer - the answer | 答案
      ttl - the time to live | 存活时间
    • get

      public Optional<String> get(String id)
      Description copied from interface: CaptchaStore
      Retrieves a CAPTCHA answer. 检索验证码答案。
      Specified by:
      get in interface CaptchaStore
      Parameters:
      id - the CAPTCHA ID | 验证码 ID
      Returns:
      the answer if present | 答案(如果存在)
    • getAndRemove

      public Optional<String> getAndRemove(String id)
      Atomically retrieves and removes the CAPTCHA answer for the given id. 原子地获取并移除给定id的验证码答案。

      If a getAndRemoveCommand was provided via the builder, it will be used to perform an atomic get-and-delete operation (e.g., Redis GETDEL). Otherwise, falls back to a non-atomic GET followed by DEL.

      如果通过构建器提供了 getAndRemoveCommand,则使用它执行原子的获取并删除操作 (例如 Redis GETDEL)。否则回退到非原子的先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:
      getAndRemove in interface CaptchaStore
      Parameters:
      id - the CAPTCHA id | 验证码id
      Returns:
      the answer if present | 答案(如果存在)
    • remove

      public void remove(String id)
      Description copied from interface: CaptchaStore
      Removes a CAPTCHA. 删除验证码。
      Specified by:
      remove in interface CaptchaStore
      Parameters:
      id - the CAPTCHA ID | 验证码 ID
    • exists

      public boolean exists(String id)
      Description copied from interface: CaptchaStore
      Checks if a CAPTCHA exists. 检查验证码是否存在。
      Specified by:
      exists in interface CaptchaStore
      Parameters:
      id - the CAPTCHA ID | 验证码 ID
      Returns:
      true if exists | 如果存在返回 true
    • clearExpired

      public void clearExpired()
      Description copied from interface: CaptchaStore
      Clears all expired CAPTCHAs. 清除所有过期的验证码。
      Specified by:
      clearExpired in interface CaptchaStore
    • clearAll

      public void clearAll()
      Description copied from interface: CaptchaStore
      Clears all CAPTCHAs. 清除所有验证码。
      Specified by:
      clearAll in interface CaptchaStore
    • size

      public int size()
      Description copied from interface: CaptchaStore
      Gets the current size. 获取当前大小。
      Specified by:
      size in interface CaptchaStore
      Returns:
      the size | 大小
    • getKeyPrefix

      public String getKeyPrefix()
      Gets the key prefix. 获取键前缀。
      Returns:
      the key prefix | 键前缀