SpringBoot 缓存(EhCache 2.x 篇)

SpringBoot 缓存

在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManager),Spring Boot根据下面的顺序去侦测缓存提供者:

* Generic


* JCache (JSR-107)


* EhCache 2.x

* Hazelcast

* Infinispan

* Redis

* Guava

* Simple

关于 Spring Boot 的缓存机制:

高速缓存抽象不提供实际存储,并且依赖于由org.springframework.cache.Cacheorg.springframework.cache.CacheManager接口实现的抽象。 Spring Boot根据实现自动配置合适的CacheManager,只要缓存支持通过@EnableCaching注释启用即可。

Spring Boot 配置 EhCache 2.x

官方文档上对于注解缓存的介绍资料非常之少,往往需要我们自己去了解相应的缓存提供者。我这里主要介绍的是 EhCache .

引入依赖

pom.xml文件中引入以下依赖

      <!--开启 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>

引入配置文件 ehcache.xml

resource文件夹下创建文件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"

updateCheck="false">

<defaultCache

eternal="false"

maxElementsInMemory="1000"

overflowToDisk="false"

diskPersistent="false"

timeToIdleSeconds="0"

timeToLiveSeconds="600"

memoryStoreEvictionPolicy="LRU" />

<!-- 这里的 users 缓存空间是为了下面的 demo 做准备 -->

<cache

name="users"

eternal="false"

maxElementsInMemory="100"

overflowToDisk="false"

diskPersistent="false"

timeToIdleSeconds="0"

timeToLiveSeconds="300"

memoryStoreEvictionPolicy="LRU" />


</ehcache>
ehcache.xml 文件配置详解

部分资料来源于网络

  • diskStore:为缓存路径,ehcache分为内存和磁盘两级,此属性定义磁盘的缓存位置。
  • defaultCache:默认缓存策略,当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,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。

在主类加上启动注解

在 Spring Boot 主类加上开启缓存的注解@EnableCaching

demo : SpringBoot + EhCache

搭建 Spring Boot 工程

我搭建了一个普通的 SpringBoot 工程,配置了 Druid+MySQL。

并在数据库中创建了 users 表,各字段如下:

字段名 属性
id bigint
uuid varchar
name varchar
age int

用户实体类

User.java

public class User {

    private long id;

private String uuid;

private String name;

private Integer age;
//省略 get、set 及 toString 方法

}

用户数据库操作接口

UserDao.java

@Mapper

public interface UserDao{
void delete(String uuid);
User update(User user);

User findByUuid(String uuid);

int save(@Param("user") User user);

}

用户操作Mapper文件

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="qg.fangrui.boot.dao.UserDao">

<!--目的:为Dao接口方法提供SQL语句-->

<!--映射实体对象-->

<resultMap id="UserResultMap" type="qg.fangrui.boot.model.User">

<id property="id" column="id" />

<result property="uuid" column="uuid" />

<result property="name" column="name" />

<result property="age" column="age" />

</resultMap>

<insert id="save">

INSERT INTO users(name, age, uuid)

