Springboot使用ehcache缓存
本文部分步骤继承于springboot使用cache缓存,如果有不清楚的,请移驾springboot使用cache缓存
ehcache
是一种广泛使用的开源Java
分布式缓存。主要面向通用缓存,Java EE
和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip
缓存servlet
过滤器,支持REST
和SOAP api
等特点。
1、依赖导入
整合ehcache
必须要导入它的依赖。
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
2、yml文件
需要说明的是config:classpath:/ehcache.xml
可以不用写,因为默认就是这个路径。但ehcache.xml
必须有。
spring:
cache:
type: ehcache
ehcache:
config: classpath:/config/ehcache.xml
3、ehcache.xml
在resources
目录下新建config
文件夹,在文件夹中建立ehcache.xml
文件。
<ehcache> <!--
磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
path:指定在硬盘上存储对象的路径
path可以配置的目录有:
user.home(用户的家目录)
user.dir(用户当前的工作目录)
java.io.tmpdir(默认的临时目录)
ehcache.disk.store.dir(ehcache的配置目录)
绝对路径(如:d:\\ehcache)
查看路径方法:String tmpDir = System.getProperty("java.io.tmpdir");
-->
<diskStore path="java.io.tmpdir" /> <!--
defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
eternal:代表对象是否永不过期 (指定true则下面两项配置需为0无限期)
timeToIdleSeconds:最大的发呆时间 /秒
timeToLiveSeconds:最大的存活时间 /秒
overflowToDisk:是否允许对象被写入到磁盘
说明:下列配置自缓存建立起600秒(10分钟)有效 。
在有效的600秒(10分钟)内,如果连续120秒(2分钟)未访问缓存,则缓存失效。
就算有访问,也只会存活600秒。
-->
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="true" /> <cache name="myCache" maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="600" overflowToDisk="true" /> </ehcache>
4、使用缓存
@CacheConfig(cacheNames={“myCache”})
设置ehcache
的名称,这个名称必须在ehcache.xml
已配置。
import com.example.ehcache.dao.PersonRepository;
import com.example.ehcache.entity.Person;
import com.example.ehcache.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; /**
* @author 李振
* @date 2018/12/27
*
*/
@Service
@CacheConfig(cacheNames = {"myCache"})
public class DemoServiceImpl implements DemoService {
@Autowired
private PersonRepository personRepository;
/**
* 注意:如果没有指定key,则方法参数作为key保存到缓存中
*/
/**
* @param person
* @return
* @CachePut缓存新增的或更新的数据到缓存,其中缓存的名称为people,数据的key是person的id
*/
@CachePut(key = "#person.id")
@Override
public Person save(Person person) {
Person p = personRepository.save(person);
System.out.println("为id,key为:" + p.getId() + "数据做了缓存");
return p;
} /**
* @param id
* @CacheEvict从缓存people中删除key为id的数据
*/
@CacheEvict
@Override
public void remove(Integer id) {
System.out.println("删除了id,key为" + id + "的数据缓存");
personRepository.delete(id);
} /**
* @param person
* @return
* @Cacheable缓存key为person的id数据到缓存people中
*/
@Cacheable(key = "#person.id")
@Override
public Person findOne(Person person) {
Person p = personRepository.findOne(person.getId());
System.out.println("为id,key为:" + p.getId() + "数据做了缓存");
return p;
}
}
以上
原文出处:
吟风者,springboot使用ehcache缓存, https://www.jianshu.com/p/9e4eb5e5bc1b
Springboot使用ehcache缓存的更多相关文章
- 转载-Springboot整合ehcache缓存
转载:https://www.cnblogs.com/xzmiyx/p/9897623.html EhCache是一个比较成熟的Java缓存框架,最早从hibernate发展而来, 是进程中的缓存系统 ...
- springboot 整合ehcache缓存
1.CacheManager Spring Boot默认集成CacheManager,如下包所示: 可以看出springboot自动配置了 JcacheCacheConfiguration. EhCa ...
- Springboot整合Ehcache缓存
Pom.xml导包 <!-- ehcache --> <dependency> <groupId>org.springframework.boot</grou ...
- springboot整合ehcache缓存失效
最近做了个微信公众号后台,因为只是单应用就选用了ehcache来做本地缓存,主要是用于缓存微信的accece_token和jsapi_ticket.在使用ehcache的时候遇到了@Cacheable ...
- 【spring-boot】spring-boot 整合 ehcache 实现缓存机制
方式一:老 不推荐 参考:https://www.cnblogs.com/lic309/p/4072848.html /*************************第一种 引入 ehcach ...
- SpringBoot学习笔记(10)-----SpringBoot中使用Redis/Mongodb和缓存Ehcache缓存和redis缓存
1. 使用Redis 在使用redis之前,首先要保证安装或有redis的服务器,接下就是引入redis依赖. pom.xml文件如下 <dependency> <groupId&g ...
- SpringBoot中Shiro缓存使用Redis、Ehcache
在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...
- Springboot整合Ehcache 解决Mybatis二级缓存数据脏读 -详细
前面有写了一篇关于这个,但是这几天又改进了一点,就单独一篇在详细说明一下 配置 application.properties ,启用Ehcache # Ehcache缓存 spring.cache.t ...
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
随机推荐
- pytorch_08_RNN
1.循环神经网络的提出是基于记忆模型的想法,期望网络能够记住前面出现的特征,并依据特征推断后面的结果,而且整体的网络结构不断循环,因而得名循环神经网络. 2.循环神经网络的基本结构特别简单,就是将网络 ...
- Nginx 转发页面跳转重定向
简介 Nginx在反向代理过程中,通过重定向跳转时会找不到URL.是因为经常没有配置Host header 的端口,需要如下标红部分一样配置端口号. 只添加Host重定向之后,就会没有端口号. 方案 ...
- 【CF1172E】Nauuo and ODT(Link-Cut Tree)
[CF1172E]Nauuo and ODT(Link-Cut Tree) 题面 CF 给你一棵树,每个节点有一个颜色. 定义一条路径的权值为路径上不同颜色的数量.求所有有向路径的权值和. 有\(m\ ...
- 【BZOJ4823】[CQOI2017]老C的方块(网络流)
[BZOJ4823][CQOI2017]老C的方块(网络流) 题面 BZOJ 题解 首先还是给棋盘进行黑白染色,然后对于特殊边左右两侧的格子单独拎出来考虑. 为了和其他格子区分,我们把两侧的这两个格子 ...
- DedeCMS V5.7 SP2后台代码执行漏洞复现(CNVD-2018-01221)
dedeCMS V5.7 SP2后台代码执行漏洞复现(CNVD-2018-01221) 一.漏洞描述 织梦内容管理系统(Dedecms)是一款PHP开源网站管理系统.Dedecms V5.7 SP2 ...
- 项目中出现多个域名下的Cookie
前言:我们在查看一个项目的Cookie时,有时会看到多个域名下的Cookie,如下图: 其中一种常见的原因是:因为我们在项目中引用了另一个项目的资源.如下图: 重点:浏览器的一种默认机制:如果我们引用 ...
- Mac设置su root密码
转自:https://blog.csdn.net/maxsky/article/details/44905003 大家都知道在 Linux 下,执行 su 命令后输入密码即可切换到 root 用户执 ...
- python基础(27):类成员的修饰符、类的特殊成员
1. 类成员的修饰符 类的所有成员在上一步骤中已经做了详细的介绍,对于每一个类的成员而言都有两种形式: 公有成员,在任何地方都能访问 私有成员,只有在类的内部才能方法 私有成员和公有成员的定义不同:私 ...
- PlayJava Day002
今日所学: /* 2019.08.19开始学习,此为补档. */ 流程控制 条件: 一重用if 二重用if ... else 三重用if ... else if ... else 多重用switch ...
- PHP+Swoole 作为网络通信框架
PHP的异步.并行.高性能网络通信引擎,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,异步Redis,数据库连接池,AsyncTask,消息队列, ...