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;
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()){
文字列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);
}
}
public static void cacheddo(httpservletrequest request、httpservletresponse応答、フィルターチェーンチェーン)がioexception、servletexception {
cacheinfo cacheinfo = getCacheInfo(リクエスト);
文字列QueryString = request.getQueryString();
// cacheinfoが空である場合、キャッシュが空でない場合、キャッシュは必要ありません。
if(cacheinfo!= null){
long now = system.currenttimemillis();
同期(cacheresp.class){
if(now-cacheinfo.lastupdatime> cacheinfo.cachedtime){
system.out.println( "キャッシュを使用しない:");
proxyResponse proxyResponse = new proxyResponse(response);
Chain.dofilter(request、proxyResponse);
cacheinfo.cachemap.put(querystring、proxyresponse.getBuffer());
cacheinfo.lastupdateTime = now;
}それ以外 {
System.out.println( "Cacheを使用");
}
}
string cachestr = cacheinfo.cachemap.get(queryString).toString();
Response.getWriter()。write(cachestr);
}それ以外 {
Chain.dofilter(リクエスト、応答);
}
}
private static cacheinfo getCacheInfo(httpservletrequestリクエスト){
string key = request.getRequesturi()。置き換え(request.getContextPath()、 "");
cacheinfo cacheinfo = cfgmap.get(key);
if(cacheinfo!= null &&
cacheinfo.needcache(request.getquerystring())){
cacheinfoを返します。
}
nullを返します。
}
public static class cacheinfo {
パブリックリストquerystrings = arrays.aslist(new String [] {"list"、 "index"});
Public Long CachedTime = 1000;
Public Long LastUpDateTime = 0;
パブリックマップ<文字列、stringbuffer> cachemap = new hashmap <string、stringbuffer>();
public cacheinfo(リストクエリストリング、整数キャッシュタイム){
素晴らしい();
if(cachedtime!= null){
this.cachedtime = cachedtime;
}
this.querystrings = querystrings;
}
/**
*
* @param querystrings request.getquerystring
* @戻る
*/
パブリックブールニードキャッシュ(String Querystrings){
if(querystrings == null){// queryStringsが空の場合、すべてのクエリはデフォルトでキャッシュされます
trueを返します。
}
return querystrings.contains(querystrings);
}
}
private static class proxyResponseはhttpservletResponseWrapperを拡張します{
private stringwriter sw = new StringWriter();
// private bytearrayoutputStream baos = new bytearrayoutputStream();
public proxyResponse(httpservletResponse応答){
super(response);
}
@オーバーライド
public printwriter getWriter()はioExceptionをスローします{
新しいprintwriter(sw)を返します。
}
public stringbuffer getBuffer(){
sw.getBufferを返します();
}
}
}