EhCache 缓存框架
Ehcache 介绍
EhCache 从 Hibernate 发展而来,是一个纯Java的进程内缓存框架,具有快速、精干等特点。Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。
主要特性:
1.快速,简单
2.多种缓存策略
3.缓存数据有两级:内存和磁盘,因此无需担心容量问题
4.缓存数据会在虚拟机重启的过程中写入磁盘
5.可以通过RMI、可插入API等方式进行分布式缓存
6.具有缓存和缓存管理器的侦听接口
7.支持多缓存管理器实例,以及一个实例的多个缓存区域
8.提供Hibernate的缓存实现
在 pom.xml 文件中添加 Ehcache 依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId>
</dependency>
配置文件:
在配置文件 application.yaml 中配置 ehcache 的相关参数,具体内容如下:
spring:application:name: spring-boot-bulking-ehcachecache:type: ehcacheehcache:config: classpath:/ehcache.xml
添加 Ehcache 配置:
在src/main/resources 目录下,创建配置文件ehcache.xml ,内容如下:
<ehcache name="test"><diskStore path="java.io.tmpdir"/><defaultCachemaxEntriesLocalHeap="1000"eternal="false"timeToIdleSeconds="300"timeToLiveSeconds="600"overflowToDisk="true"memoryStoreEvictionPolicy="LRU"></defaultCache><cache name="userCache"maxEntriesLocalHeap="200"eternal="false"timeToIdleSeconds="300"timeToLiveSeconds="600"overflowToDisk="true"></cache>
</ehcache>
参数含义:
diskStore:磁盘缓存位置
name:缓存名称
maxElementsInMemory:内存中最大缓存对象数
maxElementsOnDisk:硬盘中最大缓存对象数,若是0表示无穷大
eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false
timeToIdleSeconds:设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态
timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
diskSpoolBufferSizeMB:磁盘缓存区大小,默认为30MB。每个Cache都应该有自己的一个缓存区。
diskPersistent:是否缓存虚拟机重启期数据。
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
开启缓存:
入口启动类添加注解 @EnableCaching
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class})
@EnableCaching // 开启缓存,Spring Boot 会自动配置缓存的 CacheManager
public class StartApplication {public static void main(String[] args) {SpringApplication.run(StartApplication.class, args);}
}
缓存业务使用:
@Component
@CacheConfig(cacheNames = "userCache")
public class UserService {@Cacheable(key = "#id")public User getUserById(Long id) {System.out.println("缓存中无值");User user = User.builder().id(id).userName("雪糕(" + id + ")").age(18).address("杭州").build();return user;}@CachePut(key = "#user.id")public User updateUser(User user) {user.setUserName("雪糕(new name)");return user;}@CacheEvict(key = "#id")public void deleteById(Long id) {System.out.println("db 删除数据,id=" + id);}
}
@CacheConfig 作用于类上,用来描述该类中所有方法使用的缓存名称。当然也可以不使用该注解,直接在具体方法上的缓存注解里配置名称
@Cacheable 用于查询方法上,表示将一个方法的返回值缓存起来。默认情况下,缓存的 key 就是方法的参数,缓存的 value 就是方法的返回值
@CachePut 更新操作,当数据库中的数据更新后,缓存中的数据也要跟着更新,使用该注解,可以将方法的返回值自动更新到已经存在的 key 上
@CacheEvict 删除操作,当数据库中的数据删除后,相关的缓存数据也要自动清除。
除了采用 @Cacheable 、@CachePut 等方法注解解耦式操作缓存外,我们也可以使用 CacheManager显示方式手动来操作缓存。
CacheManager
Spring定义了CacheManager和Cache接口统一不同的缓存技术。其中CacheManager是Spring提供的各种缓存技术的抽象接口,而Cache接口包含缓存的读、写、删等各种操作。
针对不同的缓存技术,需要实现不同的CacheManager,Spring预先定义了主流缓存框架的cacheManger实现类
本文我们使用 EhCache 缓存,代码示例如下:
@Component
public class UserCacheManager {@Resourceprivate CacheManager cacheManager;public User getUserById(Long id) {Cache cache = cacheManager.getCache("userCache");User user = cache.get(id, User.class);if (user == null) {System.out.println("缓存中无值");user = User.builder().id(id).userName("雪糕(" + id + ")").age(18).address("杭州").build();cache.put(id, user);}return user;}public User updateUser(User user) {user.setUserName("雪糕(new name)");Cache cache = cacheManager.getCache("userCache");cache.put(user.getId(), user);return user;}public void deleteById(Long id) {Cache cache = cacheManager.getCache("userCache");cache.evict(id);System.out.println("db 删除数据,id=" + id);}
}
演示代码地址
https://github.com/aalansehaiyang/spring-boot-bulking 模块:spring-boot-bulking-ehcache
EhCache 缓存框架相关推荐
- [原创]mybatis中整合ehcache缓存框架的使用
mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...
- SpringBoot集成Cache缓存(Ehcache缓存框架,注解方式)
1.说明 Spring定义了CacheManager和Cache接口, 用来统一不同的缓存技术, 例如JCache,EhCache,Hazelcast,Guava,Redis等. 本文通过Spring ...
- 一文玩转 EhCache 缓存框架!
Ehcache 介绍 EhCache 从 Hibernate 发展而来,是一个纯Java的进程内缓存框架,具有快速.精干等特点.Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存, ...
- EHcache缓存框架详解
EhCache是一个纯Java的进程内缓存框架,具有快速.精干等特点,也是Hibernate中默认的CacheProvider. 归纳一下它大概具有一下几个特点: 1. 快速. 2. 简单. 3. 多 ...
- Spring MVC学习总结(7)——Spring MVC整合Ehcache缓存框架
Ehcache算是当前比较流行的缓存框架,使用缓存可以极大的缓解服务器和数据库的压力,提高访问效率,提高服务器的并发能力.接下来我们看怎么把缓存使用起来. SpringMVC集成Ehcache所需的j ...
- mybatis学习教程中级(十)mybatis和ehcache缓存框架整合(重点)
1.前言 前面讲解了mybatis的一级.二级缓存.一级然并卵(spring整合后),二级还是有用的.我们现在来看看用ehcache来维护管理二级缓存.不要问我为什么,因为都这么用!!!java是框架 ...
- springboot2.3.4集成EhCache缓存框架完整代码
代码部分 pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...
- cachehelper java,初识EHCache缓存框架(2.x版本)
参考文档: 关于EHCache3.x版本的使用详见此篇博客: Maven依赖: net.sf.ehcache ehcache 2.10.3 1.独立使用 在class根目录下加入配置文件ehcache ...
- Spring MVC整合Ehcache缓存框架
https://blog.csdn.net/u012562943/article/details/52289433 转载于:https://www.cnblogs.com/pjlhf/p/881874 ...
最新文章
- 阿里原来是这么干的!Spring Boot 五种热部署方式
- 【Zookeeper进阶】大白话解释Zookeeper的选举机制
- CBV 验证装饰器的使用
- linux内核和w,Linux内核中Makefile、Kconfig和.config的关系
- Oracle集合运算
- 数字图像处理-- 图像的统计方法
- 记一次 React 组件无法更新状态值的问题分析与解决
- python线程监控_Python监控php-fpm进程
- 【转】处理百万级以上的数据提高查询速度的方法
- 访问服务器ipmi(DHCP)接口
- 固态硬盘在IDE、AHCI模式下的速度对比
- 解决微信公众平台图片不可引用
- Oracle ADF 12.2.1 使用报告
- 混战多年,K12在线教育的故事讲到哪了?
- 《GTD I》读书笔记
- EZo UIBuilder中嵌入微博分享按钮【实例】
- 谈谈Linux中Redis的薪火相传与反客为主及如何实现
- 在浏览器端浏览EPUB
- 明明有内存报错CUDA out of memory
- Termux安装Linux及图形化教程
热门文章
- Duplicate entry for key 'PRIMARY'
- 诺基亚S40机型新手美化攻略
- 使用扩散模型从文本生成图像
- 【Machine Learning】【Andrew Ng】- notes(Week 2: Computing Parameters Analytically)
- 网络安全设备配置交换机
- 《人类简史》一、智人觉醒——席卷全球的洪水
- 存储测试报告模板1.0
- OV4689 和IMX179的dts中节点的区别,以及dts节点的注释说明,基于rv1108平台
- 237_自定义抽签器三
- 基于spring boot的邮件微服务消息中间件设计与实现 毕业论文+系统功能图v1.0.vsdx+项目源码