前面的章节,讲解了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的更多相关文章

  1. SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache

    前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...

  2. Spring Boot集成Spring Data Reids和Spring Session实现Session共享

    首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...

  3. Spring boot集成spring session实现session共享

    最近使用spring boot开发一个系统,nginx做负载均衡分发请求到多个tomcat,此时访问页面会把请求分发到不同的服务器,session是存在服务器端,如果首次访问被分发到A服务器,那么se ...

  4. Spring Boot 集成spring security4

    项目GitHub地址 : https://github.com/FrameReserve/TrainingBoot Spring Boot (三)集成spring security,标记地址: htt ...

  5. Spring Boot 集成 Spring Security 实现权限认证模块

    作者:王帅@CodeSheep   写在前面 关于 Spring Security Web系统的认证和权限模块也算是一个系统的基础设施了,几乎任何的互联网服务都会涉及到这方面的要求.在Java EE领 ...

  6. Spring boot 集成Spring Security

    依赖jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...

  7. SpringBoot系列:Spring Boot集成Spring Cache

    一.关于Spring Cache 缓存在现在的应用中越来越重要, Spring从3.1开始定义了org.springframework.cache.Cache和org.springframework. ...

  8. Spring Boot 集成 Spring Security

    1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  9. Spring Boot 集成 Spring Security 入门案例教程

    前言 本文作为入门级的DEMO,完全按照官网实例演示: 项目目录结构 Maven 依赖 <parent> <groupId>org.springframework.boot&l ...

随机推荐

  1. HABSE表结构理解

    也分为行列,行是索引,锁定数据,查找数据只能通过行 列:建表时必须知道列族,真实列(列簇)在插入数据时候可以指定        查找指定列,必须带列族,列族1:name

  2. Java第三次作业第四题

    4. [问题描述]读取一个文本文件in.txt,然后将这个文件的内容,反序写到另一个文件out.txt中. [输入形式]从文件in.txt中读信息. [输出形式]反序内容写入out.txt文件中. [ ...

  3. charles 客户端进程

    本文参考:charles 客户端进程 客户端进程工具/client_process 显示使每个请求的本地客户端进程; 客户端进程工具显示负责进行每个请求的本地客户端进程的名称. 客户端进程通常是您的W ...

  4. 上传本地Jar包到阿里云的云效私有仓库

    一.前言 前2天玩了一下上传本地jar到maven中央仓库,随后我们老大又给我说了一个叫云效的私有仓库也可以玩,小编试了一下,果然很舒服,配置很简单,效率很高,几分钟就能搞定,只需要自己有个阿里云的账 ...

  5. 【pymongo.errors】Cursor not found

    pymongo.errors.CursorNotFound: Cursor not found 故事背景:先从数据库中取得所有数据 db['test'].find(),然后对结果进行for循环,但是当 ...

  6. (一)spring 高级装配-@Profile

    1.环境与profile 示例:数据库配置 a:通过@Bean注解,通过EmbeddedDatabaseBuilder创建数据源 @Bean(destroyMethod="shutdown& ...

  7. C# 反射基础用法

    1.引用命名空间 using System.Reflection; 2.object[] parameters = { 参数 }; 调用方法的参数数组 3.Type t = Type.GetType( ...

  8. SVN检出后文件没有图标显示

    SVN检出后文件没有图标显示   "Win + R"打开运行框,输入"regedit"打开注册表 在注册表编辑界面按"Ctrl + F"快捷 ...

  9. Winform组合ComboBox和TreeView实现ComboTree

    最近做Winform项目需要用到类似ComboBox的TreeView控件. 虽然各种第三方控件很多,但是存在各种版本不兼容问题.所以自己写了个简单的ComboTreeView控件. 下图是实现效果: ...

  10. redis-分布式锁-刷新信号量

    为什么需要刷新信号量呢,因为信号量有过期时间: 为什么信号量需要过期时间呢,因为需要利用超时特性,解决分布式锁存在的一些固有缺陷. 而对于类似流式API来说,一般10秒的过期时间是远远不够的.因此我们 ...