filter를 사용하여 url 레벨 메모리 캐시 구현 예시
package saleandbuy.freemodule.web.filter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class CacheResp {
private long waitTime=1000*3;
private static Map<String,CacheInfo> cfgMap=new HashMap<String, CacheResp.CacheInfo>();
public static final String QUERY_STRINGS="queryStrings";
public static final String CACHED_TIME="cachedTime";
public static final String CACHE_CONFIG="cacheConfig";
public static void config(String cfgJson) {
JSONObject cfg=JSON.parseObject(cfgJson);
for (Map.Entry<String, Object> entry : cfg.entrySet()) {
String key=entry.getKey();
Map<String, Object> value=(Map<String, Object>) entry.getValue();
List queryStrings= (JSONArray)value.get(QUERY_STRINGS);
Integer cachedTime=(Integer) value.get(CACHED_TIME);
CacheInfo cacheInfo=new CacheInfo(queryStrings,cachedTime);
cfgMap.put(key, cacheInfo);
}
}
public static void cachedDo(HttpServletRequest request, HttpServletResponse response,FilterChain chain) throws IOException, ServletException {
CacheInfo cacheInfo=getCacheInfo(request);
String queryString=request.getQueryString();
//cacheInfo ,
if(cacheInfo!=null){
long now=System.currentTimeMillis();
synchronized (CacheResp.class) {
if(now-cacheInfo.lastUpdateTime>cacheInfo.cachedTime){
System.out.println("not use cache:");
ProxyResponse proxyResponse=new ProxyResponse(response);
chain.doFilter(request, proxyResponse);
cacheInfo.cacheMap.put(queryString, proxyResponse.getBuffer());
cacheInfo.lastUpdateTime=now;
}else {
System.out.println("use cache");
}
}
String cacheStr=cacheInfo.cacheMap.get(queryString).toString();
response.getWriter().write(cacheStr);
}else {
chain.doFilter(request, response);
}
}
private static CacheInfo getCacheInfo(HttpServletRequest request){
String key=request.getRequestURI().replace(request.getContextPath(), "");
CacheInfo cacheInfo=cfgMap.get(key);
if(cacheInfo!=null&&
cacheInfo.needCache(request.getQueryString())){
return cacheInfo;
}
return null;
}
public static class CacheInfo{
public List queryStrings=Arrays.asList(new String[]{"list","index"});
public long cachedTime=1000;
public long lastUpdateTime=0;
public Map<String, StringBuffer> cacheMap=new HashMap<String, StringBuffer>();
public CacheInfo(List queryStrings, Integer cachedTime) {
super();
if(cachedTime!=null){
this.cachedTime = cachedTime;
}
this.queryStrings = queryStrings;
}
/**
*
* @param queryStrings request.getQueryString
* @return
*/
public boolean needCache(String queryStrings) {
if(queryStrings==null){//queryStrings
return true;
}
return queryStrings.contains(queryStrings);
}
}
private static class ProxyResponse extends HttpServletResponseWrapper{
private StringWriter sw=new StringWriter();
// private ByteArrayOutputStream baos=new ByteArrayOutputStream();
public ProxyResponse(HttpServletResponse response) {
super(response);
}
@Override
public PrintWriter getWriter() throws IOException {
return new PrintWriter(sw);
}
public StringBuffer getBuffer() {
return sw.getBuffer();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Core Javascript] JS 분석 from scratch: 데이터, 변수, 메모리 관련 기본지식이런 언어의 기반이 되는 지식을 알아야 나중에 더 능숙하게 다룰 수 있겠다 싶었습니다. 그렇다면 기본형과 참조형 데이터를 구분하는 기준은 무엇일까요? 이것을 이해하기 위해 알아야할 배경지식들이 있습니다. 변수와 식별...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.