时间:2021-05-19
用到了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();
}
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了php内存缓存实现方法。分享给大家供大家参考。具体如下:在php中缓存分为很多种类型如,内存缓存,文件缓存,页面缓存。本文要来讲述关于php中内存
本文实例讲述了Spring实战之类级别缓存实现与使用方法。分享给大家供大家参考,具体如下:一配置文件
前言顾名思义文件缓存转内存缓存就是将存储在文件中的数据转到内存中去,实现磁盘操作转为内存操作,这样可以大大提高数据访问速度,并能实现缓存数据的分布式部署。文件缓
本文实例讲述了php使用filter_var函数判断邮箱,url,ip格式。分享给大家供大家参考,具体如下:以前使用php的时候还不知道有过滤器filter这玩
php程序在抵抗大流量访问的时候动态网站往往都是难以招架,所以要引入缓存机制,一般情况下有两种类型缓存一、文件缓存二、数据查询结果缓存,使用内存来实现高速缓存本