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

springboot 配置redis的最简使用示例

上一篇:tp5.0 创建数据的全量索引


下一篇:SpringBoot整合Elasticsearch,2021最新Java面试真题解析