页面缓存:SimplePageCachingFilter

web.xml

  1. <!-- 页面缓存配置,配合ehcache.xml中name为“SimplePageCachingFilter”(默认值)的缓存配置使用 -->
  2. <filter>
  3. <filter-name>PageEhCacheFilter</filter-name>
  4. <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>PageEhCacheFilter</filter-name>
  8. <!-- 指定需要缓存页面的url -->
  9. <url-pattern>/userHome</url-pattern>
  10. </filter-mapping>

ehcache.xml(放在classpath路径下)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ehcache>
  3. <!--简单页面缓存 -->
  4. <cache name="SimplePageCachingFilter"
  5. maxElementsInMemory="10"
  6. maxElementsOnDisk="10"
  7. overflowToDisk="true"
  8. diskSpoolBufferSizeMB="20"
  9. timeToIdleSeconds="7"
  10. timeToLiveSeconds="10"
  11. memoryStoreEvictionPolicy="LFU"
  12. />
  13. </ehcache>

SimpleCachingHeadersPageCachingFilter与使用SimplePageCachingFilter几乎是一样的。所不同的是前者在构建返回信息的时候会设置“Last-Modified、Expires、Cache-Control、ETag”这四个缓存头信息,如果在设置之前这些信息已经存在的话,那么它们将会被忽略,而直接使用SimpleCachingHeadersPageCachingFilter重新生成过的。



局部页面缓存:SimplePageFragmentCachingFilter

