Using the Fastjson to parse the configuration, the principle is to achieve the output of the response class by itself
Copy Code code as follows:
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 is empty, you do not need caching, not empty, you need caching
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 is empty, all queries are cached by default
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 ();
}
}
}