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秒

### Ehcache 的基本原理 Ehcache 是一个纯 Java 实现的高性能缓存框架,广泛应用于分布式系统中以提高数据访问速度并减少数据库负载。其核心结构由多层组成,其中最顶层是 `CacheManager`,它充当操作 Ehcache 的入口点[^1]。 #### CacheManager 和 Cache 结构 - **CacheManager**: 可以通过静态方法 `CacheManager.getInstance()` 获取单例模式下的实例,也可以通过构造函数创建新的实例。每个 `CacheManager` 负责管理多个 `Cache` 对象。 - **Cache**: 每个 `Cache` 类似于哈希表,内部维护着一组键值对形式的数据条目(即 Element)。这些条目存储的是实际的缓存内容。 - **Element**: 表示具体的缓存单元,包含了键和对应的值。 以下是初始化 `CacheManager` 并定义缓存的一个简单例子: ```java import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; public class EhcacheExample { public static void main(String[] args) { // 创建一个新的 CacheManager CacheManager cacheManager = new CacheManager(); // 添加一个名为 "exampleCache" 的缓存 Cache exampleCache = new Cache("exampleCache", 100, false, true, 5, 10); cacheManager.addCache(exampleCache); System.out.println("Cache added successfully."); } } ``` --- ### 配置文件详解 Ehcache 支持多种配置方式,常见的有 XML 文件配置以及基于 Spring 注解的方式。XML 配置文件通常命名为 `ehcache.xml`,位于项目的资源目录下。以下是一个典型的配置片段及其参数解释[^4]: ```xml <ehcache> <!-- 定义磁盘存储位置 --> <diskStore path="java.io.tmpdir"/> <!-- 缓存名称为 userCache --> <cache name="userCache" maxEntriesLocalHeap="1000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"> </cache> </ehcache> ``` | 参数 | 描述 | |-----------------------|----------------------------------------------------------------------| | `maxEntriesLocalHeap` | 设置内存中最多能保存多少条记录 | | `eternal` | 是否永久有效,如果设置为 `true` 则忽略过期时间 | | `timeToIdleSeconds` | 记录在最后一次访问后的存活时间 | | `timeToLiveSeconds` | 记录自创建以来的最大存活时间 | --- ### Spring 整合与注解支持 Spring 提供了一组强大的缓存注解来简化开发流程,常用的注解包括 [@Cacheable](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/cache/annotation/Cacheable.html),[@CachePut](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/cache/annotation/CachePut.html),[@CacheEvict](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/cache/annotation/CacheEvict.html)[^3]。 下面展示如何将 Ehcache 集成到 Spring 中,并使用注解实现功能: #### 配置 Bean ```xml <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehCacheManager"/> </bean> <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml"/> </bean> ``` #### 使用注解 ```java @Service public class UserService { @Cacheable(value = "users", key = "#id") public User getUserById(Long id) { // 查询逻辑... return userRepository.findById(id).orElse(null); } @CachePut(value = "users", key = "#user.id") public User updateUser(User user) { // 更新逻辑... return userRepository.save(user); } @CacheEvict(value = "users", key = "#id") public void deleteUser(Long id) { // 删除逻辑... userRepository.deleteById(id); } } ``` --- ### Servlet 场景中的注意事项 当 Ehcache 应用于 Web 开发场景时,可能会遇到一些问题,比如更新静态资源(JS/CSS/JSP)后未及时反映最新版本的内容[^5]。这通常是由于浏览器端或服务端缓存机制引起的。解决办法之一是在部署新版本前手动清理缓存,或者调整缓存策略以避免不必要的持久化。 --- ### 总结 Ehcache 不仅提供了灵活高效的缓存解决方案,还能够轻松集成至主流框架如 Spring 中。无论是简单的本地缓存还是复杂的分布式环境,都可以借助它的强大功能提升性能表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值