(页面局部片段,例如<jsp:include page="">包含的部分必要配置<dispatcher>INCLUDE</dispatcher>

web.xml

  1. <!-- 页面缓存配置,配合ehcache.xml中name为“SimplePageFragmentCachingFilter”(默认值)的缓存配置使用 -->
  2. <filter>
  3. <filter-name>PageEhCacheFilter</filter-name>
  4. <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageFragmentCachingFilter</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>PageEhCacheFilter</filter-name>
  8. <url-pattern>/head.jsp</url-pattern>
  9. <!-- 指定需要过滤的请求的发送类型 -->
  10. <dispatcher>INCLUDE</dispatcher>
  11. </filter-mapping>
ehcache.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ehcache>
  3. <!--简单页面缓存 -->
  4. <cache name="SimplePageFragmentCachingFilter"
  5. maxElementsInMemory="10"
  6. maxElementsOnDisk="10"
  7. overflowToDisk="true"
  8. diskSpoolBufferSizeMB="20"
  9. timeToIdleSeconds="7"
  10. timeToLiveSeconds="10"
  11. memoryStoreEvictionPolicy="LFU"
  12. />
  13. </ehcache>

我们include的jsp页面在filter中要指定<dispatcher>INCLUDE</dispatcher>,如果没有指定任何< dispatcher >元素,默认值是REQUEST就不会拦截了;如果指定了INCLUDE,则过滤器只过滤通过include过来的请求,而不会过滤直接从客户端过来的请求。

 
  1. <dispatcher> 这个元素有四个可能的值:即REQUEST,FORWARD,INCLUDE和ERROR,可以在一个<filter-mapping>元素中加入任意数目的<dispatcher>,使得filter将会作用于直接从客户端过来的request,通过forward过来的request,通过include过来的request和通过<error-page>过来的request。如果没有指定任何<dispatcher>元素,默认值是REQUEST。
 
需要jar包:ehcache-web-2.0.4.jar

对象缓存:
方法一:net.sf.ehcache.CacheManager
  1. public void test() {
  2. net.sf.ehcache.CacheManager cacheManager = CacheManager.create();
  3. net.sf.ehcache.Cache cache = cacheManager.getCache("SimplePageCachingFilter");
  4. cache.put(new Element("key", "value")); //设置缓存
  5. cache.get("key").getValue(); //查询缓存
  6. }

方法二:org.springframework.cache.CacheManager(spring集成EhCache)


spring的缓存配置bean

  1. <!-- spring的缓存配置 -->
  2. <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  3. <property name="configLocation" value="classpath:ehcache.xml" />
  4. </bean>
  5. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
  6. <property name="cacheManager" ref="ehCacheManagerFactory"/>
  7. </bean>

使用缓存

  1. @Autowired
  2. private org.springframework.cache.CacheManager cacheManager;
  3. //private net.sf.ehcache.CacheManager //这里也可以用原生的CacheManager,注入时强制转换类型
  4. public void test() {
  5. org.springframework.cache.Cache cache = cacheManager.getCache("SimplePageCachingFilter");
  6. cache.put("key", "value"); //设置缓存
  7. cache.get("key").get(); //查询缓存
  8. //输出 value
  9. }

需要jar包:ehcache-core-2.4.3.jar


 

方法注解缓存


方法一:使用 org.springframework.cache.annotation.Cacheable注解缓存
 
spring的缓存配置bean
  1. <!-- spring的缓存配置 -->
  2. <cache:annotation-driven cache-manager="cacheManager" /> <!-- 注解缓存必须 -->
  3. <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  4. <property name="configLocation" value="classpath:ehcache.xml" />
  5. </bean>
  6. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
  7. <property name="cacheManager" ref="ehCacheManagerFactory"/>
  8. </bean>

使用注解缓存 Annotation

  1. import org.springframework.cache.annotation.Cacheable;
  2. @Cacheable(value = "SimplePageCachingFilter")
  3. //SimplePageCachingFilter为ehcache.xml上配的名字,不同的参数name使用的缓存不一样
  4. public String getData(String name) {
  5. SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
  6. System.out.println(name);
  7. return format.format(new Date());
  8. }

(更多spring缓存注解“CachePut更新缓存”、“CacheEvict清除缓存”的具体用法可自行百度)

 
方法二:使用ehcache-spring-annotations开启ehcache的注解功能
spring的缓存配置bean,注意下面cache-manager参数值与上面的区别
  1. 命名空间 xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
  2. xsi:schemaLocation= http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
  3. http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
  4.  
  5. <!-- spring的缓存配置 -->
  6. <ehcache:annotation-driven cache-manager="ehCacheManagerFactory" /> <!-- 注解缓存必须 -->
  7. <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  8. <property name="configLocation" value="classpath:ehcache.xml" />
  9. </bean>
使用注解缓存 Annotation
  1. import com.googlecode.ehcache.annotations.Cacheable;
  2. @Cacheable(cacheName = "SimplePageCachingFilter")
  3. //SimplePageCachingFilter为ehcache.xml上配的名字,不同的参数name使用的缓存不一样
  4. public String getData(String name) {
  5. SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
  6. System.out.println(name);
  7. return format.format(new Date());
  8. }

(更多注解“TriggersRemove清除缓存”的具体用法可自行百度)

 
需要jar包:ehcache-spring-annotations-1.2.0.jar、guava-r09.jar

EhCache缓存页面、局部页面和对象缓存的更多相关文章

  1. SharePoint 事件 7363:对象缓存:缓存使用的超级读者帐户没有足够的权限访问SharePoint数据库。

    转自MSND:http://technet.microsoft.com/zh-cn/library/ff758656(v=office.14) 对象缓存存储 Microsoft SharePoint ...

  2. SQLAlchemy 对象缓存和刷新

    SQLAlchemy 对象缓存和刷新 SQLAlchemy 带有对象缓存机制,在重复查询相同的对象时,直接先查询本地的缓存,而不需要从数据库加载数据. 在每个 model 对象的内部,SQLAlche ...

  3. ehcache 页面整体缓存和局部缓存

    页面缓存是否有必要?. 这样说吧,几乎所有的网站的首页都是访问率最高的,而首页上的数据来源又是非常广泛的,大多数来自不同的对象,而且有可能来自不同的db ,所以给首页做缓存是很必要的.那么主页的缓存策 ...

  4. ehcache实现页面整体缓存和页面局部缓存

    之前写过spring cache和ehcache的基本介绍和注解实现缓存管理,今天记录下web项目的页面缓存技术. 页面缓存是否有必要?. 这样说吧,几乎所有的网站的首页都是访问率最高的,而首页上的数 ...

  5. spring ehcache 页面、对象缓存

    一.Ehcache基本用法 CacheManager cacheManager = CacheManager.create(); // 或者 cacheManager = CacheManager.g ...

  6. Ehcache 整合Spring 使用页面、对象缓存

    Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一 般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布 ...

  7. (转)Ehcache 整合Spring 使用页面、对象缓存

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

  8. 在Spring中使用cache(EhCache的对象缓存和页面缓存)

    Spring框架从version3.1开始支持cache,并在version4.1版本中对cache功能进行了增强. spring cache 的关键原理就是 spring AOP,通过 spring ...

  9. Ehcache 整合Spring 使用页面、对象缓存(转载)

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

随机推荐

  1. SpringBoot中logback.xml使用application.yml中属性

    教你如何使用 springProfile 与 springProperty 让你的logback.xml 配置显得更有逼格,当别人还在苦苦挣扎弄logback-{profile}.xml的时候 你一个 ...

  2. 洛谷 P1463 [SDOI2005]反素数ant

    P1463 [SDOI2005]反素数ant 题目描述 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i< ...

  3. QT官方下载地址

    http://download.qt.io/official_releases watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/ ...

  4. 创建MAVEN项目报错

    创建MAVEN项目pom.xml报错 Failure to transfer org.apache.maven:maven-archiver:jar:2.4.2 from http://repo.ma ...

  5. ROS探索总结(十九)——怎样配置机器人的导航功能

    1.概述 ROS的二维导航功能包.简单来说.就是依据输入的里程计等传感器的信息流和机器人的全局位置,通过导航算法,计算得出安全可靠的机器人速度控制指令. 可是,怎样在特定的机器人上实现导航功能包的功能 ...

  6. notifyDataSetChanged()刷新ListView(使用JSONArray绑定的Adapter)

    1.fragment代码: package com.ts.fragment; import java.util.ArrayList; import java.util.HashMap; import ...

  7. Tomcat容器 web.xml具体解释

    <init-param> <param-name>debug</param-name> <param-value>0</param-value&g ...

  8. Linux - 配置php-fpm 以及 配置nginx支持php

    配置php-fpm [root@localhost php7]# which php-fpm /usr/local/php7/sbin/php-fpm [root@localhost php7]# p ...

  9. Java-Spring MVC:JAVA之常用的一些Spring MVC的路由写法以及参数传递方式

    ylbtech-Java-Spring MVC:JAVA之常用的一些Spring MVC的路由写法以及参数传递方式 1.返回顶部 1. 常用的一些Spring MVC的路由写法以及参数传递方式. 这是 ...

  10. discuz “欣” “衡” 用户不能注册 bug修改

    discuz “欣” “衡” 用户不能注册 原因是 discuz 有这样一段代码 function check_username($username) { $guestexp = '\xA1\xA1| ...