parent
1a69d2feee
commit
86fbeeac45
@ -1,28 +1,16 @@
|
|||||||
package ${basePackage}.config;
|
package ${basePackage}.config;
|
||||||
|
|
||||||
import net.sf.ehcache.CacheManager;
|
|
||||||
import net.sf.ehcache.config.CacheConfiguration;
|
|
||||||
import org.springframework.cache.annotation.EnableCaching;
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
import org.springframework.cache.ehcache.EhCacheCacheManager;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import xyz.wbsite.frame.cache.TokenCacheManager;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableCaching
|
@EnableCaching
|
||||||
public class CacheConfig {
|
public class CacheConfig {
|
||||||
|
|
||||||
@Bean(name = "tokenCacheManager")
|
@Bean(name = "tokenCacheManager")
|
||||||
public EhCacheCacheManager tokenCacheManager() {
|
public TokenCacheManager tokenCacheManager() {
|
||||||
EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
|
return new TokenCacheManager();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue