Fastjson используется для проанала конфигурации.
Кода -копия выглядит следующим образом:
пакет saleandbuy.freemodule.web.filter;
импортировать java.io.ioexception;
Импорт java.io.printwriter;
импортировать java.io.stringwriter;
импортировать java.util.arrays;
импортировать java.util.hashmap;
импортировать java.util.list;
импортировать java.util.map;
Импорт javax.servlet.filterchain;
Импорт javax.servlet.servletexception;
Импорт javax.servlet.http.httpservletrequest;
Импорт javax.servlet.http.httpservletresponse;
Импорт javax.servlet.http.httpservletresponsewrapper;
Импорт com.alibaba.fastjson.json;
Импорт com.alibaba.fastjson.jsonarray;
импорт com.alibaba.fastjson.jsonobject;
открытый класс cacheresp {
частное долгое ожидание = 1000*3;
Частная статическая карта <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> intpirt: cfg.EntrySet ()) {
String key = entry.getKey ();
Map <string, object> value = (map <string, object>) entry.getValue ();
Список QueryStrings = (jsonarray) value.get (Query_strings);
Integer cachedtime = (integer) value.get (cached_time);
Cacheinfo cacheinfo = new cacheinfo (Querystrings, cachedtime);
cfgmap.put (key, cacheinfo);
}
}
Общественная статическая void cacheddo (httpservletrequest, httpservletresponse, отклик, цепь фильтров
Cacheinfo cacheinfo = getCacheinfo (запрос);
String QueryString = request.getQueryString ();
// Если Cacheinfo пуст, кэш не требуется.
if (cacheinfo! = null) {
long now = System.currentTimeMillis ();
синхронизированный (cacheresp.class) {
if (теперь cacheinfo.lastupdateTime> cacheinfo.cachedtime) {
System.out.println («Не используйте кэш:»);
ProxyResponse ProxyResponse = новый ProxyResponse (ответ);
chain.dofilter (запрос, Proxyresponse);
cacheinfo.cachemap.put (querystring, proxyresponse.getbuffer ());
cacheinfo.lastupdateTime = сейчас;
}еще {
System.out.println («Использовать кэш»);
}
}
String cachestr = cacheinfo.cachemap.get (QueryString) .toString ();
response.getWriter (). написать (cachestr);
}еще {
chain.dofilter (запрос, ответ);
}
}
Private Static Cacheinfo getCacheinfo (httpservletrequest) {
String key = request.getRequesturi (). Reply (request.getContextPath (), "");
Cacheinfo cacheinfo = cfgmap.get (key);
if (cacheinfo! = null &&
cacheinfo.needcache (request.getQueryString ())) {
вернуть Cacheinfo;
}
вернуть ноль;
}
public static class cacheinfo {
public List QueryStrings = arrays.aslist (new String [] {"list", "index"});
Public Long Cachedtime = 1000;
Public LongupDateTime = 0;
public Map <String, StringBuffer> cachemap = new Hashmap <String, StringBuffer> ();
public Cacheinfo (список QueryStrings, Integer Cachedtime) {
супер();
if (cachedtime! = null) {
this.cachedtime = cachedtime;
}
this.querystrings = QueryStrings;
}
/**
*
* @param querystrings request.getQueryString
* @возвращаться
*/
public boolean edCache (String QueryStrings) {
If (QueryStrings == null) {// Когда QueryStrings пусты, все запросы кэшируются по умолчанию
вернуть истину;
}
return QueryStrings.contains (QueryStrings);
}
}
Private Static Class Proxponessone расширяет httpservletresponsewrapper {
Private StringWriter SW = new StringWriter ();
// private bytearrayoutputstream baos = new BytearrayOutputStream ();
public proxyresponse (httpservletresponse response) {
супер (ответ);
}
@Override
public printwriter getWriter () бросает ioException {
вернуть New PrintWriter (SW);
}
public StringBuffer getBuffer () {
return sw.getbuffer ();
}
}
}