0
点赞
收藏
分享

微信扫一扫

169-springboot集成(三):ehcache使用

pom依赖:

        <!--添加springdata-cache依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>

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 path="user.dir/cache_v1"/>

    <!-- maxEntriesLocalHeap:堆内存中最大缓存对象数,0没有限制 -->
    <!-- maxElementsInMemory: 在内存中缓存的element的最大数目。-->
    <!-- eternal:elements是否永久有效,如果为true,timeouts将被忽略,element将永不过期 -->
    <!-- timeToIdleSeconds:发呆秒数,发呆期间未访问缓存立即过期,当eternal为false时,这个属性才有效,0为不限制 -->
    <!-- timeToLiveSeconds:总存活秒数,当eternal为false时,这个属性才有效,0为不限制 -->
    <!-- overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上 -->
    <!-- statistics:是否收集统计信息。如果需要监控缓存使用情况,应该打开这个选项。默认为关闭(统计会影响性能)。设置statistics="true"开启统计 -->
    <!--
        默认缓存
        无过期时间,但 600 秒内无人访问缓存立即过期
    -->
    <defaultCache
            maxElementsInMemory="1000"
            eternal="false"
            timeToIdleSeconds="600"
            timeToLiveSeconds="0"
            overflowToDisk="false">
    </defaultCache>

    <cache name="sys"
           maxElementsInMemory="1"
           eternal="false"
           timeToIdleSeconds="5"
           timeToLiveSeconds="5"
           overflowToDisk="false"
           memoryStoreEvictionPolicy="LRU"/>

</ehcache>

application.yml 配置:

spring:
  cache:
    type: ehcache
    ehcache:
      config: classpath:/ehcache.xml

service层使用:

    @Cacheable(key = "#key",value = "sys")
    public Page<TestOne> listCache(String key){
        Page<TestOne> page = new Page<>();
        page = baseMapper.selectPage(page,null);
        return page;
    }
举报

相关推荐

0 条评论