springboot整和ehcache缓存

1、增加pom依赖
   

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


 2、增加配置文件  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" >
    <diskStore path = "/date/recommendapi/ehcache" />
 
    <defaultCache
            eternal="false"
            maxElementsInMemory="1000"
            maxElementsOnDisk = "10000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="0"
            timeToLiveSeconds="30"
            memoryStoreEvictionPolicy="LRU"
    />
    <!-- 缓存策略可根据需要自行添加cache
            name="recommend"                 缓存策略名称
            eternal="false"                  是否永久缓存
            maxElementsInMemory="1000"       内存缓存最大个数
            maxElementsOnDisk = "10000"      磁盘缓存最大个数
            overflowToDisk="true"            超出内存最大数是否缓存到磁盘
            diskPersistent="false"           是否持久化到磁盘,重启后恢复
            timeToIdleSeconds="0"            对象空闲时间,只对eternal属性为false起作用,0为永久
            timeToLiveSeconds="30"           对象存活时间,只对eternal属性为false起作用,0为永久
            memoryStoreEvictionPolicy="LRU"  超过内存空间,向磁盘存储数据的策略,如果overflowToDisk为false则会清理,
                                             可选:LRU 最近最少使用 FIFO 先进先出  LFU 最少使用
     -->
 
    <cache
            name="getList"   缓存value
            eternal="false"
            maxElementsInMemory="500"
            maxElementsOnDisk = "10000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="0"
            timeToLiveSeconds="30"
            memoryStoreEvictionPolicy="LRU"
    />
</ehcache>



3、properties中增加配置

#--------------------------ehcache--------------------------
spring.cache.type = ehcache
spring.cache.ehcache.config = classpath:/ehcache.xml


4、启动类上增加注解


 @EnableCaching


5、在方法中使用

  @Cacheable(value = "getList", key = "#root.targetClass.name+#root.methodName+#appId+#userId")
    public String getList(String appId,String userId) {...}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值