diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/SpringBootCallable.java b/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/SpringBootCallable.java index 9a714520..299b309a 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/SpringBootCallable.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/SpringBootCallable.java @@ -514,6 +514,7 @@ public class SpringBootCallable implements Callable { File auth = Tool.createPath(root.getAbsolutePath(), "auth"); File base = Tool.createPath(root.getAbsolutePath(), "base"); + File cache = Tool.createPath(root.getAbsolutePath(), "cache"); File excel = Tool.createPath(root.getAbsolutePath(), "excel"); File excelannotation = Tool.createPath(excel.getAbsolutePath(), "annotation"); File excelconverter = Tool.createPath(excel.getAbsolutePath(), "converter"); @@ -531,6 +532,10 @@ public class SpringBootCallable implements Callable { for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/base/")) { freeMarkerManager.outputTemp(Tool.createFile(base.getAbsolutePath(), name), option + "/java/frame/base/" + name, ctx); } + //cache + for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/cache/")) { + freeMarkerManager.outputTemp(Tool.createFile(cache.getAbsolutePath(), name), option + "/java/frame/cache/" + name, ctx); + } //excel for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/excel/")) { diff --git a/src/main/resources/modules/SpringBoot/java/Application.ftl b/src/main/resources/modules/SpringBoot/java/Application.ftl index 969bb3b5..dbaf7f3e 100644 --- a/src/main/resources/modules/SpringBoot/java/Application.ftl +++ b/src/main/resources/modules/SpringBoot/java/Application.ftl @@ -16,7 +16,6 @@ import org.springframework.cache.annotation.EnableCaching; <#else> @EnableAutoConfiguration -@EnableCaching public class Application extends SpringBootServletInitializer { @Override diff --git a/src/main/resources/modules/SpringBoot/java/config/CacheConfig.java b/src/main/resources/modules/SpringBoot/java/config/CacheConfig.java index a425f4c3..0ee20c58 100644 --- a/src/main/resources/modules/SpringBoot/java/config/CacheConfig.java +++ b/src/main/resources/modules/SpringBoot/java/config/CacheConfig.java @@ -1,28 +1,16 @@ package ${basePackage}.config; -import net.sf.ehcache.CacheManager; -import net.sf.ehcache.config.CacheConfiguration; import org.springframework.cache.annotation.EnableCaching; -import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import xyz.wbsite.frame.cache.TokenCacheManager; @Configuration @EnableCaching public class CacheConfig { @Bean(name = "tokenCacheManager") - public EhCacheCacheManager tokenCacheManager() { - EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager(); - net.sf.ehcache.config.Configuration configuration = new net.sf.ehcache.config.Configuration(); - configuration.setMaxBytesLocalHeap("2G"); - configuration.setUpdateCheck(false);//设置不检查更新 - CacheConfiguration cacheConfiguration = new CacheConfiguration(); - cacheConfiguration.setTimeToLiveSeconds(60 * 60);//一个小时有效缓存时间 - cacheConfiguration.setTimeToIdleSeconds(30 * 60);//30分钟不使用eternal为true会失效 - configuration.addDefaultCache(cacheConfiguration); - CacheManager cacheManager = CacheManager.create(configuration); - ehCacheCacheManager.setCacheManager(cacheManager); - return ehCacheCacheManager; + public TokenCacheManager tokenCacheManager() { + return new TokenCacheManager(); } } diff --git a/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java b/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java index 58cb086d..f79ed27e 100644 --- a/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java +++ b/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java @@ -13,6 +13,7 @@ import org.springframework.security.core.Authentication; import org.springframework.security.web.access.intercept.FilterSecurityInterceptor; import ${basePackage}.frame.auth.LocalData; import ${basePackage}.frame.base.Token; +import ${basePackage}.frame.cache.TokenCacheManager; import ${basePackage}.frame.utils.CookieUtil; import ${basePackage}.module.system.mgr.TokensManager; import ${basePackage}.module.system.req.TokensBuildRequest; @@ -87,16 +88,20 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { LocalData.setToken(LocalData.getTempToken()); } else { // 组装Token ~ 这边根据实际的业务组装Token - TokensManager tokensManager = LocalData.getBean(TokensManager.class); - TokensBuildRequest tokensBuildRequest = new TokensBuildRequest(); - tokensBuildRequest.setToken(token); - TokensBuildResponse tokensBuildResponse = tokensManager.build(tokensBuildRequest, LocalData.getSysToken()); - if (tokensBuildResponse.hasError()) { - LocalData.setToken(LocalData.getTempToken()); - } else { - Token token_ = tokensBuildResponse.getToken(); - LocalData.setToken(token_); + TokenCacheManager tokenCacheManager = LocalData.getBean(TokenCacheManager.class); + Token tokenObj = tokenCacheManager.getToken(token); + if (tokenObj == null) { + TokensManager tokensManager = LocalData.getBean(TokensManager.class); + TokensBuildRequest tokensBuildRequest = new TokensBuildRequest(); + tokensBuildRequest.setToken(token); + TokensBuildResponse tokensBuildResponse = tokensManager.build(tokensBuildRequest, LocalData.getSysToken()); + if (tokensBuildResponse.hasError()) { + tokenObj = LocalData.getTempToken(); + } else { + tokenObj = tokensBuildResponse.getToken(); + } } + LocalData.setToken(tokenObj); } // Action diff --git a/src/main/resources/modules/SpringBoot/java/frame/cache/TokenCacheManager.java b/src/main/resources/modules/SpringBoot/java/frame/cache/TokenCacheManager.java new file mode 100644 index 00000000..8b9fa21d --- /dev/null +++ b/src/main/resources/modules/SpringBoot/java/frame/cache/TokenCacheManager.java @@ -0,0 +1,51 @@ +package ${basePackage}.frame.cache; + +import net.sf.ehcache.Cache; +import net.sf.ehcache.CacheManager; +import net.sf.ehcache.Element; +import net.sf.ehcache.config.CacheConfiguration; +import net.sf.ehcache.config.Configuration; +import org.springframework.cache.ehcache.EhCacheCacheManager; +import xyz.wbsite.frame.base.Token; + +public class TokenCacheManager extends EhCacheCacheManager { + private CacheManager cacheManager; + private Cache cache; + private static final String TOKEN_CACHE = "tokenCache"; + + public TokenCacheManager() { + initCacheManager(); + initCache(); + } + + private void initCacheManager() { + Configuration configuration = new Configuration(); + configuration.setMaxBytesLocalHeap("1G"); + configuration.setUpdateCheck(false);//设置不检查更新 + cacheManager = CacheManager.create(configuration); + this.setCacheManager(cacheManager); + } + + private void initCache() { + CacheConfiguration cacheConfiguration = new CacheConfiguration(); + cacheConfiguration.setTimeToLiveSeconds(60 * 60);//一个小时有效缓存时间 + cacheConfiguration.setTimeToIdleSeconds(30 * 60);//30分钟不使用eternal为true会失效 + cacheConfiguration.setName(TOKEN_CACHE); + cache = new Cache(cacheConfiguration); + this.getCacheManager().addCache(cache); + } + + public void addToken(String token, Token tokenObj) { + Element element = new Element(token, tokenObj); + this.cache.put(element); + } + + public Token getToken(String token) { + Element element = this.cache.get(token); + if (element != null) { + LogUtil.d("find cache token:" + token); + return (Token) element.getObjectValue(); + } + return null; + } +} diff --git a/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManagerImpl.java b/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManagerImpl.java index 4a39ba7d..d10ee5d1 100644 --- a/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManagerImpl.java +++ b/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManagerImpl.java @@ -14,6 +14,7 @@ import ${basePackage}.frame.utils.MapperUtil; import ${basePackage}.frame.utils.Message; import ${basePackage}.frame.utils.ValidationUtil; import ${basePackage}.module.system.ent.Tokens; +import ${basePackage}.frame.cache.TokenCacheManager; import ${basePackage}.module.system.mpr.TokensMapper; import ${basePackage}.module.system.req.TokensBuildRequest; import ${basePackage}.module.system.req.TokensCreateRequest; @@ -43,6 +44,8 @@ public class TokensManagerImpl implements TokensManager { private String admin; @Autowired private TokensMapper tokensMapper; + @Autowired + private TokenCacheManager tokenCacheManager; /** * 插入 @@ -222,6 +225,7 @@ public class TokensManagerImpl implements TokensManager { newToken.setUserName(tokens.getUserName()); } + tokenCacheManager.addToken(tokens.getToken(),newToken); response.setToken(newToken); } diff --git a/src/main/resources/modules/SpringBoot/pom.ftl b/src/main/resources/modules/SpringBoot/pom.ftl index f6d9acad..9686f97c 100644 --- a/src/main/resources/modules/SpringBoot/pom.ftl +++ b/src/main/resources/modules/SpringBoot/pom.ftl @@ -50,6 +50,11 @@ mybatis-spring-boot-starter + + org.mybatis.caches + mybatis-ehcache + + com.github.pagehelper