当前位置:   article > 正文

Springboot搭建ehcache缓存&&redis缓存_springboot ehcache redis

springboot ehcache redis

一、需要引入jira包

maven坐标:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Ehcache 坐标 -->
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

build.gradle

#支持缓存注解
compile 'org.springframework:spring-core:4.3.13.RELEASE'
#引入ehcahce
 compile group: 'net.sf.ehcache', name: 'ehcache', version: '2.10.4'

  • 1
  • 2
  • 3
  • 4
  • 5

二、配置缓存文件

spring: 
   #配置本地redis
  redis:
    host: localhost
    port: 6379

 #配置缓存文件 可以不配置,这是默认的地址。resource目录下的
 #但一定要有ehcache.xml 否则就是默认的
  cache:
    ehcache:
      config: ehcache.xml
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

三、启动了增加支持缓存

@EnableTransactionManagement
@SpringBootApplication(scanBasePackages = {"com.zqm"})
-- 开启缓存
@EnableCaching
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        setRegisterErrorPageFilter(false);
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

综上几个步骤你已经完成了springboot的redis搭建,更多知识,缓存注解请看传送门信息

四、报错"Cannot find cache named ‘getString’ for Builder解决

这是为配置ehcache.xml文件增加那个分组名
对应xml文件中的name。
解决:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  ~ Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
  ~ Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
  ~ Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
  ~ Vestibulum commodo. Ut rhoncus gravida arcu.
  -->

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

<!--  这个是磁盘存储路径,当内存缓存满了的时候,就会往这里面放,-->
<!--  user.home是操作系统缓存的临时目录,不同操作系统缓存目录不一样。-->
    <diskStore path="user.home"/>


    <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="30" timeToLiveSeconds="30"
                  overflowToDisk="false">
    </defaultCache>
    <!-- 
        配置自定义缓存
        maxElementsInMemory:缓存中允许创建的最大对象数
        eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
        timeToIdleSeconds:(单位是秒)缓存数据的钝化时间,也就是在一个元素消亡之前,
                    两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,
                    如果该值是 0 就意味着元素可以停顿无穷长的时间。
        timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,
                    这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
        overflowToDisk:内存不足时,是否启用磁盘缓存。
        memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。
        缓存的3 种清空策略 :
           FIFO ,first in first out (先进先出).
           LFU , Less Frequently Used (最少使用).意思是一直以来最少被使用的。缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存。
           LRU ,Least Recently Used(最近最少使用). (ehcache 默认值).缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
    -->
    <cache name="getString"
           maxElementsInMemory="100000"
           overflowToDisk="false"
           timeToIdleSeconds="20"
           timeToLiveSeconds="36000"
           memoryStoreEvictionPolicy="LFU">
    </cache>
    <cache name="other"
           maxElementsInMemory="100000"
           overflowToDisk="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="36000"
           memoryStoreEvictionPolicy="LFU">
    </cache>
</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
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

五、传送门

  1. Mac redis安装
  2. 缓存注解使用,@Cacheable@CacheEvict@Caching
  3. redis缓存注解使用
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/745049
推荐阅读
相关标签
  

闽ICP备14008679号