这里介绍Spring Boot结合JPA,MySQL和Ehcache实现缓存功能,提高程序访问效率。

一、Maven依赖

  

<!-- caching -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>

二、程序的启动类加上 @EnableCaching

3、application.yml和ehcache.xml配置文件 

application.yml
#使用ehcache缓存配置
spring:
cache:
type: ehcache
ehcache:
config: classpath:ehcache.xml
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<cache name="sysCache"
maxElementsInMemory="100000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="true"
memoryStoreEvictionPolicy="LRU"/>
<defaultCache
eternal="false"
maxElementsInMemory="10000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
4、使用 

注意:
1)@CacheConfig(cacheNames = {“lemonCache”})设置了ehcache的名称,这个名称就是ehcache.xml内的名称; (可以不指定,应为在yml 中已经制定了)
2)@Cacheable:应用到读取数据的方法上,即可缓存的方法,如查找方法:先从缓存中读取,如果没有再调 用方法获取数据,然后把数据添加到缓存中,适用于查找;
3)@CachePut:主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用。适用于更新和插入;
4)@CacheEvict:主要针对方法配置,能够根据一定的条件对缓存进行清空。适用于删除。

整合reids   https://blog.csdn.net/plei_yue/article/details/79362372

最新文章

  1. docker本地私有仓库的创建,及https错误修正
  2. [No000037]操作系统Operating Systems操作系统历史与硬件概况History of OS &amp; Summaries!
  3. LeetCode----66. Plus One(Java)
  4. 10 个最适合 Web 和 APP 开发的 NodeJS 框架
  5. 【python cookbook】【字符串与文本】5.查找和替换文本
  6. DevExpress控件使用系列--ASPxGridView+Popup+Tab
  7. 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
  8. Data Mining 概念
  9. struts2(四)之输入校验
  10. python正则表达式判断素数【厉害了】
  11. 关于在IE浏览器中使用控件问题
  12. Egret飞行模拟-开发记录02
  13. 27-5-LTDC控制LCD显示屏
  14. 960网格,一个web时代的标志。
  15. 跟我学SharePoint 2013视频培训课程——使用垃圾箱(5)
  16. activiti 数据表设计
  17. Performs the analysis process on a text and return the tokens breakdown of the text
  18. KMP + 求最小循环节 --- POJ 2406 Power Strings
  19. 【RF库测试】对出错的处理
  20. 读书笔记之JavaScript中的数据类型

热门文章

  1. Vue router link
  2. 7.Java集合-Arrays类实现原理及源码分析
  3. python连接impala时,执行SQL报错expecting list of size 2 for struct args
  4. k2系列-安装篇
  5. Linux中找回误删除的文件
  6. 利用CodeBlocks结合freeglut快速搭建OpenGL开发环境
  7. vue sample
  8. Java锁--Condition
  9. BZOJ2301——莫比乌斯&amp;&amp;整除分块
  10. P4461 [CQOI2018]九连环