本文是在一次使用spring cache过程中遇到的一些问题,写一个简单的demo。以springboot做基本框架,使用声明式缓存cache来实现,基本靠注解实现,话不多说。先说配置
1、redis基本配置
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2、spring.redis配置
redis.host = 填写你自己的地址
redis.port = 6379
redis.password = 填写你自己的密码
redis.maxActive = 50
redis.maxIdle = 50
redis.testOnBorrow = true
redis.testWhileIdle = true
redis.maxWait = 60000
redis.timeout = 10000
redis.db = 0
3、srpingboot启动配置
@EnableCaching
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class,MongoAutoConfiguration.class,MongoDataAutoConfiguration.class,ActiveMQAutoConfiguration.class})
public class MtpApiApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MtpApiApplication.class, args);
}
4、接口类
我这里返回的是一个对象。具体的业务具体分析
/**
* 从缓存获取数据
* @param itemNo
* @return
*/
ItemProductStatisticsModel getStatistics(String itemNo);
5、实现类
记得加上@Cacheable接口。60*10表示过期时间,后面的是key,但不完全是key,具体的下面说,这个问题困扰了我好一会,常量类和实现类见如下
public class MtpApiRedisKey {
/**查询统计缓存key*/
public static final String KEY_CACHE_ITEM_VIEW_STATISTICS="#itemNo+'_'+'key_cache_item_view_statistics'";
}
@Override
@Cacheable(value = "test#60*10",key = MtpApiRedisKey.KEY_CACHE_ITEM_VIEW_STATISTICS)
public ItemProductStatisticsModel getStatistics(String itemNo) {
logger.info("从缓存中获取的数据为:{}",itemNo);
return new ItemProductStatisticsModel();
}
6、测试类
@Override
public DataResponse<ItemInfoDetailModel> getItemDetail(@ApiIgnore UserSessionInfo userInfo, String itemNo) {
ItemInfoDetailModel detailModel = itemInfoService.getItemDetail(itemNo);
//获取统计信息
int count = 0;
for(int i = 0 ; i < 5; i++){
logger.info("执行的次数为:{}",count++);
ItemProductStatisticsModel statisticsModel = itemInfoService.getStatistics(itemNo);
}
return new DataResponse<>(detailModel);
}
7、实验
通过5次循环调用itemInfoService.getItemDetail(itemNo)方法,我们来看实际执行了几次。
2018-11-10 17:18:46.961 [http-nio-8083-exec-1] INFO [bootstrap,626a13fda4de2ba2,626a13fda4de2ba2,false] com.controller.MtpItemInfoController - 执行的次数为:0
2018-11-10 17:18:48.161 [http-nio-8083-exec-1] INFO [bootstrap,626a13fda4de2ba2,626a13fda4de2ba2,false] com.service.impl.MtpItemInfoService - 从缓存中获取的数据为:I20181109161946289943
2018-11-10 17:18:49.663 [http-nio-8083-exec-1] INFO [bootstrap,626a13fda4de2ba2,626a13fda4de2ba2,false] com.controller.MtpItemInfoController - 执行的次数为:1
2018-11-10 17:18:55.435 [http-nio-8083-exec-1] INFO [bootstrap,626a13fda4de2ba2,626a13fda4de2ba2,false] com.controller.MtpItemInfoController - 执行的次数为:2
2018-11-10 17:18:55.741 [http-nio-8083-exec-1] INFO [bootstrap,626a13fda4de2ba2,626a13fda4de2ba2,false] com.controller.MtpItemInfoController - 执行的次数为:3
2018-11-10 17:18:56.050 [http-nio-8083-exec-1] INFO [bootstrap,626a13fda4de2ba2,626a13fda4de2ba2,false] com.controller.MtpItemInfoController - 执行的次数为:4
8、结论
从上图我们可以看出,加了cache之后,cache的类打印的日志只有一次:
从缓存中获取的数据为:I20181109161946289943
说明这次的缓存是使用成功的,最后来看下是否进了redis换缓存,通过redis管理工具来看,刚开始以为存入的是key是:I20181109161946289943_key_cache_item_view_statistics,结果找不到,后面通过其他渠道得知,正确的key是
test#60*10:I20181109161946289943_key_cache_item_view_statistics
至此,本丸通过实验完成cache的基本使用,其他注解的使用,目前还没用到,欢迎一起交流。