Ehcache对质页面、对象、数据缓存,同时支持集群/分布式缓存。整合Spring、Hibernate也非常的简单,spring对Ehcache的支持也非常好。EHCache支持内存和磁盘的缓存,支持LRU、LFU和FIFO多种淘汰算法,支持分布式的Cache,可以作为Hibernate的缓存插件。同时它也能提供基于Filter的Cache,该Filter可以缓存响应的内容并采用Gzip压缩提高响应速度。
下面配置EhCache的页面缓存
一、依赖jar包
ehcache-core-2.5.2.jar
ehcache-web-2.0.4.jar
其它spring等相关jar包
二、加入ehcache.xml配置
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="30" timeToLiveSeconds="30" overflowToDisk="false"/> <!-- 配置自定义缓存 maxElementsInMemory:缓存中允许创建的最大对象数 eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。 timeToIdleSeconds: 缓存数据的钝化时间,也就是在一个元素消亡之前, 两次访问时间的最大时间间隔值, 这只能在元素不是永久驻留时有效, 如果该值是 0 就意味着元素可以停顿无穷长的时间。 timeToLiveSeconds: 缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值, 这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。 overflowToDisk:内存不足时,是否启用磁盘缓存。 memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。 --> <cache name="SimplePageCachingFilter" maxElementsInMemory="10000" eternal="false" overflowToDisk="false" timeToIdleSeconds="3" timeToLiveSeconds="6" memoryStoreEvictionPolicy="LFU" /> </ehcache>
三、重新实现SimplePageCachingFilter类,SimplePageCachingFilter类也可以不重现实现,也能达到缓存效果,这里根据实现需求,进行重新实现
package com.cache; import java.util.Enumeration; import javax.servlet.FilterChain; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.ehcache.CacheException; import net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter; import org.apache.cxf.common.util.StringUtils; /** * EhCache 页面缓存过滤器 * */ public class PageCachingFilter extends SimplePageCachingFilter { private final static String FILTER_URL_PATTERNS = "patterns"; private static String[] cacheURLs; private void init() throws CacheException { String patterns = filterConfig.getInitParameter(FILTER_URL_PATTERNS); cacheURLs = StringUtils.split(patterns, ","); } @Override protected void doFilter(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) { try { if (cacheURLs == null) { init(); } String url = request.getRequestURI(); boolean flag = false; if (cacheURLs != null && cacheURLs.length > 0) { for (String cacheURL : cacheURLs) { if (url.contains(cacheURL.trim())) { flag = true; break; } } } if (flag) {// 包含我们指定要缓存的url 就缓存该页面,否则执行正常的页面转向 String query = request.getQueryString(); if (query != null) { query = "?" + query; url = url + query; } System.out.println("当前请求被缓存:" + url); super.doFilter(request, response, chain); } else { chain.doFilter(request, response); } } catch (Exception e) { e.printStackTrace(); } } @SuppressWarnings("rawtypes") private boolean headerContains(final HttpServletRequest request, final String header, final String value) { logRequestHeaders(request); final Enumeration accepted = request.getHeaders(header); while (accepted.hasMoreElements()) { final String headerValue = (String) accepted.nextElement(); if (headerValue.indexOf(value) != -1) { return true; } } return false; } /** 兼容ie6/7 gzip压缩*/ @Override 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; } }
四、在web.xml加入配置
<!-- EhCache页面缓存 --> <filter> <filter-name>PageCachingFilter</filter-name> <filter-class>com.cache.PageCachingFilter</filter-class> <init-param> <param-name>patterns</param-name> <!-- 配置你需要缓存的url --> <param-value>/pageCache.jsp</param-value> </init-param> </filter> <filter-mapping> <filter-name>PageCachingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
五、编辑测试页面pageCache.jsp
<%@page import="java.text.SimpleDateFormat"%> <%@page import="java.util.Date"%> <%@ 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>测试EhCache页面缓存</title> </head> <body> 测试EhCache页面缓存 <% SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); out.print(format.format(new Date())); System.out.println(System.currentTimeMillis()); %> </body> </html>
相关推荐
Ehcache 整合Spring 使用页面、对象缓存
NULL 博文链接:https://bijian1013.iteye.com/blog/2263270
以java配置类方式 整合EhCache来实现页面整体缓存及页面局部缓存。详情见博客http://blog.csdn.net/poorCoder_/article/details/56483954
配置ehcache缓存,存储内存的设置,与spring 的整合等
缓存可以提高查询数据性能, 对同一批数据进行多次查询时, 第一次查询走数据库, 查询数据后,将数据保存在内存中,第二次以后查询 可以直接从内存获取数据,而不需要 和数据库进行交互。
1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...
在一些要求高一致性(任何数据变化都能及时的被查询到)的系统和应用中,就不能再使用EhCache来解决了,这个时候使用集中式缓存是个不错的选择,因此本文将介绍如何在Spring Boot的缓存支持中使用Redis进行数据缓存...
Spring4.1+ehcache2.10.2实现缓存功能!
SpringBoo2.x整合Ehcache3.x作为缓存底层存储。
spring整合了ehcache,实现了缓存功能。附带读取properties文件信息功能。
当我们不指定具体其他第三方实现的时候,Spring Boot的Cache模块会...而实际生产使用的时候,因为我们可能需要更多其他特性,往往就会采用其他缓存框架,所以接下来我们会分几篇分别介绍几个常用优秀缓存的整合与使用。
Spring Boot中使用EhCache实现缓存支持,介绍整合方法和配置
代码内含BoneCp连接池和注解缓存。没有提供Spring的jar包。只要加入Spring的包就可以使用。或者只需要BoneCP和注解缓存的可以直接到lib里拿jar包。
本文先通过Ehcache独立应用的范例来介绍它的基本使用方法,然后再介绍与Spring整合的方法。
使用springboot 集成ehcache,里面附带数据库建库脚本,controller,service以及对ehcache集成,操作的详细样例,使用spring注解形式。
这是一个spring整合ehcache的完整用例,导入包后直接可以运行。ehcache.jar要导入1.2以上的,否则会报错。
3.Ehcache方法缓存及页面缓存。见applicationContext-cache.xml及web.xml的pageEhCacheFilter 4.MyBatis+Maven代码生成工具。见bin目录 5.作为项目或者技术研究的基础架构。必要的jar包依赖都已配置好,基本都采用较...
自己项目的开发包集合,其中包括:缓存处理ehcache相关jar,spring MVC4.0 jar,ehcache依赖jar,以及其他jar(图像处理thumbnailator-0.4.2),包虽然不是很新但可用。实际使用时找包较为麻烦,现在整理出来,希望...