Class MemoryCaptchaStore

java.lang.Object
cloud.opencode.base.captcha.store.MemoryCaptchaStore
All Implemented Interfaces:
CaptchaStore, AutoCloseable

public final class MemoryCaptchaStore extends Object implements CaptchaStore, AutoCloseable
Memory Captcha Store - In-memory CAPTCHA storage 内存验证码存储 - 内存中的验证码存储

Thread-safe in-memory implementation of CaptchaStore.

线程安全的内存验证码存储实现。

Features | 主要功能:

  • ConcurrentHashMap-based storage - 基于 ConcurrentHashMap 的存储
  • Automatic expiration cleanup via scheduled executor - 通过调度执行器自动清理过期数据
  • Configurable maximum size with LRU eviction - 可配置最大大小及 LRU 驱逐
  • AutoCloseable for resource cleanup - AutoCloseable 用于资源清理

Usage Examples | 使用示例:

try (MemoryCaptchaStore store = new MemoryCaptchaStore(5000)) {
    store.store("id", "answer", Duration.ofMinutes(5));
    Optional<String> answer = store.getAndRemove("id");
}

Security | 安全性:

  • Thread-safe: Yes (ConcurrentHashMap + atomic operations) - 线程安全: 是
  • 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:
  • Constructor Details

    • MemoryCaptchaStore

      public MemoryCaptchaStore()
      Creates a store with default max size. 创建具有默认最大大小的存储。
    • MemoryCaptchaStore

      public MemoryCaptchaStore(int maxSize)
      Creates a store with specified max size. 创建具有指定最大大小的存储。
      Parameters:
      maxSize - the maximum size | 最大大小
  • Method Details

    • 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)
      Description copied from interface: CaptchaStore
      Retrieves and removes a CAPTCHA answer. 检索并删除验证码答案。
      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 | 大小
    • shutdown

      public void shutdown()
      Shuts down the cleanup scheduler. 关闭清理调度器。
    • close

      public void close()
      Closes this store and shuts down the cleanup scheduler. 关闭此存储并关闭清理调度器。

      Delegates to shutdown(). Enables try-with-resources usage.

      委托给 shutdown()。支持 try-with-resources 使用。

      Specified by:
      close in interface AutoCloseable