当前位置:网站首页>Demo of guavacache
Demo of guavacache
2022-06-30 09:28:00 【Xiao Lu, a migrant worker in Beijing】
cache Interface
public interface Cache {
<T> T get(Object key, Class<T> type);
boolean has(Object key);
void put(Object key, Object value);
void del(Object key);
}
cache factory
public class CacheFactory {
private static class GuavaCache implements Cache {
private org.springframework.cache.Cache cache;
GuavaCache(String cacheName, int expiredMinutes) {
cache = new ConcurrentMapCache(cacheName,
CacheBuilder.newBuilder()
.expireAfterWrite(expiredMinutes, TimeUnit.MINUTES)
.build().asMap(),
false);
}
GuavaCache(String cacheName, int expiredTime, TimeUnit timeUnit) {
cache = new ConcurrentMapCache(cacheName,
CacheBuilder.newBuilder()
.expireAfterWrite(expiredTime, timeUnit)
.build().asMap(),
false);
}
@Override
public <T> T get(Object key, Class<T> type) {
return cache.get(key, type);
}
@Override
public boolean has(Object key) {
return cache.get(key) != null;
}
@Override
public void put(Object key, Object value) {
cache.put(key, value);
}
@Override
public void del(Object key) {
cache.put(key,"");
}
}
static private final String PREFIX = "CACHE_FACTORY_";
static private int sn = 1;
synchronized static public Cache getCacheWithExpiredMinutes(int expiredMinutes) {
String cacheName = PREFIX + sn;
sn += 1;
return new GuavaCache(cacheName, expiredMinutes);
}
synchronized static public Cache getCacheWithExpiredSeconds(int expiredSeconds) {
String cacheName = PREFIX + sn;
sn += 1;
return new GuavaCache(cacheName, expiredSeconds, TimeUnit.SECONDS);
}
}
Use
@Service
public class GuangPingDistributionDataHandler {
private Cache guangpingKeyWordListCache = CacheFactory.getCacheWithExpiredSeconds(10);
private static final String CACHEKEY = "guangpingCacheKey";
@Autowired
private DataToGuangPingAsynProducer dataToGuangPingAsynProducer;
@Autowired
private KeywordCommonRelationMapper keywordCommonRelationMapper;
@Override
Boolean isHitKeywords(List<String> keyWordList) {
List<String> giangPingKeyWordListFromCache = guangpingKeyWordListCache.get(CACHEKEY, List.class);
if (null == giangPingKeyWordListFromCache || giangPingKeyWordListFromCache.size() == 0) {
/** If there is no cache **/
List<String> giangPingKeyWordListFromDB = keywordCommonRelationMapper.selectKeyListByValue(String.valueOf(SystemSourceEnum.GUANGPING.getCode()));
giangPingKeyWordListFromCache = giangPingKeyWordListFromDB;
guangpingKeyWordListCache.put(CACHEKEY, giangPingKeyWordListFromDB);
}
for(String keyWord : keyWordList){
if(giangPingKeyWordListFromCache.contains(keyWord)){
return true;
}
}
return false;
}
}
边栏推荐
- Opencv learning notes-day6-7 (scroll bar operation demonstration is used to adjust image brightness and contrast, and createtrackbar() creates a scroll bar function)
- ES6 learning road 5 symbol
- Niuke walks on the tree (ingenious application of parallel search)
- Coredata acquisition in swift sorting, ascending, descending
- Wechat development tool (applet)
- Applet learning path 2 - event binding
- Design specification for smart speakers v1.0
- The elegant combination of walle and Jianbao
- Opencv learning notes -day 12 (ROI region extraction and inrange() function operation)
- Abstract factory pattern
猜你喜欢
Duplicate entry '2' for key 'primary appears in JPA‘
Installation, use and explanation of vulnerability scanning tool OpenVAS
Bottomsheetbehavior principle of realizing the home page effect of Gaode map
Abstract factory pattern
Express file upload
Opencv learning notes-day6-7 (scroll bar operation demonstration is used to adjust image brightness and contrast, and createtrackbar() creates a scroll bar function)
Agp7.0|kts makes a reinforced plug-in
Summary of Android knowledge points and common interview questions
Based on svelte3 X desktop UI component library svelte UI
Harmonyos actual combat - ten thousand words long article understanding service card development process
随机推荐
Sort (simple description)
Based on svelte3 X desktop UI component library svelte UI
Do you want the dialog box that pops up from the click?
桂林 稳健医疗收购桂林乳胶100%股权 填补乳胶产品线空白
Esp32 things (x): other functions
2021-10-20
Abstract factory pattern
ES6 learning road 5 symbol
Metasploit practice - SSH brute force cracking process
Deep understanding of continuation principle
Concatapter tutorial
Opencv learning notes-day6-7 (scroll bar operation demonstration is used to adjust image brightness and contrast, and createtrackbar() creates a scroll bar function)
Tutorial for beginners of small programs day01
Rew acoustic test (IV): test principle of rew
Cronexpression expression explanation and cases
[shutter] solve failed assertion: line 5142 POS 12: '_ debugLocked‘: is not true.
Detailed explanation of rect class
Deeply understand the working principle of kotlin collaboration suspend (beginners can also understand it)
Rew acoustic test (II): offline test
The elegant combination of walle and Jianbao