一、引入:

由于: 我们在反复地查询数据库时 ,会导致数据库压力变大,使得传统的数据库查询效率就不高。

如果短时间内 用户 发起相同的 查询请求 , 而每次都会去访问数据库 , 这就是浪费资源了。因此这个时候,缓存机制很有必要。

定义:EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。

二、使用:

(1)POM文件添加依赖:

<!-- Ehcache -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>

(2)配置文件:

在resource目录下新建ehcache.xml:

<ehcache>
    <diskStore path="java.io.tmpdir/cache" />
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120" />
    <cache
        name="book_cache"
        maxElementsInMemory="10000"
        eternal="true"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        diskPersistent="true"
        diskExpiryThreadIntervalSeconds="600" />
</ehcache>