cache function
1. ehcache.xml 등록
<cache
name="mainPage"
eternal="false"
maxElementsInMemory="1000"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
overflowToDisk="false"
diskPersistent="false"
copyOnRead="false"
copyOnWrite="false"
memoryStoreEvictionPolicy="LRU"/>
2. cahe function
<%@page pageEncoding="utf-8"%>
<%@page import="org.springframework.cache.Cache"%>
<%@page import="org.springframework.cache.Cache.ValueWrapper"%>
<%@page import="org.springframework.cache.ehcache.EhCacheCacheManager"%>
<%@page import="org.springframework.cache.CacheManager"%>
<%@include file="/nci/psj/common_top.jsp" %>
<%!
/*
* 객체를 cache에 저장한다.
*
* @param request
* @param cacheKey cache 키
* @param elementKey cache에 저장할 obejct 키
* @param cacheObject cache에 저장할 object
*/
void addCacheElement(HttpServletRequest request, String cacheKey,
Object elementKey, Object cacheObject){
try{
CacheManager cm = getApplicationContext(request).getBean(EhCacheCacheManager.class);
Cache cache = cm.getCache(cacheKey);
if(cache != null){
cache.put(elementKey, cacheObject);
}
}catch(Exception e){
e.printStackTrace();
}
}
/*
* cache에서 객체를 가져온다. cache에 저장된 객체가 없으면 null 리턴.
*
* @param request
* @param cacheKey cache 키
* @param elementKey cache에 저장된 obejct 키
*/
Object getCacheElement(HttpServletRequest request, String cacheKey,
Object elementKey){
Object cacheObj = null;
try{
CacheManager cm = getApplicationContext(request).getBean(EhCacheCacheManager.class);
Cache cache = cm.getCache(cacheKey);
if(cache != null){
ValueWrapper value = cache.get(elementKey);
if(value != null){
cacheObj = value.get();
}
}
}catch(Exception e){
e.printStackTrace();
}
return cacheObj;
}
/*
* cache를 삭제한다.
*
* @param request
* @param cacheKey cache 키
*/
Object removeCache(HttpServletRequest request, String cacheKey){
Object cacheObj = null;
try{
CacheManager cm = getApplicationContext(request).getBean(EhCacheCacheManager.class);
Cache cache = cm.getCache(cacheKey);
if(cache != null){
cache.clear();
}
}catch(Exception e){
e.printStackTrace();
}
return cacheObj;
}
/*
* cache에서 객체를 삭제한다.
*
* @param request
* @param cacheKey cache 키
* @param elementKey cache에 저장된 obejct 키
*/
Object removeCacheElement(HttpServletRequest request, String cacheKey, Object elementKey){
Object cacheObj = null;
try{
CacheManager cm = getApplicationContext(request).getBean(EhCacheCacheManager.class);
Cache cache = cm.getCache(cacheKey);
if(cache != null){
cache.evict(elementKey);
}
}catch(Exception e){
e.printStackTrace();
}
return cacheObj;
}
%>
'JAVA' 카테고리의 다른 글
[JAVA] class 동적로딩 (0) | 2017.10.30 |
---|---|
[JAVA] class path에서 resource 찾기 (0) | 2017.10.30 |
[JAVA] BigInteger형을 바이트 배열로!! (0) | 2017.10.27 |
[JAVA] application att (0) | 2017.10.27 |
[JAVA] AES 대칭키 암복호화 (0) | 2017.10.27 |