1、CacheManager

Spring Boot默认集成CacheManager,如下包所示:

  可以看出springboot自动配置了 JcacheCacheConfiguration、 EhCacheCacheConfiguration、HazelcastCacheConfiguration、GuavaCacheConfiguration、RedisCacheConfiguration、SimpleCacheConfiguration 等。

  默认的CacheManager为ConcurrenMapCacheManager,Spring从Spring3.1开始基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器。Spring Boot 默认使用 ConcurrentMapCacheManager作为缓存技术。新建一个springboot项目,直接使用cache相关注解来测试,效果如下:

2、整合encache

  pom依赖引入

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

  application文件配置

spring:
cache:
#ehcache配置文件路径
ehcache:
config: classpath:/ehcache/ehcache.xml
#指定缓存类型,可加可不加
#type: ehcache

  encache.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="myEncache"> <!--
diskStore:为缓存路径,ehcache分为内存和磁盘 2级,此属性定义磁盘的缓存位置
user.home - 用户主目录
user.dir - 用户当前工作目录
java.io.tmpdir - 默认临时文件路径
-->
<diskStore path="D:/home/Tmp_Ehcache"/>
<!--
name:缓存名称。
maxElementsInMemory:缓存最大数目
maxElementsOnDisk:硬盘最大缓存个数。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
overflowToDisk:是否保存到磁盘,当系统宕机时
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
memoryStoreEvictionPolicy:可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。
FIFO,first in first out,这个是大家最熟的,先进先出。
LFU, Less Frequently Used,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
LRU,Least Recently Used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
-->
<defaultCache
eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU"
/>
<cache
name="users_test"
eternal="false"
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU"
/> </ehcache>

  启动类加上启用缓存注解

@EnableCaching
public class SpringbootCacheApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootCacheApplication.class, args);
} }

  代码中使用cache注解

@Service
public class UserServiceImpl implements UserService { @Autowired
private UserMapper userMapper; //使用ehcache配置的缓存名users_test
private final String USER_CACHE_NAME = "users_test"; @Override
public List<User> listUser() {
return userMapper.selectUserList();
} @Override
// @Cacheable(value = USER_CACHE_NAME, key = "#id")
@Cacheable(value = USER_CACHE_NAME, key = "'user' + #id")
public User selectUserById(Integer id) {
return userMapper.selectUserById(id);
} @Override
// @CacheEvict(value = USER_CACHE_NAME, key = "#id")
@CacheEvict(value = USER_CACHE_NAME, key = "'user_' + #id")
public void delete(Integer id) {
userMapper.delete(id);
} @Override
// @CacheEvict(value = USER_CACHE_NAME, key = "#user.userId")
@CacheEvict(value = USER_CACHE_NAME, key = "'user' + #user.userId")
// @CachePut(value = USER_CACHE_NAME, key = "'user' + #user.userId") //测试发现只将结果清除,key未清除,导致查询继续使用缓存但结果为空????
public void update(User user) {
userMapper.update(user);
}
}

  项目启动后查看CacheManager,如下图所示则表示整合ehcache成功 

参考源码:Github

springboot 整合ehcache缓存的更多相关文章

  1. 转载-Springboot整合ehcache缓存

    转载:https://www.cnblogs.com/xzmiyx/p/9897623.html EhCache是一个比较成熟的Java缓存框架,最早从hibernate发展而来, 是进程中的缓存系统 ...

  2. Springboot整合Ehcache缓存

    Pom.xml导包 <!-- ehcache --> <dependency> <groupId>org.springframework.boot</grou ...

  3. springboot整合ehcache缓存失效

    最近做了个微信公众号后台,因为只是单应用就选用了ehcache来做本地缓存,主要是用于缓存微信的accece_token和jsapi_ticket.在使用ehcache的时候遇到了@Cacheable ...

  4. Springboot使用ehcache缓存

    本文部分步骤继承于springboot使用cache缓存,如果有不清楚的,请移驾springboot使用cache缓存 ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存,Java ...

  5. 【Springboot】Springboot整合Ehcache

    刚刚项目上线了,记录下使用的技术...... EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache的特点 ( ...

  6. [原创]mybatis中整合ehcache缓存框架的使用

    mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...

  7. MyBatis高级篇之整合ehcache缓存框架

    MyBatis高级篇之整合ehcache缓存框架  2017-09-01  0 Comments  1,671 Views  0 Times 一.前言 MyBatis为我们提供了Cache接口,也提供 ...

  8. 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出

    1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法   Shiro框架内部整合好缓存管理器, ...

  9. 项目一:第十三天 1、菜单数据管理 2、权限数据管理 3、角色数据管理 4、用户数据管理 5、在realm中动态查询用户权限,角色 6、Shiro中整合ehcache缓存权限数据

    1 课程计划 菜单数据管理 权限数据管理 角色数据管理 用户数据管理 在realm中动态查询用户权限,角色 Shiro中整合ehcache缓存权限数据         2 菜单数据添加 2.1 使用c ...

随机推荐

  1. Django + JQuery + ajax实时显示数据

    1 创建django项目 :Visualization and  APP :Demo >>django-admin startproject Visualization >>p ...

  2. JAVA实现读取图片

    话不读说  直接上代码 package cn.kgc.ssm.common; import java.io.*; /** * @author * @create 2019-08-15 9:36 **/ ...

  3. ubuntu安装elasticsearch及head插件

    1.安装elasticsearch,参考http://www.cnblogs.com/hanyinglong/p/5409003.html就可以了 简单描述下: mkdir -p /usr/local ...

  4. 特殊字符处理 java-jsp

    public String dealStr(String name){ String newStr=""; if(name != null && name.leng ...

  5. Count on a tree 树上区间第K小

    Count on a tree 题意:求路径 u到v上的 第k小的权重. 题解:先DFS建数, 然后对于每个节点往上跑出一颗主席树, 然后每次更新. 查询的时候, u, v, k, 找到  z = l ...

  6. hdu 1024 Max Sum Plus Plus(简单dp)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意:给定一个数组,求其分成m个不相交子段和的最大值. 这题有点问题其实m挺小的但题目并没有给出 ...

  7. HDU 1087 Super Jumping! Jumping! Jumping! 最长递增子序列(求可能的递增序列的和的最大值) *

    Super Jumping! Jumping! Jumping! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64 ...

  8. codeforces 807 D. Dynamic Problem Scoring(贪心+思维)

    题目链接:http://codeforces.com/contest/807/problem/D 题意:对于动态计分的 Codeforces Round ,已知每题的 score 是根据 Round ...

  9. 你真的了解Grid布局吗?

    Grid网格布局 概述:Grid将容器划分为一个个网格,通过任意组合不同的网格,做出你想想要的布局 Grid与flex布局相似,将整个Grid分为了容器与子项(格子) Grid容器的三个重要的概念: ...

  10. Object和Objects

    Object 在Java,Object类是超级父类,是所有类的父类. public boolean equals(Object obj) { return (this == obj); } publi ...