Ehcache总结

一、Ehcache简介

Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。

 Ehcache在应用程序中的位置

 主要特性

1. 快速

2. 简单

3. 多种缓存策略

4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题

5. 缓存数据会在虚拟机重启的过程中写入磁盘

6. 可以通过RMI、可插入API等方式进行分布式缓存

7. 具有缓存和缓存管理器的侦听接口

8. 支持多缓存管理器实例,以及一个实例的多个缓存区域

9. 提供Hibernate的缓存实现

二、详细配置

1、pom文件中引入依赖坐标

<dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.6.6</version>
    </dependency>
    <dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>2.10.6</version>
    </dependency>

2、新建配置文件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">
   <!-- 
         磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
          path:指定在硬盘上存储对象的路径
   -->
  <!-- 磁盘缓存位置 -->
  <diskStore path="java.io.tmpdir/ehcache"/>
 <!-- 
        defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
   -->
  <!-- 默认缓存 -->
  <defaultCache
          maxEntriesLocalHeap="10000"
          eternal="false"
          timeToIdleSeconds="120"
          timeToLiveSeconds="120"
          maxEntriesLocalDisk="10000000"
          diskExpiryThreadIntervalSeconds="120"
          memoryStoreEvictionPolicy="LRU">
    <persistence strategy="localTempSwap"/>
  </defaultCache>
 <!-- 	
 				各个熟悉的含义
 				name 缓存空间名称(非缓存key)
        maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
				maxElementsOnDisk:硬盘中最大缓存对象数,若是0表示无穷大
				eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false  
				overflowToDisk:true表示当内存缓存的对象数目达到了maxElementsInMemory界限后,会把溢出的对象写到硬盘缓存中。注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。
				diskSpoolBufferSizeMB:磁盘缓存区大小,默认为30MB。每个Cache都应该有自己的一个缓存区。  
				diskPersistent:是否缓存虚拟机重启期数据,设置成true表示缓存虚拟机重启期数据  
				diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认为120秒 
				timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态 
				timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义  

    		memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。
     -->
  <!-- helloworld缓存 -->
  <cache name="HelloWorldCache"
         maxElementsInMemory="1000"
         eternal="false"
         timeToIdleSeconds="600"
         timeToLiveSeconds="600"
         overflowToDisk="false"
         memoryStoreEvictionPolicy="LRU"/>
</ehcache>

XML文件属性解释

ehcache.xml:里面的注释写的很清楚。

<diskStore>   : 当内存缓存中对象数量超过maxElementsInMemory时,将缓存对象写到磁盘缓存中(需对象实现序列化接口)  
<diskStore path="">     : 用来配置磁盘缓存使用的物理路径,Ehcache磁盘缓存使用的文件后缀名是*.data和*.index  
name : "缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里)  
maxElementsInMemory  : 缓存最大个数。

eternal="false"   : 对象是否永久有效,一但设置了,timeout将不起作用。 (必须设置)
maxEntriesLocalHeap="1000"  : 堆内存中最大缓存对象数,0没有限制(必须设置)
maxEntriesLocalDisk= "1000"   : 硬盘最大缓存个数。 
overflowToDisk="false"   : 当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)(内存不足时,是否启用磁盘缓存。)

diskSpoolBufferSizeMB  : 这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 
diskPersistent="false"  : 磁盘缓存在JVM重新启动时是否保持(默认为false)
timeToIdleSeconds="0"  : 导致元素过期的访问间隔(秒为单位),即当缓存闲置n秒后销毁。 当eternal为false时,这个属性才有效,0表示可以永远空闲,默认为0
timeToLiveSeconds="600"   : 元素在缓存里存在的时间(秒为单位),即当缓存存活n秒后销毁. 0 表示永远存在不过期

memoryStoreEvictionPolicy="LFU" : 当达到maxElementsInMemory时,如何强制进行驱逐默认使用"最近使用(LRU)"策略,其它还有先入先出FIFO,最少使用LFU,较少使用LRU
diskExpiryThreadIntervalSeconds :磁盘失效线程运行时间间隔,默认是120秒。
clearOnFlush   : 内存数量最大时是否清除

3、如果是SpringBoot,参考如下:

@Configuration
public class CacheConfig {

    //EhCacheManager
    @Bean(name = "ehCacheManager")
    public CacheManager cacheManager() {
        EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
        cacheManagerFactoryBean.setShared(true);
        cacheManagerFactoryBean.afterPropertiesSet();
        return cacheManagerFactoryBean.getObject();
    }
}

4、设置缓存

/**
 * 设置本地内存
 *
 * @param key
 * @return
 */
@Override
public void setLocalCache(String key, String value, Integer timeout) {
    Cache cache = ehCacheManager.getCache("HelloWorldCache");
    if (timeout == null) {
        timeout = localCacheTimeout;
    }
    if (value != null) {
        Element element = new Element(key, value);
        element.setTimeToLive(timeout);
        cache.put(element);
    }
}

5、获取本地缓存

public String getLocalCache(String key) {
    Cache cache = ehCacheManager.getCache("HelloWorldCache");
    if (cache == null) {
        return null;
    }
    Element cacheElement = cache.get(key);
    if (cacheElement == null) {
        return null;
    }
    return cacheElement.getObjectValue().toString();
}

注意:

6、ehcache 2.x不会主动清除过期缓存,使用定时任务清理缓存

/**
 * 定时清除过期本地缓存
 **/
@Slf4j
@Configuration
@EnableScheduling
public class ExpireLocalCacheScheduler {

   @Qualifier("ehCacheManager")
    private CacheManager ehCacheManager

    /**
     * 10分钟清除一次
     */
    @Scheduled(cron = "0 */10 * * * ?")
    private void expiredLocalCache() {
        try {
        Cache cache = ehCacheManager.getCache("HelloWorldCache");
        cache.getKeysWithExpiryCheck();
        cache.evictExpiredElements()
        } catch (Exception e) {
            log.error("evictExpiredLocalCache error!", e);
        }
    }

}

总结

ehcache是java内存结构的缓存,内部结构为java ConcurrentHashMap的重写,ehcache3内部结构为ConcurrentHashMap+WeekRefererce

使用ehcache主要适合两种业务

1、访问次数多Qps特别多,类似web放刷策略

2、需要缓存数据量小

缺点

如果不使用ehcache集群,在项目集群部署的情况下,敏感数据的更新可能不及时。

1、敏感数据不使用ehcache

2、如果使用建议ehcache的过期时间设置的非常小1-10秒

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值