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)

大家都在看

  • LeetCode-1047. 删除字符串中的所有相邻重复项

    题目来源 题目详情 给出由小写字母组成的字符串 S, 重复项删除操作会选择两个相邻且相同的字母,并删除它们。 在 S 上反复执行重复项删除操作,直到无法继续删除。 在完成所有重复项…

    Linux 2023年6月7日
    0109
  • 我叫MongoDb,不懂我的看完我的故事您就入门啦!

    这是mongo基础篇,后续会连续更新4篇 大家好我叫MongoDb,自从07年10月10gen团队把我带到这个世界来,我已经13岁多啦,现在越来越多的小伙伴在拥抱我,我很高兴。我是…

    Linux 2023年6月14日
    0120
  • 每天一个 HTTP 状态码 102

    102 Processing 是用于 WebDAV 请求的一种状态码… 102 Processing 102 Processing 是用于 WebDAV协议 请求的状态…

    Linux 2023年6月7日
    0102
  • archLinux 配置用户

    archlinux 启动之后只有默认的root用户,首先介绍下系统启动到登录需要的步骤 1.系统通过systemd 以pid为1初始化系统,启动系统用户和系统必要的服务,(这一步目…

    Linux 2023年6月13日
    077
  • 解析库的使用

    使用 Xpath 使用 pyquery from pyquery import PyQuery as pq doc = pq(html) print(doc) PyQuery对象可…

    Linux 2023年6月7日
    087
  • 022.常见硬盘检测方式

    硬盘监测概述 硬盘异常损坏日常相对概率较高,同时不同的文件系统(xfs,reiserfs,ext3)其检测方式不同。建议使用dmesag查看有没有硬件I/O故障的日志,也可使用用f…

    Linux 2023年6月7日
    086
  • js中对象深度拷贝的方法(浅拷贝)

    JS中,一般的赋值传递的都是对象/数组的引用,并没有真正的深拷贝一个对象(浅拷贝),某些情况下需要用到深度拷贝,可以使用如下写法 let data = {username:&quo…

    Linux 2023年6月14日
    097
  • 大数据——配置并启动集群/开启历史服务器和日志聚集

    上篇文章通过克隆将集群搭建出来,这篇文章对Hadoop进行配置,修改配置文件,启动并测试集群。开去历史服务器以及日志聚集。 部署规划 hadoop102 hadoop103 had…

    Linux 2023年6月8日
    094
  • shell的入门

    命名只能使用英文字母,数字和下划线,首个字符不能以数字开头。 中间不能有空格,可以使用下划线 不能使用标点符号 不能使用bash里面关键字 使用变量加上$就可使用 只读变量 rea…

    Linux 2023年6月8日
    084
  • 关系型、非关系型数据库存储选型盘点大全

    工作中总是遇到数据存储相关的 Bug 工单,新需求开发设计中也多多少少会有数据模型设计和存储相关的问题。经过几次存储方案设计选型和讨论后发现需要有更全面的思考框架。 日常开发中常用…

    Linux 2023年6月8日
    0102
  • jarwarSpringBoot加载包内外资源的方式,告别FileNotFoundException吧

    工作中常常会用到文件加载,然后又经常忘记,印象不深,没有系统性研究过,从最初的war包项目到现在的springboot项目,从加载外部文件到加载自身jar包内文件,也发生了许多变化…

    Linux 2023年6月6日
    094
  • 项目相关环境docker版安装教程总结

    项目环境docker及docker-compose文档 1、Linux环境介绍 centos7.6 16G以上内存空间(至少8G) 2、静态IP设置 1、找到配置文件 cd /et…

    Linux 2023年6月7日
    075
  • go语言接口

    接口在底层的实现有两个部分:type 和 data。 在源码中,显式地将 nil 赋值给接口时,接口的 type 和 data 都将为 nil。此时,接口与 nil 值判断是相等的…

    Linux 2023年6月13日
    064
  • RPA人力资源简历筛选机器人

    简历自动筛选及分析机器人,支持前程无忧、猎聘 1、自动登录招聘网站 2、自动填充简历筛选条件 3、RPA依次读取所筛选的简历信息 4、自动将简历数据复制到本地文档中 5、完成简历信…

    Linux 2023年6月7日
    0100
  • Nginx配置TCP请求转发

    背景 有时候内网的服务器需要把服务提供给外网访问,但是这个内网的服务器没有公网ip,所以可以在一台有公网ip的nginx服务器配置TCP请求转发,把内网服务的端口映射出来到公网 N…

    Linux 2023年6月6日
    083
  • QLabel文字内容行间距

    故事背景:最近做项目升级,需要界面上展示升级更新内容,用QLabel展示,字符串是这样的”1、XXXXXXX;2、XXXXXXX;3、XXXXXXX”,一个…

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