Ehcache 整合Spring 使用页面、对象缓存 逃离我推掉我的手 2022-09-24 05:20 47阅读 0赞 # Ehcache使用 # 一、准备工作 如果你的系统中已经成功加入Spring、Hibernate;那么你就可以进入下面Ehcache的准备工作。 1、 下载jar包 Ehcache 对象、数据缓存:[http://ehcache.org/downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gz&bucket=tcdistributions&file=ehcache-core-2.5.2-distribution.tar.gz][http_ehcache.org_downloads_destination_name_ehcache-core-2.5.2-distribution.tar.gz_bucket_tcdistributions_file_ehcache-core-2.5.2-distribution.tar.gz] Web页面缓存:[http://ehcache.org/downloads/destination?name=ehcache-web-2.0.4-distribution.tar.gz&bucket=tcdistributions&file=ehcache-web-2.0.4-distribution.tar.gz][http_ehcache.org_downloads_destination_name_ehcache-web-2.0.4-distribution.tar.gz_bucket_tcdistributions_file_ehcache-web-2.0.4-distribution.tar.gz] 2、 需要添加如下jar包到lib目录下 ehcache-core-2.5.2.jar ehcache-web-2.0.4.jar 主要针对页面缓存 Maven配置如下: <!-- EhCache-Core --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.11</version> </dependency> <!-- EhCache-Web --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-web</artifactId> <version>2.0.4</version> </dependency> 3、 当前工程的src目录中加入配置文件 ehcache.xml ehcache.xsd 这些配置文件在ehcache-core这个jar包中可以找到 二、Ehcache基本用法 CacheManager cacheManager = CacheManager.create(); // 或者 cacheManager = CacheManager.getInstance(); // 或者 cacheManager = CacheManager.create(“/config/ehcache.xml”); // 或者 cacheManager = CacheManager.create(“[http://localhost:8080/test/ehcache.xml][http_localhost_8080_test_ehcache.xml]“); cacheManager = CacheManager.newInstance(“/config/ehcache.xml”); // ……. // 获取ehcache配置文件中的一个cache Cache sample = cacheManager.getCache(“sample”); // 获取页面缓存 BlockingCache cache = new BlockingCache(cacheManager.getEhcache(“SimplePageCachingFilter”)); // 添加数据到缓存中 Element element = new Element(“key”, “val”); sample.put(element); // 获取缓存中的对象,注意添加到cache中对象要序列化 实现Serializable接口 Element result = sample.get(“key”); // 删除缓存 sample.remove(“key”); sample.removeAll(); // 获取缓存管理器中的缓存配置名称 for (String cacheName : cacheManager.getCacheNames()) \{ System.out.println(cacheName); \} // 获取所有的缓存对象 for (Object key : cache.getKeys()) \{ System.out.println(key); \} // 得到缓存中的对象数 cache.getSize(); // 得到缓存对象占用内存的大小 cache.getMemoryStoreSize(); // 得到缓存读取的命中次数 cache.getStatistics().getCacheHits(); // 得到缓存读取的错失次数 cache.getStatistics().getCacheMisses(); 三、页面缓存 页面缓存主要用Filter过滤器对请求的url进行过滤,如果该url在缓存中出现。那么页面数据就从缓存对象中获取,并以gzip压缩后返回。其速度是没有压缩缓存时速度的3-5倍,效率相当之高!其中页面缓存的过滤器有CachingFilter,一般要扩展filter或是自定义Filter都继承该CachingFilter。 CachingFilter功能可以对HTTP响应的内容进行缓存。这种方式缓存数据的粒度比较粗,例如缓存整张页面。它的优点是使用简单、效率高,缺点是不够灵活,可重用程度不高。 EHCache使用SimplePageCachingFilter类实现Filter缓存。该类继承自CachingFilter,有默认产生cache key的calculateKey()方法,该方法使用HTTP请求的URI和查询条件来组成key。也可以自己实现一个Filter,同样继承CachingFilter类,然后覆写calculateKey()方法,生成自定义的key。 CachingFilter输出的数据会根据浏览器发送的Accept-Encoding头信息进行Gzip压缩。 在使用Gzip压缩时,需注意两个问题: 1. Filter在进行Gzip压缩时,采用系统默认编码,对于使用GBK编码的中文网页来说,需要将操作系统的语言设置为:zh\_CN.GBK,否则会出现乱码的问题。 2. 默认情况下CachingFilter会根据浏览器发送的请求头部所包含的Accept-Encoding参数值来判断是否进行Gzip压缩。虽然IE6/7浏览器是支持Gzip压缩的,但是在发送请求的时候却不带该参数。为了对IE6/7也能进行Gzip压缩,可以通过继承CachingFilter,实现自己的Filter,然后在具体的实现中覆写方法acceptsGzipEncoding。 具体实现参考: protected boolean acceptsGzipEncoding(HttpServletRequest request) \{ boolean ie6 = headerContains(request, “User-Agent”, “MSIE 6.0”); boolean ie7 = headerContains(request, “User-Agent”, “MSIE 7.0”); return acceptsEncoding(request, “gzip”) || ie6 || ie7; \} 在ehcache.xml中加入如下配置 [http_ehcache.org_downloads_destination_name_ehcache-core-2.5.2-distribution.tar.gz_bucket_tcdistributions_file_ehcache-core-2.5.2-distribution.tar.gz]: http://ehcache.org/downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gz&bucket=tcdistributions&file=ehcache-core-2.5.2-distribution.tar.gz [http_ehcache.org_downloads_destination_name_ehcache-web-2.0.4-distribution.tar.gz_bucket_tcdistributions_file_ehcache-web-2.0.4-distribution.tar.gz]: http://ehcache.org/downloads/destination?name=ehcache-web-2.0.4-distribution.tar.gz&bucket=tcdistributions&file=ehcache-web-2.0.4-distribution.tar.gz [http_localhost_8080_test_ehcache.xml]: http://localhost:8080/test/ehcache.xml
还没有评论,来说两句吧...