springboot2.0 使用ehcache缓存

首先在springboot启动类添加

@EnableCaching

其次在serviceImpl层的方法上添加

@Cacheable(cacheNames = "zjehcache")

ehcache.xml:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd">
    <cache name="zjehcache" eternal="true" maxEntriesLocalHeap="0" ></cache>

    <!--
         磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
         path:指定在硬盘上存储对象的路径
      -->
        <!--<diskStore path="java.io.tmpdir" />-->

    <!--
        defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
        maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
        eternal:代表对象是否永不过期
        timeToIdleSeconds:最大的发呆时间
        timeToLiveSeconds:最大的存活时间
        overflowToDisk:是否允许对象被写入到磁盘
    -->
    <!--<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />-->
    <!--
23         cache:为指定名称的对象进行缓存的特殊配置
24         name:指定对象的完整名
25   -->
        <!--<cache name="com.zbaccp.entity.Person" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />-->


</ehcache>

cacheNames 的名字是自定义的与ehcache.xml中的 name一致。

点击项目model开始下载。

猜你喜欢

转载自blog.csdn.net/m0_38044453/article/details/80569858