Ehcache 特性

EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。


Ehcache缓存的特点:
1. 快速.
2. 简单.
3. 多种缓存策略
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
5. 缓存数据会在虚拟机重启的过程中写入磁盘
6. 可以通过RMI、可插入API等方式进行分布式缓存
7. 具有缓存和缓存管理器的侦听接口
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
9. 提供Hibernate的缓存实现


Ehcache缓存的使用(1) – 安装ehcache
Ehcache 的特点,是一个纯Java ,过程中(也可以理解成插入式)缓存实现,单独安装Ehcache ,需把ehcache-X.X.jar 和相关类库方到classpath中。如项目已安装了Hibernate ,则不需要做什么,直接可以使用Ehcache 。


Ehcache缓存的使用(2) - 生成CacheManager
使用CacheManager 创建并管理Cache
1.创建CacheManager有4种方式:
A:使用默认配置文件创建
Java代码
1. CacheManager manager = CacheManager.create();


B:使用指定配置文件创建
Java代码
1. CacheManager manager = CacheManager.create("src/config/ehcache.xml");


C:从classpath中找寻配置文件并创建
Java代码
1. URL url = getClass().getResource("/anothername.xml");
2. CacheManager manager = CacheManager.create(url);


D:通过输入流创建
Java代码
1. InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
2. try {
3. manager = CacheManager.create(fis);
4. } finally {
5. fis.close();
6. }


Ehcache缓存的使用(3) – 解读Ehcache配置文件ehcache.xml
重要的参数
<diskStore path="D:/work2/renhewww/cache"/>
<cache name=" sampleCache1"
maxElementsInMemory="1"
maxElementsOnDisk="10000"
eternal="false"
overflowToDisk="true"
diskSpoolBufferSizeMB="20"
diskPersistent="true"
timeToIdleSeconds="43200"
timeToLiveSeconds="86400"
memoryStoreEvictionPolicy="LFU"
/>


Ehcache缓存的使用(4) – 创建Cache
通过CacheManager创建Cache
Cache cache = manager.getCache("sampleCache1");


Ehcache缓存的使用(5) – 利用cache存取数据
存储数据
Element element = new Element("key1", "value1");
cache.put(new Element(element);
获取数据
Element element = cache.get("key1");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值