方式一:老 不推荐

参考:https://www.cnblogs.com/lic309/p/4072848.html

/*************************第一种   引入 ehcache.xml   使用注解即可  **********************************

方式二:集成springboot   

参考:https://www.jb51.net/article/135050.htm

2.1依赖:

 

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

2.2  入口类配置

加入注解 @EnableCaching

@SpringBootApplication
@EnableCaching
public class DemoApplication {
}

2.3 在src\main\resources目录下,添加ehcache.xml文件

ehcache.xml:(关于字段意思:参考:https://www.cnblogs.com/lic309/p/4072848.html)

<?xml version="1.0" encoding="UTF-8"?>
     xsi:noNamespaceSchemaLocation = "http://ehcache.org/ehcache.xsd"
     updateCheck = "false">
 
 <!-- 指定一个文件目录,当EHCache把数据写到硬盘上时,将把数据写到这个文件目录下 -->
 <diskStore path = "java.io.tmpdir"/>
 
 <!-- 默认的管理策略 -->
 <defaultCache
   eternal = "false"
   maxElementsInMemory = "10000"
   overflowToDisk = "true"
   diskPersistent = "false"
   timeToIdleSeconds = "120"
   timeToLiveSeconds = "120"
   diskExpiryThreadIntervalSeconds = "120"
   memoryStoreEvictionPolicy = "LRU"/>
 
 <!-- 此缓存最多可以存活timeToLiveSeconds秒,如果期间超过timeToIdleSeconds秒未访问,缓存失效 -->
 <cache
   name = "userCache"
   eternal = "false"
   maxElementsInMemory = "100"
   overflowToDisk = "false"
   diskPersistent = "false"
   timeToIdleSeconds = "120"
   timeToLiveSeconds = "180"
   memoryStoreEvictionPolicy = "LRU"/>
 
 <!-- maxElementsInMemory 内存中最大缓存对象数,看着自己的heap大小来搞 -->
 <!-- eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false -->
 <!-- maxElementsOnDisk:硬盘中最大缓存对象数,若是0表示无穷大 -->
 <!-- overflowToDisk:true表示当内存缓存的对象数目达到了maxElementsInMemory界限后,
 会把溢出的对象写到硬盘缓存中。注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。-->
 <!-- diskSpoolBufferSizeMB:磁盘缓存区大小,默认为30MB。每个Cache都应该有自己的一个缓存区。-->
 <!-- diskPersistent:是否缓存虚拟机重启期数据 -->
 <!-- diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认为120秒 -->
 
 <!-- timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,
 如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,
 EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,
 则表示对象可以无限期地处于空闲状态 -->
 
 <!-- timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,
 如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期,
 EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,
 则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义 -->
 
 <!-- memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,
 Ehcache将会根据指定的策略去清理内存。可选策略有:LRU(最近最少使用,默认策略)、
 FIFO(先进先出)、LFU(最少访问次数)。-->
 
</ehcache>

2.4 application.application配置

# 配置ehcache缓存
spring.cache.type=ehcache
# 指定ehcache配置文件路径
spring.cache.ehcache.config=classpath:/ehcache.xml

test:

import com.bbf.frame.Application;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import javax.annotation.Resource; @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class TestCache {
@Resource
private CacheManager cacheManager; @Test
public void cacheTest() {
// 显示所有的Cache空间
System.out.println(StringUtils.join(cacheManager.getCacheNames(), ","));
Cache cache = cacheManager.getCache("userCache");
cache.put("key", "");
System.out.println("缓存成功");
String res = cache.get("key", String.class);
System.out.println(res);
}
}

@Cacheable 拼接key   eg:

@Cacheable(value = "page_user",key ="T(String).valueOf(#page).concat('-').concat(#pageSize)",unless = "#result=null")//由于page是int型,concat要求变量必须为String,所以强转一下
@Override
public List<SysUserEntity> page(int page, int pageSize) { return userMapper.page(page,pageSize); }

【spring-boot】spring-boot 整合 ehcache 实现缓存机制的更多相关文章

  1. 【spring-boot】spring-boot整合ehcache实现缓存机制

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. ehcache提供了多种缓存策略,主要分为内存和磁盘两级,所以无需担心 ...

  2. spring-boot整合ehcache实现缓存机制

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. ehcache提供了多种缓存策略,主要分为内存和磁盘两级,所以无需担心 ...

  3. (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...

  4. Spring使用Cache、整合Ehcache(转)

    今天在做Spring使用Cache.整合Ehcache时发现一篇非常好的文章,原文地址 http://elim.iteye.com/blog/2123030 从3.1开始,Spring引入了对Cach ...

  5. Spring Boot集成EHCache实现缓存机制

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

  6. Spring整合Ehcache管理缓存

    前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...

  7. Spring整合Ehcache管理缓存(转)

    目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...

  8. spring整合ehcache实现缓存

    Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它支持注解方式使用缓存,非常方便. spring本身内置了对Cache的支持,之 ...

  9. Spring使用Cache、整合Ehcache

    http://haohaoxuexi.iteye.com/blog/2123030 Spring使用Cache 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring ...

随机推荐

  1. xp sql2000 安装SP4失败解决方案

    环境:xp sp3 SQL: sql2000sql2000 SP4下载链接: http://www.microsoft.com/downloads/details.aspx?familyid=8E2D ...

  2. NodeJs之配置文件管理

    查询了一些资料,我使用nodejs的object作为配置文件,首先定义一个module config.js: var config = { uploadPath: "E:\\" } ...

  3. Ajax中最有名axios插件(只应用于Ajax)(post方法,官网写错了,应是字符串格式)

    /* axios v0.18.0 | (c) 2018 by Matt Zabriskie */!function(e,t){"object"==typeof exports&am ...

  4. ubuntu 间简单相互通信

    1.  nc  命令 在一台机器上运行nc -l 来监听本机的2222号端口 另外一台机器就能连接到这台监听的机器上,假设上面那台机器的ip是192. nc 之后就能互相发送字符了 2. iptux通 ...

  5. APP中https证书有效性验证引发安全问题(例Fiddler可抓https包)

    原文: https://blog.csdn.net/woddle/article/details/71175140 在实际项目代码审计中发现,目前很多手机银行虽然使用了https通信方式,但是只是简单 ...

  6. [UE4]Delay的使用技巧:改变引擎执行顺序

    如果要游戏一开始就让机器人开火,但这是引擎还没有执行到武器的创建步骤,就可以使用“Delay”并设置函数的等待时间,让引擎先执行创建枪的步骤,然后机器人开火就没问题了.

  7. Apache CLI Demo

    1. Options private Options options = new Options(); 2. option (1) way1 launcher.options.addOption(&q ...

  8. 使用FPM打包工具打rpm包

    使用FPM打包工具打rpm包 一:安装ruby环境和gem命令 fpm 是 ruby写的,因此系统环境需要ruby且版本必须大于1.8.5 # yum -y install ruby rubygems ...

  9. 超详细Redis数据库入门教程

    [本教程目录] 1.redis是什么2.redis的作者何许人也3.谁在使用redis4.学会安装redis5.学会启动redis6.使用redis客户端7.redis数据结构 – 简介8.redis ...

  10. python-模块的导入import

    #-*- coding:utf-8 -*- #本次学习:模块的导入 ''' 1.模块名不能与第三方库或者本地库名字重名/冲突 2.导入模块时,寻找顺序:现在当前目录找,再去我们环境变量配置的pytho ...