EhCache是一个纯Java的进程内缓存框架,具有快速、精干等特点,也是Hibernate中默认的CacheProvider。
归纳一下它大概具有一下几个特点:
1. 快速.
2. 简单.
3. 多种缓存策略
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
5. 缓存数据会在虚拟机重启的过程中写入磁盘
6. 可以通过RMI、可插入API等方式进行分布式缓存
7. 具有缓存和缓存管理器的侦听接口
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
9. 提供Hibernate的缓存实现
那么我们在开发中到底如何运用EhCache框架呢?
获取Ehcache相关jar包及帮助文档。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
/** * maxElementsInMemory:缓存中允许创建的最大对象数
* eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
* timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前,两次访问时间的最大时间间隔值, 这只能在元素不是永久驻留时有效,
* 如果该值是 0 就意味着元素可以停顿无穷长的时间。
* timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,
* 如果该值是0就意味着元素可以停顿无穷长的时间。 overflowToDisk:内存不足时,是否启用磁盘缓存。
* memoryStoreEvictionPolicy:缓存满了之后的淘汰算法
*
* @param args
*/
public static void main(String[] args) {
// 创建一个缓存管理器对象
CacheManager cacheManager = CacheManager.create();
// 命名缓存管理器
cacheManager.setName( "testCacheManager" );
// 创建一个指定缓存名称的缓存对象
Cache cache = new Cache( "testCache" , 4 , false , false , 1 , 1 );
// cache.setDisabled(true);
// 将缓存对象添加至缓存管理器
cacheManager.addCache(cache);
// cacheManager.shutdown();
System.out.println( "判断缓存管理器中是否存在指定的缓存对象:"
+ cacheManager.cacheExists( "testCache" ));
DiskStorePathManager disStoreManager = cacheManager
.getDiskStorePathManager();
System.out.println( "获取当前配置文件硬盘路径:"
+ disStoreManager.getFile( "testCache.xml" ));
Map<String, Object> map = new HashMap<String, Object>();
map.put( "name" , "tom" );
map.put( "sex" , "男" );
map.put( "age" , 1 );
// 注意:如果当前缓存对象设置了内存中最大缓存keyValue对象的话,如果超出时,则后面的覆盖前面的keyValue对象
cache.put( new Element( "cache1" , map));
cache.put( new Element( "cache2" , map));
cache.put( new Element( "cache3" , map));
Element element = new Element( "cache4" , map);
element.setTimeToLive( 1 );
cache.put(element);
String[] cacheNames = cacheManager.getCacheNames();
for ( int i = 0 ; i < cacheNames.length; i++) {
System.out.println( "缓存" + i + ":" + cacheNames[i]);
}
// System.out.println("当前活动的缓存配置文件内容:\n"
// + cacheManager.getActiveConfigurationText());
System.out.println( "缓存管理器对象是否命名:" + cacheManager.isNamed());
Cache testCahe = cacheManager.getCache( "testCache" );
System.out.println( "缓存的状态:" + testCahe.getStatus());
System.out.println( "缓存对象平均获取时间:" + testCahe.getAverageGetTime());
System.out.println( "获取缓存对象占用内存空间大小:" + testCahe.getMemoryStoreSize());
System.out.println( "获取缓存对象大小:" + testCahe.getSize());
System.out.println( "缓存是否关闭:" + testCahe.isDisabled());
System.out.println( "判断某一个缓存key是否存在在缓存中"
+ testCahe.isKeyInCache( "cache3" ));
System.out.println( "判断某一个缓存值是否缓存在对象中:" + testCahe.isValueInCache(map));
// 验证缓存对象是否禁用
if (!testCahe.isDisabled()) {
System.out.println( "判断缓存中某个对象是否过期:"
+ testCahe.isExpired(testCahe.get( "cache3" )));
} else {
System.out.println(testCahe.getName() + "缓存已关闭" );
}
System.out.println( "判断某一个key是否缓存在内存中:"
+ testCahe.isElementInMemory( "cache1" ));
System.out.println( "判断某一个key是否缓存在磁盘中:"
+ testCahe.isElementOnDisk( "cache1" ));
System.out.println( "\n" );
List cacheKey = cache.getKeys();
for ( int i = 0 ; i < cacheKey.size(); i++) {
Element cacheElement = testCahe.get(cacheKey.get(i));
System.out.println( "Key:" + cacheKey.get(i) + ",value:"
+ cacheElement.getObjectValue());
}
}
|
相关推荐
NULL 博文链接:https://x125858805.iteye.com/blog/2161734
主要为大家详细介绍了Spring+EHcache缓存实例,EhCache是一个纯Java的进程内缓存框架,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Ehcache配置详解及CacheManager使用,设置时间和调用工具类实现
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认CacheProvider。这篇文章主要介绍了Spring Boot整合EhCache的步骤详解,需要的朋友可以参考下
缓存ehcache开源框架,2. 简单. 3. 多种缓存策略 4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题 5. 缓存数据会在虚拟机重启的过程中写入磁盘 6. 可以通过RMI、可插入API等方式进行分布式缓存 7. 具有缓存和...
主要介绍了详解SpringBoot缓存的实例代码(EhCache 2.x 篇),具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Spring Boot JPA 二级缓存(ehcache) Spring Boot Hikari 连接池详解 Spring Boot Caffeine 本地缓存 Spring Boot Redis 缓存 Spring Boot Redis 分布式锁 Spring Boot Event 事件发布与监听 Spring Boot 集成 ...
(10) spring-support.jar 这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。 (11) spring-web.jar 这个jar文件包含Web应用...
* 使用其他 json 转换框架 * 全局异常捕捉 * JPA 连接数据库 * 配置 JPA * 整合 JPA 保存数据 * 使用 JdbcTemplate 保存数据 * 常用配置 * 静态资源处理 * 实现任务调度 * 普通类调用 Bean * 使用模板引擎 * 集成 ...
1.40 Spring Boot集成EHCache实现缓存机制 1.41 Spring Boot分布式Session状态保存Redis 1.42 Spring Boot Shiro权限管理 1.43 Spring Boot Shiro权限管理 1.44 Spring Boot Shiro权限管理 1.45 Spring Boot Shiro...
chapter6-1-1:使用Spring StateMachine框架实现状态机 Spring Boot Actuator监控端点小结 在传统Spring应用中使用spring-boot-actuator模块提供监控端点 Spring Boot应用的后台运行配置 Spring Boot自定义Banner ...
- chapter6-1-1:[使用Spring StateMachine框架实现状态机](http://blog.didispace.com/spring-statemachine/) - [Spring Boot Actuator监控端点小结](http://blog.didispace.com/spring-boot-actuator-1/) - [在...
这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。 (11) spring-web.jar 这个jar文件包含Web应用开发时,用到Spring框架时...