forked from fanruan/design
Starryi
3 years ago
4 changed files with 185 additions and 70 deletions
@ -0,0 +1,86 @@ |
|||||||
|
package com.fr.design.mainframe.theme; |
||||||
|
|
||||||
|
import com.fr.base.theme.TemplateTheme; |
||||||
|
import com.fr.base.theme.TemplateThemeConfig; |
||||||
|
import com.fr.concurrent.NamedThreadFactory; |
||||||
|
import com.fr.module.ModuleContext; |
||||||
|
|
||||||
|
import java.util.concurrent.ExecutorService; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Starryi |
||||||
|
* @version 1.0 |
||||||
|
* Created by Starryi on 2021/8/24 |
||||||
|
*/ |
||||||
|
public class AsyncThemeFetcher<T extends TemplateTheme> { |
||||||
|
|
||||||
|
private final ExecutorService executorService; |
||||||
|
private final TemplateThemeConfig<T> config; |
||||||
|
|
||||||
|
public AsyncThemeFetcher(int threads, TemplateThemeConfig<T> config) { |
||||||
|
this.executorService = ModuleContext.getExecutor().newFixedThreadPool( |
||||||
|
threads, |
||||||
|
new NamedThreadFactory(this.getClass().getName()) |
||||||
|
); |
||||||
|
this.config = config; |
||||||
|
} |
||||||
|
|
||||||
|
public void shutdown() { |
||||||
|
executorService.shutdown(); |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isShutdown() { |
||||||
|
return executorService.isShutdown(); |
||||||
|
} |
||||||
|
|
||||||
|
public void submit(String themeName, AsyncThemeFetchCallback<T> callback) { |
||||||
|
callback.beforeCachedFetch(); |
||||||
|
executorService.submit(new Runnable() { |
||||||
|
@Override |
||||||
|
public void run() { |
||||||
|
if (executorService.isShutdown()) { |
||||||
|
return; |
||||||
|
} |
||||||
|
T theme = config.cachedFetch(themeName, new TemplateThemeConfig.CacheCondition<T>() { |
||||||
|
@Override |
||||||
|
public boolean shouldCacheTheme(T theme) { |
||||||
|
return callback.shouldCache(AsyncThemeFetcher.this, theme); |
||||||
|
} |
||||||
|
}); |
||||||
|
if (executorService.isShutdown()) { |
||||||
|
return; |
||||||
|
} |
||||||
|
callback.afterCachedFetch(theme); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
public interface AsyncThemeFetchCallback<T extends TemplateTheme> { |
||||||
|
void beforeCachedFetch(); |
||||||
|
boolean shouldCache(AsyncThemeFetcher<T> fetcher, T theme); |
||||||
|
void afterCachedFetch(T theme); |
||||||
|
} |
||||||
|
|
||||||
|
public static class AsyncThemeFetchCallbackAdapter<T extends TemplateTheme> implements AsyncThemeFetchCallback<T> { |
||||||
|
|
||||||
|
@Override |
||||||
|
public void beforeCachedFetch() { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean shouldCache(AsyncThemeFetcher<T> fetcher, T theme) { |
||||||
|
// 如果Fetcher已经关闭就不放进缓存里了
|
||||||
|
// 因为可切换工作目录,所以submit时的工作目录环境与最终获取到主题数据时的工作目录环境可能不是同一个,
|
||||||
|
// 如果仍然放进缓存中,会污染当前工作目录环境的主题缓存.
|
||||||
|
// TODO: 除了根据asyncThemeFetch的关闭情况来判断是否缓存主题,也可以更加精细的判断前后的工作目录环境是否时同一个
|
||||||
|
// TODO: 后续看情况再优化吧.
|
||||||
|
return !fetcher.isShutdown(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void afterCachedFetch(T theme) { |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue