您的位置:首页 > 其它

EhCache缓存使用教程

2021-04-28 20:46 169 查看

文章发表在我的博客上:https://blog.ysboke.cn/archives/124.html

什么是ehcache

纯Java的进程内缓存,直接在JVM虚拟机中缓存,速度非常快。缓存有两级,内存存储完了自动存到磁盘上。

数据可持久化在磁盘上,vm(虚拟机)重启后数据恢复

和redis比怎么样

redis都听过,内存数据库,ehcache的缓存是jvm级别的,速度超过redis。redis通过socket访问缓存数据,效率没ehcache高。

对于分布式和集群,redis有成熟的方案,而ehcache在分布式和集群情况下的缓存恢复、数据缓存的支持不是很好。

如果是大型系统,存在缓存共享、分布式部署、缓存内容很大的,建议用redis。

二者可以共同使用。

使用方法

1、在pom.xml里添加依赖:

<!--引入Mybatis的ehCache的适配-->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.3</version>
</dependency>

2、在src/main/java/resources下创建固定名称的ehcache.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false"
monitoring="autodetect"
dynamicConfig="true">
<!-- 当内存中不够存储时,存储到指定数据在磁盘中的存储位置。 -->
<diskStore path="cache" />

<!-- 默认缓存,当借助CacheManager.add("demoCache")创建Cache时,EhCache便会采用<defalutCache/>指定的的管理策略 -->
<defaultCache
maxElementsInMemory="3000"
eternal="false"
copyOnRead="true"
copyOnWrite="true"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="true"
diskPersistent="true">
<copyStrategy class="com.moti.utils.MyCopyStrategy"/>
</defaultCache>

<!-- 自定义的缓存策略 -->
<cache name="HelloWorldCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="5"
timeToLiveSeconds="5"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>

</ehcache>

必须的属性:

  • maxElementsInmemory—— 在内存中缓存的element的最大数目
  • maxElementsOnDisk——在磁盘上缓存的elements的最大数目,0表示不限制
  • eternal——设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断
  • overFlowToDisk——设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上

可选的属性:

  • timeToIdleSeconds——可闲置时间。当缓存在EhCache中的数据前后两次访问的时间超过timeToIdleSeconds的属性取值时,这些数据便会删除,默认值是0,也就是可闲置时间无穷大
  • timeToLiveSeconds——缓存element的有效生命期,默认是0。也就是element存活时间无穷大
  • diskSpoolBufferSizeMB——设置DiskStore(磁盘缓存)的缓存区大小.默认是30MB.每个Cache都应该有自己的一个缓冲区.
  • diskPersistent——在VM重启的时候是否启用磁盘保存EhCache中的数据,默认是false。
  • diskExpiryThreadIntervalSeconds——磁盘缓存的清理线程运行间隔,默认是120秒。每个120s,相应的线程会进行一次EhCache中数据的清理工作 memoryStoreEvictionPolicy——当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略。默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出)
  • clearOnFlush——内存达到最大时是否清除

3、使用示例

public class Test1 {

@Test
public void test1() {
// 1. 创建缓存管理器
CacheManager cacheManager = CacheManager.create("./src/main/resources/ehcache.xml");

// 2. 获取缓存对象
Cache cache = cacheManager.getCache("HelloWorldCache");

// 3. 创建元素
Element element = new Element("key1", "value1");

// 4. 将元素添加到缓存
cache.put(element);

// 5. 获取缓存
Element value = cache.get("key1");
System.out.println(value);
System.out.println(value.getObjectValue());

// 6. 删除元素
cache.remove("key1");

Person p1 = new Person("小明",18,"杭州");
Element pelement = new Element("xm", p1);
cache.put(pelement);
Element pelement2 = cache.get("xm");
System.out.println(pelement2.getObjectValue());

System.out.println(cache.getSize());

// 7. 刷新缓存
cache.flush();

// 8. 关闭缓存管理器
cacheManager.shutdown();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: