Java Application Cache
Application Cache is used very wide.
we need to cache user/business information in application, cause of it is used often, so don't need to clear cache.
sure, we can control of it, but if we cache so many messages, we will be lose control. every business want to cache something to improve it's performance, so what's the solution?
we can use soft reference, it will be GC before out of memory, and it used cache in so many cache framework.
package com.statestreet.tlp.cache;
import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
* Common cache use scenarios include an application cache, a second level (L2)[EHCache] cache and a hybrid cache.
*
*
* Global Cache.[Application Cache]
*
* Strong ==> Soft ==> Weak ==> Phantom
*
* SoftReference: Soft references are most often used to implement memory-sensitive caches
* Soft reference objects, which are cleared at the discretion of the garbage collector in response to memory demand.
*
* WeakHashMap: the key is not usually use will be delete, there map is not synchronized {@link Collections#synchronizedMap Collections.synchronizedMap}.
* case of back thread delete object don't use, so it is not used abroad
* PhantomReference: garbage collector will be delete object, phantom reference always returns null
.
* almost we can't see this example.
*
*
* @author e557400
*
*/
public class GlobalCache {
protected final Log log = LogFactory.getLog(GlobalCache.class);
private static final GlobalCache gc = new GlobalCache();
/**
* Application Cache don't clear, until we call.
*/
private ConcurrentHashMap> cache = new ConcurrentHashMap>();
private ConcurrentHashMap cachehitCount = new ConcurrentHashMap();
private GlobalCache(){
}
public void clear(){
cache.clear();
cachehitCount.clear();
}
public static GlobalCache getInstance(){
return gc;
}
/**
* Thread-safe cache put.
*
* cache get method is not thread-safe, invalidate by other thread.
* but map.putIfAbsent() which is atomic and therefore thread-safe.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with the specified key,
* or null if there was no mapping for the key
*/
public Object put(CacheKey key, Object value) {
if(value == null){
throw new IllegalArgumentException("put GlobalCache value can't be null");
}
Object ret = get(key);
if (value != ret) {
if(log.isDebugEnabled()){
if(ret == null){
log.debug("put new Cache( key["+key+"], value["+value+"] )");
}else{
log.error("attempt to override Cache( key["+key+"], old value["+ret+"] to value["+value+"] ), but will be failed.");
}
}
ret = cache.putIfAbsent(key, new SoftReference
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.