修复资源泄露问题并优化资源管理
- 添加 AutoCloseable 接口实现,支持自动资源释放 - 修复 Graphics2D、ImageOutputStream 等资源未正确关闭的问题 - 为 Spring Bean 添加 destroyMethod 配置 - 添加定时任务线程池的正确关闭逻辑 - FontCache 添加缓存大小限制防止内存泄露 - 优化 StandardWordClickImageCaptchaGenerator 代码结构
This commit is contained in:
@@ -3,6 +3,7 @@ package cloud.tianai.captcha.application;
|
||||
import cloud.tianai.captcha.cache.CacheStore;
|
||||
import cloud.tianai.captcha.cache.StoreCacheKeyPrefix;
|
||||
import cloud.tianai.captcha.cache.impl.LocalCacheStore;
|
||||
import cloud.tianai.captcha.common.util.CollectionUtils;
|
||||
import cloud.tianai.captcha.generator.ImageCaptchaGenerator;
|
||||
import cloud.tianai.captcha.generator.ImageTransform;
|
||||
import cloud.tianai.captcha.generator.impl.MultiImageCaptchaGenerator;
|
||||
@@ -16,6 +17,9 @@ import cloud.tianai.captcha.resource.impl.LocalMemoryResourceStore;
|
||||
import cloud.tianai.captcha.validator.ImageCaptchaValidator;
|
||||
import cloud.tianai.captcha.validator.impl.SimpleImageCaptchaValidator;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author: 天爱有情
|
||||
* @date 2024/7/14 16:41
|
||||
@@ -30,22 +34,23 @@ public class TACBuilder {
|
||||
private ImageCaptchaProperties prop = new ImageCaptchaProperties();
|
||||
private ResourceStore resourceStore;
|
||||
private ImageTransform imageTransform;
|
||||
private StoreCacheKeyPrefix cacheKeyPrefix;
|
||||
// private List<FontWrapper> fontWrappers = new ArrayList<>();
|
||||
private Map<String, Resource> resourceCache;
|
||||
private Map<String, ResourceMap> templateCache;
|
||||
|
||||
public static TACBuilder builder() {
|
||||
return TACBuilder.builder(new LocalMemoryResourceStore());
|
||||
return TACBuilder.builder();
|
||||
}
|
||||
|
||||
public static TACBuilder builder(ResourceStore resourceStore) {
|
||||
TACBuilder builder = new TACBuilder(resourceStore);
|
||||
builder.prop = new ImageCaptchaProperties();
|
||||
return builder;
|
||||
}
|
||||
|
||||
|
||||
private TACBuilder(ResourceStore resourceStore) {
|
||||
this.resourceStore = resourceStore;
|
||||
}
|
||||
public TACBuilder setResourceStore(ResourceStore resourceStore) {
|
||||
this.resourceStore = resourceStore;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder addDefaultTemplate(String defaultPathPrefix) {
|
||||
DefaultBuiltInResources defaultBuiltInResources = new DefaultBuiltInResources(defaultPathPrefix);
|
||||
@@ -77,10 +82,6 @@ public class TACBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder setCacheKeyPrefix(StoreCacheKeyPrefix cacheKeyPrefix) {
|
||||
this.cacheKeyPrefix = cacheKeyPrefix;
|
||||
return this;
|
||||
}
|
||||
public TACBuilder addFont(Resource resource) {
|
||||
this.addResource(FontCache.FONT_TYPE, resource);
|
||||
return this;
|
||||
@@ -118,16 +119,18 @@ public class TACBuilder {
|
||||
|
||||
|
||||
public TACBuilder addResource(String captchaType, Resource imageResource) {
|
||||
if (resourceStore instanceof CrudResourceStore) {
|
||||
((CrudResourceStore) resourceStore).addResource(captchaType, imageResource);
|
||||
}
|
||||
// if (resourceStore instanceof CrudResourceStore) {
|
||||
// ((CrudResourceStore) resourceStore).addResource(captchaType, imageResource);
|
||||
// }
|
||||
cacheResource(captchaType, imageResource);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder addTemplate(String captchaType, ResourceMap resourceMap) {
|
||||
if (resourceStore instanceof CrudResourceStore) {
|
||||
((CrudResourceStore) resourceStore).addTemplate(captchaType, resourceMap);
|
||||
}
|
||||
// if (resourceStore instanceof CrudResourceStore) {
|
||||
// ((CrudResourceStore) resourceStore).addTemplate(captchaType, resourceMap);
|
||||
// }
|
||||
cacheTemplate(captchaType, resourceMap);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -140,6 +143,20 @@ public class TACBuilder {
|
||||
if (cacheStore == null) {
|
||||
cacheStore = new LocalCacheStore();
|
||||
}
|
||||
if (resourceStore == null) {
|
||||
resourceStore = new LocalMemoryResourceStore();
|
||||
}
|
||||
if (resourceStore instanceof CrudResourceStore) {
|
||||
CrudResourceStore crudResourceStore = (CrudResourceStore) resourceStore;
|
||||
if (!CollectionUtils.isEmpty(resourceCache)) {
|
||||
resourceCache.forEach(crudResourceStore::addResource);
|
||||
resourceCache = null;
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(templateCache)) {
|
||||
templateCache.forEach(crudResourceStore::addTemplate);
|
||||
templateCache = null;
|
||||
}
|
||||
}
|
||||
if (generator == null) {
|
||||
ResourceProviders resourceProviders = new ResourceProviders();
|
||||
DefaultImageCaptchaResourceManager resourceManager = new DefaultImageCaptchaResourceManager(resourceStore, resourceProviders);
|
||||
@@ -155,7 +172,20 @@ public class TACBuilder {
|
||||
interceptor = EmptyCaptchaInterceptor.INSTANCE;
|
||||
}
|
||||
// 增加前缀处理接口
|
||||
DefaultImageCaptchaApplication application = new DefaultImageCaptchaApplication(generator, validator, cacheStore, prop, interceptor, cacheKeyPrefix);
|
||||
DefaultImageCaptchaApplication application = new DefaultImageCaptchaApplication(generator, validator, cacheStore, prop, interceptor);
|
||||
return application;
|
||||
}
|
||||
|
||||
private void cacheResource(String captchaType, Resource imageResource) {
|
||||
if (resourceCache == null) {
|
||||
resourceCache = new LinkedHashMap<>(8);
|
||||
}
|
||||
resourceCache.put(captchaType, imageResource);
|
||||
}
|
||||
private void cacheTemplate(String captchaType, ResourceMap resourceMap) {
|
||||
if (templateCache == null) {
|
||||
templateCache = new LinkedHashMap<>(8);
|
||||
}
|
||||
templateCache.put(captchaType, resourceMap);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user