引言:
我们为什么要讲ehcache呢?ehcache是什么呢?应用在什么场景中?
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点。
为了提高系统性能,减少网络开销,如:访问redis、访问数据库的压力,使用本地缓存是非常好的手段之一。本地缓也是一种十分典型的降级方法
那为何不使用redis缓存,为何还要引出一个新的缓存呢?我们看下区别
- ehcache直接在jvm虚拟机中缓存,速度快,效率高。
- 但是缓存共享麻烦,集群分布式应用不方便,如果是单个应用或者对缓存访问要求很高的应用,用ehcache。
- redis是通过socket访问到缓存服务,效率比ecache低,比数据库要快很多。
- 处理集群和分布式缓存方便,有成熟的方案。如果是大型系统,存在缓存共享、分布式部署、缓存内容很大的,建议用redis。
开发环境:
技术框架:springboot、ehcache
项目仓库:https://gitee.com/jikeh/JiKeHCN-RELEASE.git
项目名:spring-boot-ehcache
案例背景:0、亿级流量高并发广告系统多级缓存架构
0、声明式缓存
Spring 定义 CacheManager 和 Cache 接口用来统一不同的缓存技术。例如 JCache、 EhCache、 Hazelcast、 Guava、 Redis 等。在使用 Spring 集成 Cache 的时候,我们需要注册实现的 CacheManager 的 Bean。
@Configuration @EnableCaching public class CacheConfiguration { ? @Bean public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() { EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean(); ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml")); ehCacheManagerFactoryBean.setShared(true); return ehCacheManagerFactoryBean; } @Bean public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean ehCacheManagerFactoryBean) { return new EhCacheCacheManager(ehCacheManagerFactoryBean.getObject()); } }
1、Spring Boot默认集成CacheManager
Spring Boot 为我们自动配置了多个 CacheManager 的实现。
注意:确认你的springboot版本,>= 1.3的才会默认集成,否则需要通过声明式缓存来实现
我们这里使用的springboot版本为1.5,使用默认集成的方式来实现ehcache缓存,我们这里就无需添加上面的代码了
Spring Boot 为我们自动配置了 JcacheCacheConfiguration、 EhCacheCacheConfiguration、HazelcastCacheConfiguration、GuavaCacheConfiguration、RedisCacheConfiguration、SimpleCacheConfiguration 等。
在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManager)
2、Springboot整合EhCache的详细步骤
1)添加pom文件maven依赖
<!--开启 cache 缓存 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> ? <!-- ehcache缓存 --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.5</version> </dependency>
2)配置ehcache.xml
下载ehcache.xsd文件,配置本地目录
下载地址:http://www.ehcache.org/ehcache.xsd
将其放到resource目录下
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <!-- 默认缓存策略 --> <defaultCache maxEntriesLocalHeap="10000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="120" memoryStoreEvictionPolicy="LRU"> </defaultCache> ? <!-- 测试 --> <cache name="users" maxEntriesLocalHeap="10000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="120" memoryStoreEvictionPolicy="LRU"> </cache> </ehcache>
参数详解:http://www.ehcache.org/ehcache.xml
defaultCache:默认缓存策略,如果你指定的缓存策略没有找到,那么就用这个默认的缓存策略。
name:缓存名称,必须唯一。 Sets the name of the cache. This is used to identify the cache. It must be unique.
maxEntriesLocalHeap:内存中可以缓存多少个缓存条目,在实践中,你是需要自己去计算的,比如你计算你要缓存的对象是什么?有多大?最多可以缓存多少MB,或者多少个G的数据?除以每个对象的大小,计算出最多可以放多少个对象。maxElementsInMemory在2.5以后被废弃了
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,是否溢出到磁盘
diskPersistent:是否启用磁盘持久化的机制,在jvm崩溃的时候和重启之间,不用
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)
3)开启缓存支持
@EnableCaching
4)项目中使用
常用注解介绍:
- @CacheConfig 类级别来统一设置
String[] cacheNames() default {}; ? //缓存的名称,在 spring 配置文件中定义 @CacheConfig(cacheNames = "users")
- @cache这个相当于save()操作
String[] value() default {}; ? //缓存的名称,在 spring 配置文件中定义,与cacheNames两者,必须指定至少一个 @Cacheable(value = "saveUser") ? String key() default ""; //缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 例如: @Cacheable(value = "saveUser",key = "#userId") ? String condition() default ""; //缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存 例如: @Cacheable(value = "saveUser",condition = "#userId.length()>0")
- @cachePut相当于update()操作,使用@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,而@cache则是先看下有没已经缓存了,然后再选择是否执行方法。
- @CacheEvict相当于delete()操作。用来清除缓存用的。
//前面与@cache的含义相同 ? boolean allEntries() default false; //是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存 例如: @CachEvict(value=”testcache”,allEntries=true) ? boolean beforeInvocation() default false; 是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行完的时候(包括抛出异常的时候)就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存 例如: @CachEvict(value=”testcache”,beforeInvocation=true)
本文暂时没有评论,来添加一个吧(●'◡'●)