读书人

j2ee页面静态化提案encache web cache

发布时间: 2012-08-30 09:55:54 作者: rapoo

j2ee页面静态化方案encache web cache框架源码分析2

encache的web cache代码分析

?

1.抽象filter分析

? ??public abstract class Filter implements javax.servlet.Filter {

?

对于checkNoReentry的实现,很简单

? /**

?

? 当然还有两个抽象方法

? ?? /**

? ?这个类对于缓存的内容的头信息 Last-Modified?Expires?Cache-Control?ETag删除,替换成当前encache里面的系统信息,输出的时候也输出encache处理过的头信息,这样我们就不管浏览器的缓存处理了,只能等待encache的缓存过期。

?

4.PageFragmentCachingFilter的简单处理

? ?? protected PageInfo buildPage(final HttpServletRequest request, final HttpServletResponse response,

    private static final long serialVersionUID = 1L;    private static final Logger LOG = LoggerFactory.getLogger(PageInfo.class);    private static final int FOUR_KB = 4196;    private static final int GZIP_MAGIC_NUMBER_BYTE_1 = 31;    private static final int GZIP_MAGIC_NUMBER_BYTE_2 = -117;    private static final long ONE_YEAR_IN_SECONDS = 60 * 60 * 24 * 365;   //存储的信息包括状态码,contentType,cookie,body,是否gzip过,过期时间,headers等信息    public PageInfo(final int statusCode, final String contentType,                     final Collection cookies,                     final byte[] body, boolean storeGzipped, long timeToLiveSeconds,                    final Collection<Header<? extends Serializable>> headers) throws AlreadyGzippedException {        //Note that the ordering is switched with headers at the end to deal with the erasure issues with Java generics causing        //a conflict with the deprecated PageInfo header                 this.init(statusCode, contentType, headers, cookies, body, storeGzipped, timeToLiveSeconds);    }   /**     * @param ungzipped the bytes to be gzipped     * @return gzipped bytes     *///提供的gzip处理方法    private byte[] gzip(byte[] ungzipped) throws IOException, AlreadyGzippedException {        if (isGzipped(ungzipped)) {            throw new AlreadyGzippedException("The byte[] is already gzipped. It should not be gzipped again.");        }        final ByteArrayOutputStream bytes = new ByteArrayOutputStream();        final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bytes);        gzipOutputStream.write(ungzipped);        gzipOutputStream.close();        return bytes.toByteArray();    }    private byte[] ungzip(final byte[] gzipped) throws IOException {        final GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(gzipped));        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(gzipped.length);        final byte[] buffer = new byte[FOUR_KB];        int bytesRead = 0;        while (bytesRead != -1) {            bytesRead = inputStream.read(buffer, 0, FOUR_KB);            if (bytesRead != -1) {                byteArrayOutputStream.write(buffer, 0, bytesRead);            }        }        byte[] ungzipped = byteArrayOutputStream.toByteArray();        inputStream.close();        byteArrayOutputStream.close();        return ungzipped;    }}

?

6.GzipFilter的filter,这个filter对于浏览器支持gzip的则进行gzip压缩之后输出

读书人网 >Web前端

热点推荐