redis缓存jsp页面提高速度

技术讲解:用filter 获取 spring缓存的页面 存路径到redis里面,每次请求进来 redis没有 就去渲染 顺便存一个 下次请求再来就有了,方便省事 

三个文件:如下

package com.dls.config.redis;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

/**
 * @author wl
 */
public class CustomHttpServletResponse extends HttpServletResponseWrapper {

    private ServletOutputStream outputStream;
    private PrintWriter writer;
    private CustomServletOutputStream cout;

    public CustomHttpServletResponse(HttpServletResponse response) {
        super(response);
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        if (writer != null) {
            throw new IllegalStateException("getWriter() has already been called on this response.");
        }

        if (outputStream == null) {
            outputStream = getResponse().getOutputStream();
            cout = new CustomServletOutputStream(outputStream);
        }

        return cout;

    }

    @Override
    public PrintWriter getWriter() throws IOException {
        if (outputStream != null) {
            throw new IllegalStateException("getOutputStream() has already been called on this response.");
        }

        if (writer == null) {
            cout = new CustomServletOutputStream(getResponse().getOutputStream());
            writer = new PrintWriter(new OutputStreamWriter(cout, getResponse().getCharacterEncoding()), true);
        }
        return writer;

    }
    @Override
    public void flushBuffer() throws IOException {
        if (writer != null) {
            writer.flush();
        } else if (outputStream != null) {
            cout.flush();
        }
    }

    public byte[] getCopy() {
        if (cout != null) {
            return cout.getCopy();
        } else {
            return new byte[0];
        }
    }
}

 

package com.dls.config.redis;

import javax.servlet.ServletOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author wl
 */
public class CustomServletOutputStream extends ServletOutputStream {

    private OutputStream outputStream;
    private ByteArrayOutputStream byteArrayOutputStream;

    public CustomServletOutputStream(OutputStream outputStream){
        this.outputStream=outputStream;
        byteArrayOutputStream=new ByteArrayOutputStream();
    }


    @Override
    public void write(int b) throws IOException {
        outputStream.write(b);
        byteArrayOutputStream.write(b);
    }


    public byte[] getCopy(){
        return byteArrayOutputStream.toByteArray();
    }
}

 下面的过滤器需要注意 不能用@A...什么的 注入 需要手动注入  具体原因百度

package com.dls.config.redis;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author wl
 */
public class StateHtmlFilter implements Filter {

    RedisCacheManager redisCacheManager;

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        //注入redis
        ServletContext context = servletRequest.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
        redisCacheManager = ctx.getBean(RedisCacheManager.class);
        //转换对象
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        CustomHttpServletResponse response=new CustomHttpServletResponse((HttpServletResponse) servletResponse);
        //获取地址:demo/a?a=1
        String requestURI = request.getRequestURI();
        String queryString = request.getQueryString();
        String url = requestURI+"?"+queryString;
        if(redisCacheManager.get(url) == null ||redisCacheManager.get(url) == ""){
            chain.doFilter(request,response);
            byte[] data=response.getCopy();
            String htmStr=new String(data,servletResponse.getCharacterEncoding());
            //缓存起来s
            redisCacheManager.set(url,htmStr);
        }else {
            servletResponse.getWriter().write(redisCacheManager.get(url).toString());
        }
    }

    @Override
    public void destroy() {

    }
}

集成redis spring 配置过滤器就不贴了   这样就无敌了, 吊的一批 天下无敌

你可能感兴趣的:(java基础)