Fastjson é usado para analisar a configuração.
A cópia do código é a seguinte:
pacote saleandbuy.freemodule.web.filter;
importar java.io.ioException;
importar java.io.printwriter;
importar java.io.stringwriter;
importar java.util.arrays;
importar java.util.hashmap;
importar java.util.list;
importar java.util.map;
importar javax.servlet.filterchain;
importar javax.servlet.servletexception;
importar javax.servlet.http.httpServletRequest;
importar javax.servlet.http.httpServletResponse;
importar javax.servlet.http.httpServletResponseWrapper;
importação com.alibaba.fastjson.json;
importação com.alibaba.fastjson.jsonArray;
importação com.alibaba.fastjson.jsonObject;
classe pública Cacheresp {
private Waittime longo = 1000*3;
mapa estático privado <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);
para (map.entry <string, objeto> Entrada: cfg.entryset ()) {
String key = Entry.getKey ();
Mapa <string, object> value = (map <string, object>) entrada.getValue ();
List Querystrings = (jsonArray) value.get (query_strings);
Inteiro CachedTime = (número inteiro) Value.get (cached_time);
CacheInfo cacheInfo = novo cacheInfo (Querystrings, cachedtime);
cfgmap.put (chave, cacheInfo);
}
}
public static void Cacheddo (solicitação httpServletRequest, resposta httpServletResponse, cadeia de filtro) lança ioexception, servletexception {
CacheInfo CacheInfo = getCacheInfo (solicitação);
String querystring = request.getQueryString ();
// Se o CacheInfo estiver vazio, não será necessário cache.
if (cacheInfo! = null) {
long now = System.currenttimemillis ();
Sincronizado (Cacheresp.class) {
if (agora cacheinfo.lastupdateTime> cacheinfo.cachedtime) {
System.out.println ("não use cache:");
ProxyResponse ProxyResponse = new ProxyResponse (resposta);
Chain.Dofilter (Solicitação, ProxyResponse);
cacheinfo.cachemap.put (querystring, proxyResponse.getBuffer ());
CacheInfo.LastUpDateTime = agora;
}outro {
System.out.println ("use cache");
}
}
String cachestr = cacheinfo.cachemap.get (querystring) .toString ();
Response.getWriter (). Write (Cachest);
}outro {
Chain.Dofilter (solicitação, resposta);
}
}
Cacheinfo estático privado getCacheInfo (solicitação httpServletRequest) {
String key = request.getRequesturi (). Substituir (request.getContextPath (), "");
CacheInfo cacheInfo = cfgmap.get (key);
if (cacheinfo! = null &&
cacheinfo.needcache (request.getQueryString ())) {
retornar cacheinfo;
}
retornar nulo;
}
classe estática pública Cacheinfo {
public list Querystrings = Arrays.asList (new String [] {"List", "Index"});
Public Long Cachedtime = 1000;
public Long LastUpDateTime = 0;
mapa público <string, stringbuffer> Cachemap = new Hashmap <String, StringBuffer> ();
public CacheInfo (lists de consultas, tempo inteiro em cache) {
super();
if (cachedtime! = null) {
this.cachedtime = cachedtime;
}
this.QueryStrings = Querystrings;
}
/**
*
* @param querystrings request.getQueryString
* @retornar
*/
public boolean NeedCache (String Querystrings) {
If (querystrings == null) {// Quando o querystrings está vazio, todas as consultas são armazenadas em cache por padrão
retornar true;
}
retornar querystrings.contains (Querystrings);
}
}
Classe estática privada ProxyResponse estende HttpServletResponsewrapper {
private stringwriter sw = new stringWriter ();
// private bytearrayOutputStream baos = new ByteArrayOutputStream ();
Public ProxyResponse (HTTPServletResponse Response) {
super (resposta);
}
@Override
public printWriter getWriter () lança ioexception {
devolver novo PrintWriter (SW);
}
public stringbuffer getBuffer () {
retornar sw.getBuffer ();
}
}
}