标签:实用案例-缓存数据到内存
CacheFilter
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.web.decorator.MyResponse;
//单例
public class CacheFilter implements Filter {
//实例变量[每个线程共享]
private Map<String,byte[]> cache = new HashMap<String,byte[]>();
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
//NO1:取得客户端访问的资源路径
String uri = request.getRequestURI();
//NO2:根据uri去缓存中查询对应的页面资源有没有
byte[] data = cache.get(uri);
//NO3:如果没有
if(data==null){
//将请求和响应放行到web资源中
MyResponse myResponse = new MyResponse(response);
chain.doFilter(request,myResponse);
//NO4:将刚取得的数据放入到缓存中,便于下次重用
data = myResponse.getBuffer();
cache.put(uri,data);
System.out.println("从服务端取得资源");
//NO5:如果有,直接从缓存中了得数据
}
//NO6:向浏览器输出数据
response.getOutputStream().write(data);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
PrintWriter pw = response.getWriter();
ServletOutputStream sout = response.getOutputStream();
sout.write(data.getBytes("UTF-8"));
MyServletOutputStream mySout = myResponse.getOutputStream();
sout.write("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".getBytes());
//mySout.write("AAAAAA".getBytes[]);
}
}
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
//对response对象的装饰/包装
public class MyResponse extends HttpServletResponseWrapper{
private HttpServletResponse response;
//缓存
private ByteArrayOutputStream bout = new ByteArrayOutputStream();
private PrintWriter pw;
public MyResponse(HttpServletResponse response) {
super(response);
this.response = response;
}
//重写父类方法,目的是将字节输出到缓存中去[字节]
public ServletOutputStream getOutputStream() throws IOException {
return new MyServletOutputStream(bout);
}
//重写父类方法,目的是将字符输出到缓存中去[字符]
public PrintWriter getWriter() throws IOException {
pw = new PrintWriter(new OutputStreamWriter(bout,"UTF-8"));
return pw;
}
//取得缓存中的数据
public byte[] getBuffer(){
if(pw!=null){
pw.flush();
}
return bout.toByteArray();
}
}
//带有缓存功能ServletOutputStream
class MyServletOutputStream extends ServletOutputStream{
private ByteArrayOutputStream bout;
public MyServletOutputStream(ByteArrayOutputStream bout) {
this.bout = bout;
}
public void write(int b) throws IOException {
}
public void write(byte[] bytes) throws IOException {
//将字节数组的内容写入缓存
bout.write(bytes);
//确保所有字节数组内容进入缓存
bout.flush();
}
}
标签:实用案例-缓存数据到内存
原文地址:http://blog.51cto.com/357712148/2105523