前面的章节,讲解了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

最新文章

  1. CentOS 6.5 安全加固及性能优化 (转)
  2. Git删除tag
  3. HDU 2087 kmp模板题
  4. 当类库项目中无法使用Application.StartupPath
  5. HDU1106
  6. linux 系统中的特殊文件
  7. 抓包分析YY音频
  8. 基于layUI实现前端分页功能
  9. uva10410 栈
  10. css 字体两端对齐
  11. gtest 参数化
  12. Java Swing 界面中文乱码问题解决(Idea环境)
  13. SpringBoot跨域问题
  14. spring深入学习(四)-----spring aop
  15. 用 Homebrew 带飞你的 Mac
  16. Java删除文件夹和其子文件、文件的拷贝和剪切
  17. Python-递加计数器
  18. HDU2167(SummerTrainingDay02-D 状态压缩dp)
  19. Wannafly挑战赛9 A - 找一找
  20. html5和CSS3中新特性

热门文章

  1. 【学习笔记】第八章 python3核心技术与实践--条件与循环
  2. HTML定位——绝对定位和相对定位、固定定位
  3. Session和Cookie的用法及区别
  4. spring中基于注解使用ehcache
  5. 【linux】【jenkins】自动化运维五 整合邮件提醒
  6. SpringBoot之简单入门
  7. RxSwift 入门
  8. JSR303 后端校验包的使用
  9. opencv边缘检测
  10. 如何把安全证书导入到java中的cacerts证书库(转)