spring-boot缓存测试
来源:互联网 发布:查询数字顺序软件 编辑:程序博客网 时间:2024/06/08 08:40
需求描述
对于频繁访问的数据,为了减少生成数据所需要的成本,我们有时需要对这些频繁访问的数据做缓存。对于数据缓存,有很多开源包可以实现,如EHCache ,JCache ,ShiftOne 等。以前做过缓存相关的东西,都是利用一些开源缓存工具,自己写一个单例类来实现定时缓存功能。这次测试,我们通过使用spring-boot中集成的缓存工具,利用注解方式实现数据缓存。经过测试后,发现spring-boot对缓存的支持已经十分完善,而且支持的缓存技术也很多。使用起来也非常方便。
实现
本次测试使用的是spring-boot中集成中谷歌的缓存技术:guava。
使用时只需要引入一些pom依赖,在启动类中增加一个@EnableCaching注解,然后在需要缓存的方法中加一个@Cacheable(“books”)注解即可。关于缓存时间和缓存大小,可以直接在application.yml中配置。
maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency>
代码
@SpringBootApplication@EnableCachingpublic class Application { public static void main(String[] args){ SpringApplication.run(Application.class,args); }}
controller层
@RequestMapping(value = "/cache/test",method = RequestMethod.GET) @ApiOperation(value = "缓存测试",notes = "") public Book getBook(@RequestParam()String bookName){ return bookCache.getBook(bookName); }
service层
@Servicepublic class BookCache { public static Integer index = 0; @Cacheable("books") public Book getBook(String bookName){ index = index + 1; Book book = new Book(bookName,index); return book; }}
在实体Book中,我们重写构造器,每次创建新的对象时,都会打印一下输出一条内容,方便我们测试
public class Book { public Book(String bookName,Integer index){ System.out.println("创建一本书"); this.bookName = bookName; this.createDate = new Date(); this.index = index; } private String bookName; private Date createDate; private Integer index; public Integer getIndex() { return index; } public void setIndex(Integer index) { this.index = index; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; }}
配置文件
spring: profiles: active: dev jackson: serialization-inclusion: NON_NULL date-format: yyyy-MM-dd'T'HH:mm:ss time-zone: Asia/Shanghai cache: guava: # 缓存名称 cache-names: books #缓存最大数量500条, 缓存失效时间 10s spec: maximumSize=500,expireAfterWrite=5s
对于配置文件就不说太多了,都很明显了。cache-names: books要与方法上使用的那个name保持一致。
测试及总结
1、我们在controller中调用该请求,从第一次调用开始,前10s内容,只有一次输出“创建一本书”,10s以后,再调用,就会再次出现“创建一本书”。
2、配置文件中直接可以配置缓存时间,个人觉得比自己写要方便太多,再次膜拜一下大神们造的轮子。
3、测试相关代码下载地址
spring-boot缓存测试
需求描述
对于频繁访问的数据,为了减少生成数据所需要的成本,我们有时需要对这些频繁访问的数据做缓存。对于数据缓存,有很多开源包可以实现,如EHCache ,JCache ,ShiftOne 等。以前做过缓存相关的东西,都是利用一些开源缓存工具,自己写一个单例类来实现定时缓存功能。这次测试,我们通过使用spring-boot中集成的缓存工具,利用注解方式实现数据缓存。经过测试后,发现spring-boot对缓存的支持已经十分完善,而且支持的缓存技术也很多。使用起来也非常方便。
实现
本次测试使用的是spring-boot中集成中谷歌的缓存技术:guava。
使用时只需要引入一些pom依赖,在启动类中增加一个@EnableCaching注解,然后在需要缓存的方法中加一个@Cacheable(“books”)注解即可。关于缓存时间和缓存大小,可以直接在application.yml中配置。
maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency>
代码
@SpringBootApplication@EnableCachingpublic class Application { public static void main(String[] args){ SpringApplication.run(Application.class,args); }}
@RequestMapping(value = "/cache/test",method = RequestMethod.GET) @ApiOperation(value = "缓存测试",notes = "") public Book getBook(@RequestParam()String bookName){ return bookCache.getBook(bookName); }
service层
@Servicepublic class BookCache { public static Integer index = 0; @Cacheable("books") public Book getBook(String bookName){ index = index + 1; Book book = new Book(bookName,index); return book; }}
在实体Book中,我们重写构造器,每次创建新的对象时,都会打印一下输出一条内容,方便我们测试
public class Book { public Book(String bookName,Integer index){ System.out.println("创建一本书"); this.bookName = bookName; this.createDate = new Date(); this.index = index; } private String bookName; private Date createDate; private Integer index; public Integer getIndex() { return index; } public void setIndex(Integer index) { this.index = index; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; }}
配置文件
spring: profiles: active: dev jackson: serialization-inclusion: NON_NULL date-format: yyyy-MM-dd'T'HH:mm:ss time-zone: Asia/Shanghai cache: guava: # 缓存名称 cache-names: books #缓存最大数量500条, 缓存失效时间 10s spec: maximumSize=500,expireAfterWrite=5s
对于配置文件就不说太多了,都很明显了。cache-names: books要与方法上使用的那个name保持一致。
测试及总结
1、我们在controller中调用该请求,从第一次调用开始,前10s内容,只有一次输出“创建一本书”,10s以后,再调用,就会再次出现“创建一本书”。
2、配置文件中直接可以配置缓存时间,个人觉得比自己写要方便太多,再次膜拜一下大神们造的轮子。
3、测试相关代码下载地址
- spring-boot缓存测试
- spring boot 缓存@EnableCaching
- spring-boot的缓存
- Spring-Boot单元格测试
- spring boot 测试 单元测试
- Spring boot 测试
- Spring boot测试
- spring boot 集成测试
- Spring boot 测试
- Spring Boot系列(十八) 缓存
- Spring Boot Redis 数据缓存
- 9、spring-boot缓存支持
- spring boot之缓存Redis
- Spring Boot 数据缓存 Cache
- Spring Boot缓存实战 EhCache
- Spring Boot缓存实战 Redis
- spring boot redis注解缓存。
- Spring boot 使用 Redis 缓存
- hadoop配置
- Redis【五大基本类型】
- MATLAB使用eval函数批量修改文件名
- html5 canvas清空画布方法
- ActiveMQ安装步骤
- spring-boot缓存测试
- SpringMVC学习,总结
- 最小生成树,Kruskal算法+并查集,最舒适路线
- 6.16与上上题一样的题
- Ijkplayer直播App卡顿问题分析
- Android平台ROM的定制及精简教程
- 设计模式工厂模式
- Linux趣事 -- (2.3)学生信息管理系统
- 《算法图解》第三章选择排序