- 1、增加接口类 StoreCacheKeyPrefix,用于处理 DefaultImageCaptchaApplication 的缓存 key 处理

- 2、DefaultImageCaptchaApplication 中的缓存 key 处理修改为 接口,便于扩展使用
- 3、DefaultImageCaptchaApplication的构建器 TACBuilder 中增加对 StoreCacheKeyPrefix 的支持
This commit is contained in:
chxlay
2025-11-11 14:55:12 +08:00
parent a46cb7d5fd
commit a323c2b262
3 changed files with 42 additions and 4 deletions
@@ -2,6 +2,7 @@ package cloud.tianai.captcha.application;
import cloud.tianai.captcha.application.vo.ImageCaptchaVO; import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
import cloud.tianai.captcha.cache.CacheStore; import cloud.tianai.captcha.cache.CacheStore;
import cloud.tianai.captcha.cache.StoreCacheKeyPrefix;
import cloud.tianai.captcha.common.AnyMap; import cloud.tianai.captcha.common.AnyMap;
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant; import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.common.exception.ImageCaptchaException; import cloud.tianai.captcha.common.exception.ImageCaptchaException;
@@ -41,6 +42,8 @@ public class DefaultImageCaptchaApplication implements ImageCaptchaApplication {
private CacheStore cacheStore; private CacheStore cacheStore;
/** 验证码配置属性. */ /** 验证码配置属性. */
private final ImageCaptchaProperties prop; private final ImageCaptchaProperties prop;
/** 缓存key 前缀处理器. */
private StoreCacheKeyPrefix storeCacheKeyPrefix;
/** 默认的过期时间. */ /** 默认的过期时间. */
private long defaultExpire = 20000L; private long defaultExpire = 20000L;
@@ -50,9 +53,10 @@ public class DefaultImageCaptchaApplication implements ImageCaptchaApplication {
ImageCaptchaValidator imageCaptchaValidator, ImageCaptchaValidator imageCaptchaValidator,
CacheStore cacheStore, CacheStore cacheStore,
ImageCaptchaProperties prop, ImageCaptchaProperties prop,
CaptchaInterceptor captchaInterceptor) { CaptchaInterceptor captchaInterceptor,
StoreCacheKeyPrefix storeCacheKeyPrefix) {
this.prop = prop; this.prop = prop;
this.storeCacheKeyPrefix = null != storeCacheKeyPrefix? storeCacheKeyPrefix: StoreCacheKeyPrefix.prefixed(prop.getPrefix());
setImageCaptchaValidator(imageCaptchaValidator); setImageCaptchaValidator(imageCaptchaValidator);
setCacheStore(cacheStore); setCacheStore(cacheStore);
// 默认过期时间 // 默认过期时间
@@ -235,7 +239,8 @@ public class DefaultImageCaptchaApplication implements ImageCaptchaApplication {
} }
protected String getKey(String id) { protected String getKey(String id) {
return prop.getPrefix().concat(":").concat(id); // 改为通过接口扩展
return storeCacheKeyPrefix.compute(id);
} }
@Override @Override
@@ -1,6 +1,7 @@
package cloud.tianai.captcha.application; package cloud.tianai.captcha.application;
import cloud.tianai.captcha.cache.CacheStore; import cloud.tianai.captcha.cache.CacheStore;
import cloud.tianai.captcha.cache.StoreCacheKeyPrefix;
import cloud.tianai.captcha.cache.impl.LocalCacheStore; import cloud.tianai.captcha.cache.impl.LocalCacheStore;
import cloud.tianai.captcha.generator.ImageCaptchaGenerator; import cloud.tianai.captcha.generator.ImageCaptchaGenerator;
import cloud.tianai.captcha.generator.ImageTransform; import cloud.tianai.captcha.generator.ImageTransform;
@@ -29,6 +30,7 @@ public class TACBuilder {
private ImageCaptchaProperties prop = new ImageCaptchaProperties(); private ImageCaptchaProperties prop = new ImageCaptchaProperties();
private ResourceStore resourceStore; private ResourceStore resourceStore;
private ImageTransform imageTransform; private ImageTransform imageTransform;
private StoreCacheKeyPrefix cacheKeyPrefix;
// private List<FontWrapper> fontWrappers = new ArrayList<>(); // private List<FontWrapper> fontWrappers = new ArrayList<>();
public static TACBuilder builder() { public static TACBuilder builder() {
@@ -75,6 +77,10 @@ public class TACBuilder {
return this; return this;
} }
public TACBuilder setCacheKeyPrefix(StoreCacheKeyPrefix cacheKeyPrefix) {
this.cacheKeyPrefix = cacheKeyPrefix;
return this;
}
public TACBuilder addFont(Resource resource) { public TACBuilder addFont(Resource resource) {
this.addResource(FontCache.FONT_TYPE, resource); this.addResource(FontCache.FONT_TYPE, resource);
return this; return this;
@@ -148,7 +154,8 @@ public class TACBuilder {
if (interceptor == null) { if (interceptor == null) {
interceptor = EmptyCaptchaInterceptor.INSTANCE; interceptor = EmptyCaptchaInterceptor.INSTANCE;
} }
DefaultImageCaptchaApplication application = new DefaultImageCaptchaApplication(generator, validator, cacheStore, prop, interceptor); // 增加前缀处理接口
DefaultImageCaptchaApplication application = new DefaultImageCaptchaApplication(generator, validator, cacheStore, prop, interceptor, cacheKeyPrefix);
return application; return application;
} }
} }
@@ -0,0 +1,26 @@
package cloud.tianai.captcha.cache;
/**
* 验证码缓存Key前缀处理
*
* @author Alay
* @since 2025-11-11 13:44
*/
public interface StoreCacheKeyPrefix {
/**
* 缓存Key 计算处理
*
* @param captchaId 原始验证码Id
* @return 处理后的验证码缓存Key
*/
String compute(String captchaId);
static StoreCacheKeyPrefix prefixed(String prefix) {
if (prefix == null || prefix.isEmpty()) {
throw new IllegalArgumentException("prefix must not be null or empty");
}
return captchaId -> prefix.concat(":").concat(captchaId);
}
}