springboot 配置redis的最简使用示例
作者:互联网
pom.xml的配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
配置文件 application.yml
spring:
redis:
database: 13
host: 127.0.0.1
port: 6379
jedis:
pool:
max-active: 100
max-idle: 100
min-idle: 10
max-wait: 1000ms
controller
@GetMapping("/gpage")
public ModelAndView showGoods(Long goodsId){
ModelAndView mav = new ModelAndView("/goods");
logger.info(“goodsId—>>>”+goodsId);
Goods goods = goodsService.findById(goodsId);
List<GoodsCover> covers = goodsCoverService.findByGoodsId(goodsId);
List<GoodsParam> params = goodsParamService.findByGoodsId(goodsId);
List<GoodsDetail> details = goodsDetailService.findByGoodsId(goodsId);
mav.addObject("goods",goods);
mav.addObject("covers", covers);
mav.addObject("details", details);
mav.addObject("params", params);
return mav;
}
在service中使用redis缓存
@Cacheable(value=“goods” , key = “#goodsId”)
public Goods findById(Long goodsId){
return goodsDao.findById(goodsId);
}
注意启动类添加@EnableCaching
标签:addObject,最简,goods,springboot,mav,boot,示例,redis,goodsId 来源: https://blog.csdn.net/zgx20130619/article/details/106536500