服务器之家

服务器之家 > 正文

使用filter实现url级别内存缓存示例

时间:2019-11-16 14:05     来源/作者:java教程网

用到了fastJson用来解析配置,原理是通过自己实现response类来得到输出的内容

 

复制代码代码如下:


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();
  }
 }
}

 

标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
配置IIS网站web服务器的安全策略配置解决方案
配置IIS网站web服务器的安全策略配置解决方案 2019-05-23
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
运维必须知道的关于云服务器的十个问题
运维必须知道的关于云服务器的十个问题 2019-05-24
返回顶部