# 047. 完成 spring boot 整合 ehcache 的搭建以支持服务本地堆缓存

因为之前跟大家提过,三级缓存,多级缓存,服务本地堆缓存 + redis 分布式缓存 + nginx 本地缓存组成的

每一层缓存在高并发的场景下,都有其特殊的用途,需要综合利用多级的缓存,才能支撑住高并发场景下各种各样的特殊情况

服务本地堆缓存的作用是预防 redis 层的彻底崩溃,作为缓存的最后一道防线,避免数据库直接裸奔

服务本地堆缓存,我们用什么来做缓存?除了最简单的使用 map 来手动管理缓存之外,还有很多流行的框架可选,Guava cache、ehcache 等,在 spring 中,有一个 Cache 抽象,可以用来支持整合多个缓存框架。我们这里使用 ehcache

spring boot + ehcache 整合起来,演示一下是怎么使用的

添加依赖,由于本人是第一次使用 ehcache,所以跟着老师的版本走

compile 'net.sf.ehcache:ehcache:2.8.3'
// spring cache 缓存抽象支持
compile 'org.springframework:spring-context-support'
1
2
3

# ehcache 配置

# spring boo 1.x

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

/**
 *
 * @author : zhuqiang
 * @date : 2019/4/7 10:03
 */
@Configurable
@EnableCaching
public class CacheConfiguration {
    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
        EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
        cacheManagerFactoryBean.setShared(true);
        return cacheManagerFactoryBean;
    }

    @Bean
    public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean ehCacheManagerFactoryBean) {
        return new EhCacheCacheManager(ehCacheManagerFactoryBean.getObject());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

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">

  	<!-- diskStore:ehcache其实是支持内存+磁盘+堆外内存,几个层级的缓存 -->
  	<!-- 在这里设置一下,但是一般不用的 -->
    <diskStore path="java.io.tmpdir/Tmp_EhCache" />

    <!-- defaultCache,是默认的缓存策略 -->
    <!-- 如果你指定的缓存策略没有找到,那么就用这个默认的缓存策略 -->
    <!-- external:如果设置为true的话,那么timeout就没有效果,缓存就会一直存在,一般默认就是false -->
    <!-- maxElementsInMemory:内存中可以缓存多少个缓存条目,在实践中,你是需要自己去计算的,比如你计算你要缓存的对象是什么?有多大?最多可以缓存多少MB,或者多少个G的数据?除以每个对象的大小,计算出最多可以放多少个对象 -->
    <!-- overflowToDisk:如果内存不够的时候,是否溢出到磁盘 -->
    <!-- diskPersistent:是否启用磁盘持久化的机制,在jvm崩溃的时候和重启之间,不用 -->
    <!-- timeToIdleSeconds:对象最大的闲置的时间,如果超出闲置的时间,可能就会过期,我们这里就不用了,缓存最多闲置5分钟就被干掉了 -->
    <!-- timeToLiveSeconds:对象最多存活的时间,我们这里也不用,超过这个时间,缓存就过期,就没了 -->
    <!-- memoryStoreEvictionPolicy:当缓存数量达到了最大的指定条目数的时候,需要采用一定的算法,从缓存中清除一批数据,LRU,最近最少使用算法,最近一段时间内,最少使用的那些数据,就被干掉了 -->
    <defaultCache
        eternal="false"
        maxElementsInMemory="1000"
        overflowToDisk="false"
        diskPersistent="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="0"
        memoryStoreEvictionPolicy="LRU" />

 	<!-- 手动指定的缓存策略 -->
 	<!-- 比如你一个应用吧,可能要缓存很多种不同的数据,比如说商品信息,或者是其他的一些数据 -->
 	<!-- 对不同的数据,缓存策略可以在这里配置多种 -->
    <cache
        name="local"  
        eternal="false"
        maxElementsInMemory="1000"
        overflowToDisk="false"
        diskPersistent="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="0"
        memoryStoreEvictionPolicy="LRU" />

	<!-- ehcache这种东西,简单实用,是很快速的,1小时上手可以用在项目里了,没什么难度的 -->   
    <!-- ehcache这个技术,如果讲深了,里面的东西还是很多的,高级的feature,但是我们这里就不涉及了 -->  

</ehcache>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# spring boot 2.x

由于本次笔记使用 2.x,所以配置方式不太一样了,没有深入研究,是因为提供了自动配置类,自动配置类中其实和手动配置的类似

spring:
  cache:
    type: ehcache
    ehcache:
      config: classpath:/ehcache.xml
1
2
3
4
5

以上配置在 yml 中写,加上 ehcache.xml 就可以了

# spring cache 测试

import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import cn.mrcode.cachepdp.eshop.cache.model.ProductInfo;
import cn.mrcode.cachepdp.eshop.cache.service.CacheService;

@Service
public class CacheServiceImpl implements CacheService {
    public static final String CACHE_NAME = "local";

    /**
     * 将商品信息保存到本地缓存中
     */
    @CachePut(value = CACHE_NAME, key = "'key_'+#productInfo.getId()")
    public ProductInfo saveLocalCache(ProductInfo productInfo) {
        return productInfo;
    }

    /**
     * 从本地缓存中获取商品信息
     */
    @Cacheable(value = CACHE_NAME, key = "'key_'+#id")
    public ProductInfo getLocalCache(Long id) {
        return null;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

可以看到,在存在的时候直接返回了传递进来的对象, 但是在读取的时候返回了 null,如果先存后取成功了的话,那么久说明目前正和 ehache 是成功的

@Controller
public class CacheController {

    @Autowired
    private CacheService cacheService;

    @RequestMapping("/testPutCache")
    @ResponseBody
    public String testPutCache(ProductInfo productInfo) {
        cacheService.saveLocalCache(productInfo);
        return "success";
    }

    @RequestMapping("/testGetCache")
    @ResponseBody
    public ProductInfo testGetCache(Long id) {
        return cacheService.getLocalCache(id);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 启动程序测试

http://localhost:6002/testPutCache?id=1&name=iphone7&price=100.80
http://localhost:6002/testGetCache?id=1
1
2

注意:由于一开始使用 boot 1.x 的手动配置方式,导致存储后,再获取为 null 的情况出现,修改成 boot 2.x 的配置就可以了