parent
86fbeeac45
commit
35fe7d28e9
@ -1,16 +1,43 @@
|
||||
package ${basePackage}.config;
|
||||
|
||||
import net.sf.ehcache.Cache;
|
||||
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 TokenCacheManager tokenCacheManager() {
|
||||
return new TokenCacheManager();
|
||||
public static final String TOKEN_CACHE = "tokenCache";
|
||||
|
||||
@Bean(name = TOKEN_CACHE)
|
||||
public EhCacheCacheManager getCacheManager() {
|
||||
net.sf.ehcache.config.Configuration configuration = new net.sf.ehcache.config.Configuration();
|
||||
configuration.setMaxBytesLocalHeap("1G");
|
||||
configuration.updateCheck(false);
|
||||
configuration.addDiskStore(new DiskStoreConfiguration().path("java.io.tmpdir"));
|
||||
CacheManager cacheManager = CacheManager.create(configuration);
|
||||
|
||||
// 添加token缓存
|
||||
cacheManager.addCache(buildTokenCache());
|
||||
return new EhCacheCacheManager(cacheManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建TokenCache
|
||||
*
|
||||
* @return 缓存
|
||||
*/
|
||||
private Cache buildTokenCache() {
|
||||
CacheConfiguration config = new CacheConfiguration();
|
||||
config.setMemoryStoreEvictionPolicy("LFU");//最少使用
|
||||
config.setTimeToLiveSeconds(60);//最长有效时间
|
||||
config.setTimeToIdleSeconds(60);//无访问最长有效时间
|
||||
config.setName(TOKEN_CACHE);
|
||||
return new Cache(config);
|
||||
}
|
||||
}
|
||||
|
@ -1,51 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue