web.xmlweb
<!-- 页面缓存配置,配合ehcache.xml中name为“SimplePageCachingFilter”(默认值)的缓存配置使用 --> <filter> <filter-name>PageEhCacheFilter</filter-name> <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter</filter-class> </filter> <filter-mapping> <filter-name>PageEhCacheFilter</filter-name> <!-- 指定须要缓存页面的url --> <url-pattern>/userHome</url-pattern> </filter-mapping>
ehcache.xml(放在classpath路径下)
<?xml version="1.0" encoding="UTF-8"?> <ehcache> <!--简单页面缓存 --> <cache name="SimplePageCachingFilter" maxElementsInMemory="10" maxElementsOnDisk="10" overflowToDisk="true" diskSpoolBufferSizeMB="20" timeToIdleSeconds="7" timeToLiveSeconds="10" memoryStoreEvictionPolicy="LFU" /> </ehcache>
SimpleCachingHeadersPageCachingFilter与使用SimplePageCachingFilter几乎是同样的。所不一样的是前者在构建返回信息的时候会设置“Last-Modified、Expires、Cache-Control、ETag”这四个缓存头信息,若是在设置以前这些信息已经存在的话,那么它们将会被忽略,而直接使用SimpleCachingHeadersPageCachingFilter从新生成过的。
(页面局部片断,例如<jsp:include page="">包含的部分必要配置<
dispatcher
>INCLUDE</
dispatcher
>
)spring
<!-- 页面缓存配置,配合ehcache.xml中name为“SimplePageFragmentCachingFilter”(默认值)的缓存配置使用 --> <filter> <filter-name>PageEhCacheFilter</filter-name> <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageFragmentCachingFilter</filter-class> </filter> <filter-mapping> <filter-name>PageEhCacheFilter</filter-name> <url-pattern>/head.jsp</url-pattern> <!-- 指定须要过滤的请求的发送类型 --> <dispatcher>INCLUDE</dispatcher> </filter-mapping>
<?xml version="1.0" encoding="UTF-8"?> <ehcache> <!--简单页面缓存 --> <cache name="SimplePageFragmentCachingFilter" maxElementsInMemory="10" maxElementsOnDisk="10" overflowToDisk="true" diskSpoolBufferSizeMB="20" timeToIdleSeconds="7" timeToLiveSeconds="10" memoryStoreEvictionPolicy="LFU" /> </ehcache>
<dispatcher> 这个元素有四个可能的值:即REQUEST,FORWARD,INCLUDE和ERROR,能够在一个<filter-mapping>元素中加入任意数目的<dispatcher>,使得filter将会做用于直接从客户端过来的request,经过forward过来的request,经过include过来的request和经过<error-page>过来的request。若是没有指定任何<dispatcher>元素,默认值是REQUEST。
public void test() { net.sf.ehcache.CacheManager cacheManager = CacheManager.create(); net.sf.ehcache.Cache cache = cacheManager.getCache("SimplePageCachingFilter"); cache.put(new Element("key", "value")); //设置缓存 cache.get("key").getValue(); //查询缓存 }
方法二:org.springframework.cache.CacheManager(spring集成EhCache)缓存
<!-- spring的缓存配置 --> <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehCacheManagerFactory"/> </bean>
使用缓存
@Autowired private org.springframework.cache.CacheManager cacheManager; //private net.sf.ehcache.CacheManager //这里也能够用原生的CacheManager,注入时强制转换类型 public void test() { org.springframework.cache.Cache cache = cacheManager.getCache("SimplePageCachingFilter"); cache.put("key", "value"); //设置缓存 cache.get("key").get(); //查询缓存 //输出 value }
须要jar包:ehcache-core-2.4.3.jarapp
方法注解缓存jsp
<!-- spring的缓存配置 --> <cache:annotation-driven cache-manager="cacheManager" /> <!-- 注解缓存必须 --> <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehCacheManagerFactory"/> </bean>
使用注解缓存 Annotationsvn
import org.springframework.cache.annotation.Cacheable; @Cacheable(value = "SimplePageCachingFilter") //SimplePageCachingFilter为ehcache.xml上配的名字,不一样的参数name使用的缓存不同 public String getData(String name) { SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); System.out.println(name); return format.format(new Date()); }
(更多spring缓存注解“CachePut更新缓存”、“CacheEvict清除缓存”的具体用法可自行百度)google
命名空间 xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xsi:schemaLocation= http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd <!-- spring的缓存配置 --> <ehcache:annotation-driven cache-manager="ehCacheManagerFactory" /> <!-- 注解缓存必须 --> <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean>
import com.googlecode.ehcache.annotations.Cacheable; @Cacheable(cacheName = "SimplePageCachingFilter") //SimplePageCachingFilter为ehcache.xml上配的名字,不一样的参数name使用的缓存不同 public String getData(String name) { SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); System.out.println(name); return format.format(new Date()); }
(更多注解“TriggersRemove清除缓存”的具体用法可自行百度)url