SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache
前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache、RedisCache、ConcurrentMapCache等。
这一节我们来看看Spring Cache使用EhCache。
一、EhCache使用演示
EhCache是一个纯Java的进程内缓存框架,具有快速、精干等特点,Hibernate中的默认Cache就是使用的EhCache。
本章节示例是在Spring Boot集成Spring Cache的源码基础上进行改造。源码地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache
使用EhCache作为缓存,我们先引入相关依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--ehcache依赖-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
然后创建EhCache的配置文件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">
<!--
磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于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="cache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"
timeToLiveSeconds="600" overflowToDisk="true" />
</ehcache>
然后SpringBoot配置文件中,指明缓存类型并声明Ehcache配置文件的位置。
server:
port: 10900
spring:
profiles:
active: dev
cache:
type: ehcache
ehcache:
config: classpath:/ehcache.xml
这样就可以开始使用Ehcache了,测试代码与Spring Boot集成Spring Cache一致。
SpringBoot启动类,@EnableCaching开启Spring Cache缓存功能。
@EnableCaching
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
String tmpDir = System.getProperty("java.io.tmpdir");
System.out.println("临时路径:" + tmpDir);
SpringApplication.run(SpringbootApplication.class, args);
}
}
CacheApi接口调用类,方便调用进行测试。
@RestController
@RequestMapping("cache")
public class CacheApi {
@Autowired
private CacheService cacheService;
@GetMapping("get")
public User get(@RequestParam int id){
return cacheService.get(id);
}
@PostMapping("set")
public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){
User u = new User(code, name);
return cacheService.set(id, u);
}
@DeleteMapping("del")
public void del(@RequestParam int id){
cacheService.del(id);
}
}
CacheService缓存业务处理类,添加缓存,更新缓存和删除。
@Slf4j
@Service
public class CacheService {
private Map<Integer, User> dataMap = new HashMap <Integer, User>(){
{
for (int i = 1; i < 100 ; i++) {
User u = new User("code" + i, "name" + i);
put(i, u);
}
}
};
// 获取数据
@Cacheable(value = "cache", key = "'user:' + #id")
public User get(int id){
log.info("通过id{}查询获取", id);
return dataMap.get(id);
}
// 更新数据
@CachePut(value = "cache", key = "'user:' + #id")
public User set(int id, User u){
log.info("更新id{}数据", id);
dataMap.put(id, u);
return u;
}
//删除数据
@CacheEvict(value = "cache", key = "'user:' + #id")
public void del(int id){
log.info("删除id{}数据", id);
dataMap.remove(id);
}
}
源码地址:https://github.com/imyanger/springboot-project/tree/master/p22-springboot-cache-ehcache
SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache的更多相关文章
- SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache
前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...
- Spring Boot集成Spring Data Reids和Spring Session实现Session共享
首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...
- Spring boot集成spring session实现session共享
最近使用spring boot开发一个系统,nginx做负载均衡分发请求到多个tomcat,此时访问页面会把请求分发到不同的服务器,session是存在服务器端,如果首次访问被分发到A服务器,那么se ...
- Spring Boot 集成spring security4
项目GitHub地址 : https://github.com/FrameReserve/TrainingBoot Spring Boot (三)集成spring security,标记地址: htt ...
- Spring Boot 集成 Spring Security 实现权限认证模块
作者:王帅@CodeSheep 写在前面 关于 Spring Security Web系统的认证和权限模块也算是一个系统的基础设施了,几乎任何的互联网服务都会涉及到这方面的要求.在Java EE领 ...
- Spring boot 集成Spring Security
依赖jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...
- SpringBoot系列:Spring Boot集成Spring Cache
一.关于Spring Cache 缓存在现在的应用中越来越重要, Spring从3.1开始定义了org.springframework.cache.Cache和org.springframework. ...
- Spring Boot 集成 Spring Security
1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Spring Boot 集成 Spring Security 入门案例教程
前言 本文作为入门级的DEMO,完全按照官网实例演示: 项目目录结构 Maven 依赖 <parent> <groupId>org.springframework.boot&l ...
随机推荐
- Spring Boot应用上传文件时报错
问题描述 Spring Boot应用(使用默认的嵌入式Tomcat)在上传文件时,偶尔会出现上传失败的情况,后台报错日志信息如下:"The temporary upload location ...
- 初入计算机专业,学习c语言的第一周作业问答
2019年9月17日下午3点30,我来到了1117教室准备上我进入大学的第一堂计算机专业课,并需要完成以下作业. 2.1 你对软件工程专业或者计算机科学与技术专业了解是怎样? 我所了解的计算机就是一台 ...
- [AWS] S3 Bucket
云存储服务 2.1 为网站打开属性 属性和权限设置 设置bucket属性,打开功能:Static website hosting(静态网站托管) 设置bucket权限,Permissions ---- ...
- WebStorm在Font设置FontSize无效解决方法
我的WebStorm设置了主题.所以直接在File-Settings-Editor-Font设置了无效.它会提醒你要在主题里面改.主题在哪里呢 找到File-Settings-Editor-Color ...
- Java基本数据类型转换及运算符
上次我们说到完了Java中的基本数据类型,今天我们来说说Java中的基本数据类型转换和Java中的运算符 基本数据类型转换 java中可以从任意基本数据类型转型到外的基本数据类型 注意:(boolea ...
- python + selenium 环境搭建及问题
搭建平台windows 准备工具如下: ------------------------------------------------------------- 下载python https://w ...
- Android组件化路由实践
Android应用组件化各个组件页面之间要实现跳转使用路由是一个很好的选择.本文将实现一个比较轻量级的路由组件,主要涉及以下知识: Annotation (声明路由目标信息) AnnotationPr ...
- APP设计与开发(ui篇)
这篇文章是我个人在开发与设计APP的所用的一些设计思路,仅用于和大家分享知识并不是什么设计标准之类的.主要说明App的开发中是如何来开发与组织UI部分. UI模块结构 在项目中建立ui包用于存放ui类 ...
- Java初中级面试笔记及对应视频讲解
笔试题链接:点击打开链接 密码:提取码:7h9e 视频下载链接: 点击打开链接 提取码:hyye 百万it课程 https://pan.baidu.com/s/1ldJ_Ak7y0VL5Xmy9 ...
- wordpress访问速度慢
可能是google字体的原因 1.找到wordpress目录下wp-includes/script-loader.php这个文件 2.查找open_sans_font_url这个字段,把后面的font ...