Class MemoryCaptchaStore
java.lang.Object
cloud.opencode.base.captcha.store.MemoryCaptchaStore
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionCreates a store with default max size.MemoryCaptchaStore(int maxSize) Creates a store with specified max size. -
Method Summary
Modifier and TypeMethodDescriptionvoidclearAll()Clears all CAPTCHAs.voidClears all expired CAPTCHAs.voidclose()Closes this store and shuts down the cleanup scheduler.booleanChecks if a CAPTCHA exists.Retrieves a CAPTCHA answer.getAndRemove(String id) Retrieves and removes a CAPTCHA answer.voidRemoves a CAPTCHA.voidshutdown()Shuts down the cleanup scheduler.intsize()Gets the current size.voidStores a CAPTCHA answer.
-
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
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 | 大小
-
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:
closein interfaceAutoCloseable
-