温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

web项目如何使用ehcache-web进行页面缓存或者文件压缩

发布时间:2021-10-21 09:53:35 来源:亿速云 阅读:88 作者:小新 栏目:开发技术

这篇文章主要介绍了web项目如何使用ehcache-web进行页面缓存或者文件压缩,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

一:  小试 EhCache web 用来缓存JSP页面

0) 涉及到的jar包

     ehcache-core-2.5.2.jar,

     ehcache-web-2.0.4.jar

1) web.xml 

    这里使用了个简单的过滤器来拦截所有的jsp请求 

  <web-app 

  <filter>  

         <filter-name>PageCacheFilter</filter-name><filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter </filter-class>  

  </filter>  

  <filter-mapping>

    <filter-name>PageCacheFilter</filter-name><url-pattern>/*.jsp</url-pattern>  

  </filter-mapping>

</web-app>

2) ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../main/config/ehcache.xsd">  

<diskStore path="java.io.tmpdir/ehcache" />  

    <cache name="SimplePageCachingFilter"  

           maxElementsInMemory="10000"  

           maxElementsOnDisk="1000"  

           eternal="false"  

           overflowToDisk="true"  

           timeToIdleSeconds="5"  

           timeToLiveSeconds="10"  

           memoryStoreEvictionPolicy="LFU"  

            />  

    <defaultCache  

            maxElementsInMemory="10000"  

            eternal="false"  

            timeToIdleSeconds="120"  

            timeToLiveSeconds="120"  

            overflowToDisk="true"  

            maxElementsOnDisk="10000000"  

            diskPersistent="false"  

            diskExpiryThreadIntervalSeconds="120"  

            memoryStoreEvictionPolicy="LRU"  

            />  

</ehcache>  

3)一个简单的index.jsp页面来打印出日志

 <%@page import="java.sql.ResultSet"%>  

<%@page import="com.db.DB"%>  

<%@ page language="java" contentType="text/html; charset=utf-8"  

    pageEncoding="utf-8"%>  

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">  

<title>测试</title>  

</head>  

<body>  

<%  

    System.out.println(System.currentTimeMillis());  

%> 

</body>  

</html>  

4)测试方法 

  1.启动了项目去访问index.jsp页面 

  2.刷新index.jsp页面,看后台是否打印出日志

  3.其实使用页面缓存已对文件进行了gzip压缩了。无需在使用下面的GzipFilter进行过滤处理。

二、使用gzip优化web应用(filter实现)

    测试时没有发现有用,似乎tomcat已经启用了gzip功能 

   gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将页面文件和其他资源,

   返回到客户端,客户端加载后渲染呈现,这种情况文件一般都比较大,如果开启Gzip ,那么服务器端响应后,会将页面,

   JS,CSS等文本文件或者其他文件通过高压缩算法将其压缩,然后传输到客户端,由客户端的浏览器负责解压缩与呈现。

   通常能节省40%以上的流量   

 1) web.xml中添加过滤器

    <filter>

       <filter-name>gzipFilter</filter-name>  

       <filter-class>

           net.sf.ehcache.constructs.web.filter.GzipFilter

       </filter-class>

    </filter>

    <filter-mapping>

        <filter-name>gzipFilter</filter-name>

        <url-pattern>*.js</url-pattern>

        <url-pattern>*.tpl</url-pattern>

        <url-pattern>*.prd</url-pattern>

        <url-pattern>*.ftl</url-pattern>

        <url-pattern>*.html</url-pattern>

        <url-pattern>*.css</url-pattern>

    </filter-mapping>

感谢你能够认真阅读完这篇文章,希望小编分享的“web项目如何使用ehcache-web进行页面缓存或者文件压缩”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI