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