需求描述
对于频繁访问的数据,为了减少生成数据所需要的成本,我们有时需要对这些频繁访问的数据做缓存。对于数据缓存,有很多开源包可以实现,如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
@EnableCaching
public 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层
@Service
public 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
@EnableCaching
public 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层
@Service
public 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、配置文件中直接可以配置缓存时间,个人觉得比自己写要方便太多,再次膜拜一下大神们造的轮子。