EhCache缓存页面、局部页面和对象缓存

页面缓存: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/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • ajax的发展

    ajax(Asynchronous Javascript and XML)异步javascrip和XMl. ajax只是一种web交互方法。在客户端(浏览器)和服务区段之间传输少量…

    Linux 2023年6月13日
    089
  • 【转】我是一个CPU:这个世界慢!死!了!

    简介 经常听到有人说磁盘很慢、网络很卡,这都是站在人类的感知维度去表述的,比如拷贝一个文件到硬盘需要几分钟到几十分钟,够我去吃个饭啦;而从网络下载一部电影,有时候需要几个小时,我都…

    Linux 2023年6月16日
    0152
  • n的阶乘前100项。Table of n! for n = 1..100

    n的阶乘前100项 {1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,871782…

    Linux 2023年6月6日
    087
  • 个人学习-Linux文件系统架构

    个人学习-Linux文件系统架构 1. 参考文章 [1] https://blog.csdn.net/Holy_666/article/details/86532671 [2]CS…

    Linux 2023年6月6日
    0107
  • 计算机硬件的读写速度差异

    现代计算机系统 存储器 寄存器 CPU时钟周期 高速缓存 主存 固态硬盘 机械硬盘 压榨CPU性能带来的问题 有序性问题 可见性问题 原子性问题 作者:小牛呼噜噜 | https:…

    Linux 2023年6月6日
    0133
  • Ubuntu 忘记登录密码

    重启Ubuntu,随即长按Shift(单系统)进入Grub菜单 选择Ubuntu高级选项 选择recovery mode进入Recovery Menu界面,选择Drop to ro…

    Linux 2023年6月16日
    0204
  • 进程间通信(IPC)

    进程间通信(Interprocess Communication,IPC)是指两个或者多个进程之间进行数据交换的过程 进程拥有独立的内存空间 命令行参数(向子进程传递和exec系列…

    Linux 2023年6月6日
    0123
  • Java50个关键字之abstract

    abstract abstract 可以出现的位置: 修饰方法 修饰类 修饰类 一个类被 abstract修饰,那么该类就叫做 &#x62BD;&#x8C61;&a…

    Linux 2023年6月7日
    096
  • [转]Redis cluster failover

    今天测试了redis cluster failover 功能,在切换过程中很快,但在failover时有force 与takeover 之分 [RHZYTEST_10:REDIS:…

    Linux 2023年5月28日
    0100
  • 设计模式-单例模式

    目的:为了保证一个类在程序中只有一个实例,并且能被全局访问 场景:全局线程池 要点: 通过 Test::Instance()获取类指针 class Test { public: s…

    Linux 2023年6月8日
    097
  • Bash编程中对字符串的操作

    Bash的字符串操作 String="Hello World" #获取字符串长度,获取字符长度的变量调用应该使用${},这里大括号是必须的 #例1-1 echo…

    Linux 2023年6月13日
    0116
  • eclipse中如何打jar包并使用

    https://blog.csdn.net/qq_44985985/article/details/103992138 Original: https://www.cnblogs….

    Linux 2023年6月13日
    0129
  • 小试牛刀:Linux中部署RabbitMQ

    一、下载地址 本人采用的是 RabbitMQ 3.8.20+ Erlang 23.3.4.16 1、Erlang下载:https://github.com/erlang/otp/r…

    Linux 2023年6月14日
    098
  • PHP array_reduce()

    array_reduce array_reduce() 将回调函数 callback 迭代地作用到 array 数组中的每一个单元中,从而将数组简化为单一的值。 示例一: 示例二:…

    Linux 2023年6月7日
    0119
  • MIT6.828——Lab2内存管理准备知识

    保护模式内存管理机制 MIT6.828——Lab1 PartA MIT6.828——Lab1 PartB 分段机制的问题 ​ 分段的主要问题,出现在内存不足或者内存碎片过多的情况下…

    Linux 2023年5月27日
    0125
  • IDM 下载器的安装和使用

    下载安装 为大家提供免注册版本:IDM下载器 – Dominic 的蓝奏云分享 下载解压之后,双击第一个文件进行安装 之后一路选择”Next”即…

    Linux 2023年6月8日
    0153
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球