Skip to content

Instantly share code, notes, and snippets.

@an-sangkil
Created December 14, 2015 02:01
Show Gist options
  • Save an-sangkil/50aa90208c4cc0ef66cb to your computer and use it in GitHub Desktop.
Save an-sangkil/50aa90208c4cc0ef66cb to your computer and use it in GitHub Desktop.
JAVA Cache
package oo.cjoext.service.allthat.common.cache;
public class AllthatCache {
private String name;
private int hitCount;
private Object object;
public AllthatCache() {
}
public AllthatCache(int hitCount) {
this.hitCount = hitCount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHitCount() {
return hitCount;
}
public void setHitCount(int hitCount) {
this.hitCount = hitCount;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
@Override
public String toString() {
return "AllthatCache [name=" + name + ", hitCount=" + hitCount + ", object=" + object + "]";
}
}
package oo.cjoext.service.allthat.common.cache;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import oo.cjoext.common.ExtGetBeanUtil;
import oo.cjoext.common.dto.ExtAllthatTokenDto;
public class AllthatCacheManager<K, V> {
public static final int MAX_ENTRIES = 3;
public LinkedHashMap<K, AllthatCache> ALLTHAT_CACHE = new LinkedHashMap<K, AllthatCache>();
public ExtAllthatTokenDto getCacheToken(String key) {
if(ALLTHAT_CACHE.get(key).getObject() instanceof ExtAllthatTokenDto) {
return (ExtAllthatTokenDto)ALLTHAT_CACHE.get(key).getObject();
}
return null;
}
/**
* 저장
*/
public void save(K key, V value) {
if(ALLTHAT_CACHE.get(key) == null) {
ExtAllthatTokenDto extAllthatTokenDto = ExtGetBeanUtil.getItemManAllthatDaoModule().selectCellerToken("");
if(MAX_ENTRIES <= ALLTHAT_CACHE.size()) {
lastEntryDelete();
}
if(value instanceof AllthatCache) {
((AllthatCache)value).setObject(extAllthatTokenDto);
ALLTHAT_CACHE.put(key, (AllthatCache)value);
}
} else {
AllthatCache allthatCache = ALLTHAT_CACHE.get(key);
allthatCache.setHitCount(allthatCache.getHitCount() + 1);
ALLTHAT_CACHE.put(key, allthatCache);
}
}
public void orderByDesc() {
//ValueComparator vc = new ValueComparator(ALLTHAT_CACHE);
//TreeMap<K,AllthatCache> sorted_map = new TreeMap<K,AllthatCache>(vc);
//sorted_map.putAll(ALLTHAT_CACHE);
}
/**
* 캐시 삭제
* 히드 카운트 수로 정렬후 가장 낮은 히트카운트 삭제
*/
public void lastEntryDelete() {
// 히트 카운트 수로 정렬
ValueComparator vc = new ValueComparator(ALLTHAT_CACHE);
TreeMap<K, AllthatCache> sortedMap = new TreeMap<K, AllthatCache>(vc);
sortedMap.putAll(ALLTHAT_CACHE);
// 정열된 마지막 키중 카운트가 낮은 키 삭제
ALLTHAT_CACHE.remove(sortedMap.lastEntry().getKey());
}
/**
* 캐시 초기화
*/
public void initialization() {
ALLTHAT_CACHE.clear();
}
/**
* 정렬에 사용하기위한 내부 클래스
* @author skan
*
*/
class ValueComparator implements Comparator<K> {
Map<String, AllthatCache> base;
@SuppressWarnings("unchecked")
public ValueComparator(LinkedHashMap<?, ?> map) {
this.base = (Map<String, AllthatCache>)map;
}
public int compare(K a, K b) {
//내림차순
if(((AllthatCache)base.get(a)).getHitCount() >= ((AllthatCache)base.get(b)).getHitCount()) {
return -1;
} else {
return 1;
} // returning 0 would merge keys
}
}
}
package oo.cjoext.service.allthat.common.cache;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* LRU Cache Manager
*
* 자주 이용되는 Token 과 Category에 적용.
* @author skan
*
*/
public class AllthatTokenCache<K, V> {
private static AllthatTokenCache<?, ?> allthatTokenCache;
@SuppressWarnings("rawtypes")
public static AllthatTokenCache<?,?> getInstance() {
if(allthatTokenCache == null ) {
synchronized(AllthatTokenCache.class) {
allthatTokenCache = new AllthatTokenCache();
return allthatTokenCache;
}
}
return allthatTokenCache;
}
private int MAX_ENTRIES;
private AllthatTokenCache() {
MAX_ENTRIES = 3;
}
public AllthatTokenCache(int cacheMaxSize) {
MAX_ENTRIES = cacheMaxSize;
}
private Map<K, V> cache = Collections.synchronizedMap(new LinkedHashMap<K, V>(MAX_ENTRIES, 4.5f, true) {
private static final long serialVersionUID = 7178628060319029923L;
/**
* 가장 오래된 값을 삭제
*/
public boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return this.size() > MAX_ENTRIES;
}
});
public synchronized V get(K key) {
return cache.get(key);
}
public synchronized V put(K key, V value) {
return cache.put(key, value);
}
public void clear() {
cache.clear();
}
public synchronized int usedEntries() {
return cache.size();
}
public synchronized Collection<Map.Entry<K, V>> getAll() {
return new ArrayList<Map.Entry<K, V>>(cache.entrySet());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment