欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-24 20:08:10
用到了fastjson用来解析配置,原理是通过自己实现response类来得到输出的内容 复制代码 代码如下:package saleandbuy.freemodule....

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