EhCache HelloWorld实现
来源:互联网 发布:数据库工程师招聘 编辑:程序博客网 时间:2024/06/05 20:59
我们新建一个Maven项目,
pom.xml里引入 ehcache支持;
1
2
3
4
5
<
dependency
>
<
groupId
>net.sf.ehcache</
groupId
>
<
artifactId
>ehcache</
artifactId
>
<
version
>2.10.3</
version
>
</
dependency
>
ehcache.xml配置文件
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
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
ehcache
>
<!--
磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
path:指定在硬盘上存储对象的路径
-->
<
diskStore
path
=
"C:\ehcache"
/>
<!--
defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
eternal:代表对象是否永不过期
overflowToDisk:当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中
-->
<
defaultCache
maxElementsInMemory
=
"100"
eternal
=
"true"
overflowToDisk
=
"true"
/>
<
cache
name
=
"a"
maxElementsInMemory
=
"100"
eternal
=
"true"
overflowToDisk
=
"true"
/>
</
ehcache
>
package com.justin.hello;import net.sf.ehcache.Cache;import net.sf.ehcache.CacheManager;import net.sf.ehcache.Element;/** * ehcache缓存框架使用 * @author Administrator * */public class EhcacheTest {public static void main(String[] args) {//通过管理管理器读取缓存配置CacheManager manager=CacheManager.create("./src/main/resources/ehcache.xml");//获取指定的缓存Cache cache=manager.getCache("aaa");//实例化一个元素Element element=new Element("justin====","茅台镇酱香酒");//添加元素到缓存中cache.put(element);//根据key值获取缓存元素Element element2=cache.get("justin====");System.out.println(element2);System.out.println(element2.getObjectValue()); Element element3=cache.get("justin");System.out.println(element3);System.out.println(element3.getObjectValue());//刷新缓存cache.flush();//关闭缓存管理器manager.shutdown();}}
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache> <!-- 磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存 path:指定在硬盘上存储对象的路径 --> <diskStore path="C:\ehcache" /> <!-- defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理 maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象 eternal:代表对象是否永不过期 overflowToDisk:当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中 --> <defaultCache maxElementsInMemory="100" eternal="true" overflowToDisk="true"/> <!-- maxElementsInMemory设置成1,overflowToDisk设置成true,只要有一个缓存元素,就直接存到硬盘上去 eternal设置成true,代表对象永久有效 maxElementsOnDisk设置成0 表示硬盘中最大缓存对象数无限大 diskPersistent设置成true表示缓存虚拟机重启期数据 --> <cache name="aaa" maxElementsInMemory="1" eternal="true" overflowToDisk="true" maxElementsOnDisk="0" diskPersistent="true"/> </ehcache>
0 0
- EhCache HelloWorld实现
- ehcache 实现前端缓存
- SSH2+ehcache实现代码
- Ehcache分布式缓存实现
- ehcache 实现缓存共享
- ehcache实现缓存共享
- Spring MVC3 + Ehcache 缓存实现
- EhCache实现OSGi缓存bundle
- EHCache 实现通用类 CacheManager
- 使用EhCache实现页面缓存
- spring ehcache实现应用缓存
- SpringMvc Ehcache 实现缓存机制
- springMVC中ehcache的实现
- SpringMVC Ehcache的缓存实现
- ehcache通过注解实现缓存
- Spring MVC3 + Ehcache 缓存实现
- Spring Ehcache代码实现监控
- spring整合ehcache实现缓存
- CentOS 6.8下二进制安装MySQL 5.6
- spss modeler中生成新的变量练习题
- Java Colection集合l
- bzoj 3631 [JLOI2014]松鼠的新家
- 详解java内存泄露和如何避免内存泄漏
- EhCache HelloWorld实现
- L1-044. 稳赢
- 保持图片大小不变的情况下修改图片容量(目前全网已知最简方法)
- 动态规划练习题-14(怪盗基德的滑翔翼)
- 搭建Qt 开发环境:PyQt + qt + Sip
- L1-046. 整除光棍
- Java List集合
- BZOJ 1503: [NOI2004]郁闷的出纳员
- Codeforces gym 101343 J 状压dp