页面缓存:SimplePageCachingFilter
web.xml
<filter> <filter-name>PageEhCacheFilterfilter-name> <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilterfilter-class> filter> <filter-mapping> <filter-name>PageEhCacheFilterfilter-name> <url-pattern>/userHomeurl-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重新生成过的。
局部页面缓存: SimplePageFragmentCachingFilter
(页面局部片段,例如
web.xml
<filter> <filter-name>PageEhCacheFilterfilter-name> <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageFragmentCachingFilterfilter-class> filter> <filter-mapping> <filter-name>PageEhCacheFilterfilter-name> <url-pattern>/head.jspurl-pattern> <dispatcher>INCLUDEdispatcher> filter-mapping>
ehcache.xml
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>
我们include的jsp页面在filter中要指定
,如果没有指定任何< dispatcher >元素,默认值是REQUEST就不会拦截了;如果指定了INCLUDE,则过滤器只过滤通过include过来的请求,而不会过滤直接从客户端过来的请求。
这个元素有四个可能的值:即REQUEST,FORWARD,INCLUDE和ERROR,可以在一个元素中加入任意数目的,使得filter将会作用于直接从客户端过来的request,通过forward过来的request,通过include过来的request和通过过来的request。如果没有指定任何元素,默认值是REQUEST。
需要jar包:ehcache-web-2.0.4.jar
对象缓存:
方法一:net.sf.ehcache.CacheManager
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
<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.jar
方法注解缓存
方法一:使用 org.springframework.cache.annotation.Cacheable注解缓存
spring的缓存配置bean
<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>
使用注解缓存 Annotation
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清除缓存”的具体用法可自行百度)
方法二:使用ehcache-spring-annotations开启ehcache的注解功能
spring的缓存配置bean,注意下面cache-manager参数值与上面的区别
命名空间 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 <ehcache:annotation-driven cache-manager="ehCacheManagerFactory" /> <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> bean>
使用注解缓存 Annotation
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清除缓存”的具体用法可自行百度)
需要jar包:ehcache-spring-annotations-1.2.0.jar、guava-r09.jar
Original: https://www.cnblogs.com/zhangzongjian/p/5519743.html
Author: zhangzongjian
Title: EhCache缓存页面、局部页面和对象缓存
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/606562/
转载文章受原作者版权保护。转载请注明原作者出处!