VALUES (#{user.name}, #{user.age}, #{user.uuid})

</insert>
<select id="findByUuid" resultType="User">

SELECT * FROM users WHERE uuid = #{uuid}

</select>

<delete id="delete">

DELETE FROM users WHERE uuid = #{uuid}

</delete>
</mapper>

用户操作 service 层

一般情况下,我们在Sercive层进行对缓存的操作。先介绍 Ehcache 在 Spring 中的注解:在支持 Spring Cache 的环境下,

* @Cacheable : Spring在每次执行前都会检查Cache中是否存在相同key的缓存元素,如果存在就不再执行该方法,而是直接从缓存中获取结果进行返回,否则才会执行并将返回结果存入指定的缓存中。

* @CacheEvict : 清除缓存。

* @CachePut : @CachePut也可以声明一个方法支持缓存功能。使用@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。

* 这三个方法中都有两个主要的属性:value 指的是 ehcache.xml 中的缓存策略空间;key 指的是缓存的标识,同时可以用 # 来引用参数。

UserService.java

@Service

public class UserService {




//这里的单引号不能少,否则会报错,被识别是一个对象

private static final String CACHE_KEY = "'user'";

private static final String DEMO_CACHE_NAME = "users";

@Autowired

private UserDao userDao;

//删除用户数据

@CacheEvict(value = DEMO_CACHE_NAME,key = "'user_'+#uuid")//这是清除缓存

public void delete(String uuid){

userDao.delete(uuid);

}
//更新用户数据

@CachePut(value = DEMO_CACHE_NAME,key = "'user_'+#user.getUuid()")

public User update(User user) throws CacheException{

User user1 = userDao.findByUuid(user.getUuid());

if (null == user1){

throw new CacheException("Not Find");

}

user1.setAge(user.getAge());

user1.setName(user.getName());

return user1;

}
//查找用户数据

@Cacheable(value=DEMO_CACHE_NAME,key="'user_'+#uuid")

public User findByUuid(String uuid){

//若找不到缓存将打印出提示语句

System.err.println("没有走缓存!"+uuid);

return userDao.findByUuid(uuid);

}

//保存用户数据

@CacheEvict(value=DEMO_CACHE_NAME,key=CACHE_KEY)

public int save(User user){

return userDao.save(user);

}

}

Controller 类

最后我们创建一个 Controller 来访问我们的缓存。因为我的 SpringBoot 处于 Debug 模式,会将所有的数据库操作打印出来,这样子缓存作用就可一目了然了。

EhcacheController.java

@RestController
public class EhcacheController {

private static final Logger logger = LoggerFactory.getLogger(EhcacheController.class);

@Autowired

private UserService userService;

@RequestMapping("/encache")

public String EhcacheTest(){

logger.debug("进行Encache缓存测试");

System.out.println("====生成第一个用户====");

User user1 = new User();

//生成第一个用户的唯一标识符 UUID

String u1_uuid = UUID.randomUUID().toString();

//去掉 UUID 的 - 符号

String uuid1 = u1_uuid.substring(0,8)+u1_uuid.substring(9,13)+u1_uuid.substring(14,18)+u1_uuid.substring(19,23)+u1_uuid.substring(24);

user1.setName("张三");

user1.setAge(18);

user1.setUuid(uuid1);

if (userService.save(user1) == 0){

throw new JdbcException("用户对象插入数据库失败");

}
//第一次查询

System.out.println(userService.findByUuid(user1.getUuid()));

//通过缓存查询

System.out.println(userService.findByUuid(user1.getUuid()));

System.out.println("====修改数据====");

User user2 = new User();

user2.setName("李四-update");

user2.setAge(22);

user2.setId(user1.getId());

user2.setUuid(user1.getUuid());

try {

System.out.println(userService.update(user2));

} catch (CacheException e){

e.printStackTrace();

}
System.out.println(userService.findByUuid(user2.getUuid()));

return "success";

}

}

测试

启动 SpringBoot 工程,访问 http://localhost:8080/encache ,并查看控制台打印信息:



由控制台,我们可以清楚到看到,第一次查询用户信息时,工程将用户信息存入缓存中;在第二次查询时,无需访问数据库直接从缓存中获取用户信息。

个人参考项目:

个人参考项目:https://github.com/FunriLy/springboot-study/tree/master/%E6%A1%88%E4%BE%8B9

Spring Boot集成EHCache实现缓存机制的更多相关文章

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

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

  2. (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...

  3. Spring Boot 集成 Redis 实现缓存机制

    本文章牵涉到的技术点比较多:spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对以上这些技术点有一定的了解或者也可以先看看这篇文章 ...

  4. Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

    转自:https://blog.csdn.net/linxingliang/article/details/52263763 spring boot 自学笔记(三) Redis集成—RedisTemp ...

  5. Spring Boot 集成 Ehcache 缓存,三步搞定!

    作者:谭朝红 www.ramostear.com/articles/spring_boot_ehcache.html 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序 ...

  6. spring boot集成ehcache 2.x 用于hibernate二级缓存

    https://www.jianshu.com/p/87b2c309b776 本文将介绍如何在spring boot中集成ehcache作为hibernate的二级缓存.各个框架版本如下 spring ...

  7. 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...

  8. Spring Boot从入门到精通(六)集成Redis实现缓存机制

    Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言 ...

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

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

随机推荐

  1. Android基础新手教程——1.2.1 使用Eclipse + ADT + SDK开发Android APP

    Android基础新手教程--1.2.1 使用Eclipse + ADT + SDK开发Android APP 标签(空格分隔): Android基础新手教程 1.前言 这里我们有两条路能够选,直接使 ...

  2. 小项目: low版本的 员工信息程序:

    ### 附加两个文件1 user_info 和worker_info flag = False def logon(): #登录函数 global flag usr = input('Username ...

  3. Elasticsearch之marvel(集群管理、监控)插件安装之后的浏览详解

    前提 Elasticsearch之插件介绍及安装 https://i.cnblogs.com/posts?categoryid=950999&page=2  (强烈建议,从头开始看) 比如,我 ...

  4. jQuery插件--根据数据加载的进度动画案例

    css: *{ margin:; padding:; } @media screen and (min-width:320px){ html{font-size:12px;}} @media scre ...

  5. centos的dns配置总结

    找的一篇好的存根 DNS正反向解析 DNS(Domain Name Service)域名服务.就是域名解析服务器.所谓名称解析的过程就是某个应用程序基于某个搜索键在指定的数据库中查询.而后查询到某些对 ...

  6. ipcalcIP地址计算

    ipcalc命令是一个简单的ip地址计算器,可以完成简单的IP地址计算任务. 语法 ipcalc(选项) 选项 -b:由给定的IP地址和网络掩码计算出广播地址: -h:显示给定UP地址所对应的主机名: ...

  7. Mysql学习总结(18)——Mysql主从架构的复制原理及配置详解

    一.复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves)上,并重 ...

  8. src/MD2.c:31:20: 错误:Python.h:没有那个文件或目录

    一.前言 在CentOS 上安装fabric时出现问题,首先已安装pip, 用pip执行以下命令pip install 出现以下问题 [niy@niy-computer /]$ sudo pip in ...

  9. 【iOS与EV3混合机器人编程系列之中的一个】iOS要干嘛?EV3能够更酷!

    乐高Mindstorm EV3智能机器人(下面简称EV3)自从在2013年的CES(Consumer Electronics Show美国消费电子展)上展出之后,就吸引了全球广大机器人爱好者的眼球!E ...

  10. javascript创建对象的方法--构造函数模式

    javascript创建对象的方法--构造函数模式 一.总结 构造函数模式作用和不足 1.作用:解决工厂模式不是用new关键字来创建对象的弊端 2.作用:解决工厂模式创建的实例和模型没有内在联系的问题 ...