配置

maven

<!-- Spring Boot 缓存支持启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><!-- ehcache --><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId></dependency>

启动类

@SpringBootApplication
@ImportResource(locations = "classpath:cache/cache_manager.xml")

EhCacheCacheManager配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache-3.1.xsd"><!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 --><cache:annotation-driven cache-manager="ehCacheManager" /><!-- cacheManager工厂类,指定ehcache.xml的位置 --><bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation"  value="classpath:cache/ehcache.xml"/></bean><!-- 声明cacheManager --><bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"><property name="cacheManager" ref="cacheManagerFactory"/></bean>
</beans>

ehcache配置

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"dynamicConfig="false"><diskStore path="java.io.tmpdir/ehcache"/><!--diskStore path:用来配置磁盘缓存使用的物理路径name:  缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里)eternal="false"   元素是否永恒,如果是就永不过期(必须设置)maxElementsOnDisk====磁盘缓存中最多可以存放的元素数量,0表示无穷大maxElementsInMemory="1000" 内存缓存中最多可以存放的元素数量(必须设置)timeToIdleSeconds="0"   元素不被访问 间隔多久过期(秒为单位). 0表示可以永远空闲,默认为0。要小于timeToLiveSeconds才有意义timeToLiveSeconds="600" 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期。overflowToDisk="false"  当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)diskPersistent="false"  磁盘缓存在VM重新启动时是否保持(默认为false)diskExpiryThreadIntervalSeconds="100" 磁盘失效线程运行时间间隔,默认是120秒memoryStoreEvictionPolicy="LFU" 内存存储与释放策略.当达到maxElementsInMemory时共有三种策略,分别为LRU(最近最少使用)、LFU(最常用的)、FIFO(先进先出)默认使用"最近使用"策略--><defaultCacheeternal="false"maxElementsInMemory="3000"timeToIdleSeconds="3600"timeToLiveSeconds="0"overflowToDisk="true"diskPersistent="false"diskExpiryThreadIntervalSeconds="100"memoryStoreEvictionPolicy="LRU"/><cache name="userCache"eternal="false"maxElementsInMemory="2"overflowToDisk="true"timeToIdleSeconds="0"timeToLiveSeconds="60"memoryStoreEvictionPolicy="LFU"/></ehcache>

记录一下配置中的坑,分享给大家

问题记录

问题1

编译报错

301 Moved Permanently'

问题2

编译报错

Caused by: org.xml.sax.SAXParseException: cvc-elt.1: 找不到元素 'ehcache' 的声明。at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:204) ~[na:1.8.0_251]

问题1把http变成https就会报问题2的错误
问题1和问题2,都是因为使用ImportResource去加载ehcache.xml
很多人(包括我)喜欢加载文件这样做:

@ImportResource(locations = "classpath:cache/*.xml")

但ehcache.xml不属于spring的xml配置

github地址

https://github.com/shuaidongshuai/spring-use-ehcache-redis

SpringBoot使用Ehcache相关推荐

  1. 搭建第一个SpringBoot工程;SpringBoot整合mybatis;SpringBoot整合Redis-cluster集群;SpringBoot整合EhCache;

    写在前头,以下内容主要是为了自己复习之用,如果你有幸看到这篇文章,请不要嫌弃某些地方有所跳跃或省略. 1. 搭建第一个SpringBoot工程 1.1 创建工程 1.2 初始pom.xml文件内容 1 ...

  2. springboot整合ehcache+redis实现双缓存

    在一些对并发业务要求较高的场景下,对页面数据的响应是个急需解决的问题,对后端来说,ehcache+redis实现双缓存是解决这一问题的不错思路,而且在不少的电商项目中得到了很好的验证,但我在网上搜寻资 ...

  3. spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)

    SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...

  4. springboot整合ehcache使用

    springboot整合ehcache使用 其实本地缓存的解决方案也有很多种,像Ehcache,GuavaCache,JCache等目前Ehcache 是现在最流行的纯Java开源缓存框架,配置简单. ...

  5. Shiro+springboot+mybatis+EhCache(md5+salt+散列)认证与授权-03

    从上文:Shiro+springboot+mybatis(md5+salt+散列)认证与授权-02 当每次进行刷新时,都会从数据库重新查询数据进行授权操作,这样无疑给数据库造成很大的压力,所以需要引入 ...

  6. Springboot + Mybatis + Ehcache

    最近在做一个项目,为处理并发性较差的问题,使用了Mybatis二级缓存 但在多表联合查询的情况下,Mybatis二级缓存是存在着数据脏读的问题的 两天就是在想办法解决这个数据脏读的问题 考虑到简易性. ...

  7. springboot 缓存ehcache的简单使用

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 步骤: 1. pom文件中加 maven jar包: <!-- ehcache 缓存 --&g ...

  8. ehcache怎么删除缓存_解释SpringBoot之Ehcache 2.x缓存

    介绍 这里介绍Ehcache 2.X 缓存 添加基本的web项目 添加Ehcache 依赖 xml version="1.0" encoding="UTF-8" ...

  9. springBoot整合Ehcache——工具类调用

    项目需求要缓存常用数据,整合Ehcache实现,结果@Cacheable注解怎么都不管用,网上找了很多资料各种试各种不行,无奈!只好写个工具类使用. 一.pom.xml配置 <dependenc ...

  10. SpringBoot+Shiro+ehcache实现登录失败超次数锁定帐号

    文章目录 二.Controller层接收登录请求 三.自定义的Realm 四.密码验证器增加登录次数校验功能 五.ShiroConfig的配置类 六.EhCache 的配置 七.全局异常的配置 ### ...

最新文章

  1. 特征工程系列:特征筛选的原理与实现(下)
  2. 关于学习Python的一点学习总结(12->字典相关操作)
  3. 智能新经济的AI平台:左手技术,右手行业
  4. 冷到穿棉袄?数据中心其实可以“暖”一点
  5. COM编程之二 接口
  6. android 链接分享到朋友圈,android 分享到微信朋友圈或微信好友
  7. TotoiseSVN的上手教程
  8. 拖动布局之保存布局页面
  9. C代码在SDK编辑中的使用————(一)
  10. python 标准库 —— 线程与同步(threading、multiprocessing)
  11. C++的STL标准库学习(vector)
  12. 语音识别(五)——Mel-Frequency Analysis, FBank, 语音识别的评价指标, 声学模型进阶
  13. 裸设备和Oracle问答20例
  14. RFID固定资产管理系统是如何盘点固定资产的?
  15. BundleIdentifier (MultipleValues)
  16. matlab倒谱法基音周期,语音学习笔记1------matlab实现自相关函数法基音周期提取...
  17. 互联网:常见运营术语,PV、UV、GMV、CVR等
  18. excel宏 java,Microsoft Excel宏运行Java程序
  19. MySQL数据库卸载+MySQL常用的图形化管理工具介绍
  20. 计算机一级考试office题库2016,一级计算机练习题全国计算机等级考试一级MS+Office真题题库3+2016年...

热门文章

  1. 金融科技在互联网金融行业性风险防范领域的应用
  2. 甜在心馒头店通过泊松分布解决备货烦恼
  3. VSCode加入右键功能菜单
  4. 致远互联的下一步:从“协同”到“门户”
  5. xhr的get和post请求的简单实现
  6. DVWA靶机安装(超详细教程)
  7. dvwa最详细安装过程
  8. 自考多媒体计算机技术,自考多媒体计算机技术.doc
  9. 实对称矩阵一定可以对角化
  10. ArcGIS基础学习笔记之空间数据库管理工具ArcCatalog基础