Interface CaptchaStore

All Known Implementing Classes:
HashedCaptchaStore, MemoryCaptchaStore, RedisCaptchaStore

public interface CaptchaStore
Captcha Store - Interface for CAPTCHA storage 验证码存储 - 验证码存储接口

This interface defines the contract for storing and retrieving CAPTCHA data.

此接口定义了存储和检索验证码数据的契约。

Features | 主要功能:

  • Store and retrieve CAPTCHA answers with TTL - 存储和检索带 TTL 的验证码答案
  • Atomic get-and-remove for one-time validation - 原子获取并删除用于一次性验证
  • Expiration management - 过期管理
  • Factory methods for memory-based stores - 基于内存存储的工厂方法

Usage Examples | 使用示例:

CaptchaStore store = CaptchaStore.memory();
store.store("captcha-id", "answer", Duration.ofMinutes(5));
Optional<String> answer = store.getAndRemove("captcha-id");

Security | 安全性:

  • Thread-safe: Implementation-dependent - 线程安全: 取决于实现
  • Null-safe: No (parameters must be non-null) - 空值安全: 否(参数不能为空)
Since:
JDK 25, opencode-base-captcha V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • store

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

      Retrieves a CAPTCHA answer. 检索验证码答案。
      Parameters:
      id - the CAPTCHA ID | 验证码 ID
      Returns:
      the answer if present | 答案(如果存在)
    • getAndRemove

      Optional<String> getAndRemove(String id)
      Retrieves and removes a CAPTCHA answer. 检索并删除验证码答案。
      Parameters:
      id - the CAPTCHA ID | 验证码 ID
      Returns:
      the answer if present | 答案(如果存在)
    • remove

      void remove(String id)
      Removes a CAPTCHA. 删除验证码。
      Parameters:
      id - the CAPTCHA ID | 验证码 ID
    • exists

      boolean exists(String id)
      Checks if a CAPTCHA exists. 检查验证码是否存在。
      Parameters:
      id - the CAPTCHA ID | 验证码 ID
      Returns:
      true if exists | 如果存在返回 true
    • clearExpired

      void clearExpired()
      Clears all expired CAPTCHAs. 清除所有过期的验证码。
    • clearAll

      void clearAll()
      Clears all CAPTCHAs. 清除所有验证码。
    • size

      int size()
      Gets the current size. 获取当前大小。
      Returns:
      the size | 大小
    • memory

      static CaptchaStore memory()
      Creates a memory-based store. 创建基于内存的存储。
      Returns:
      the store | 存储
    • memory

      static CaptchaStore memory(int maxSize)
      Creates a memory-based store with max size. 创建具有最大大小的基于内存的存储。
      Parameters:
      maxSize - the maximum size | 最大大小
      Returns:
      the store | 存储
    • hashed

      static HashedCaptchaStore hashed(CaptchaStore delegate)
      Wraps a store with answer hashing for defense in depth. 使用答案哈希包装存储以实现纵深防御。
      Parameters:
      delegate - the delegate store | 被包装的存储
      Returns:
      the hashed store | 哈希存储