springboot中@Cache和redis的使用

1导入redis依赖

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2.配置redis

spring:
  datasource:
    url: jdbc:mysql:///sx
    username: root
    password:
    driver-class-name: com.mysql.jdbc.Driver
#  cache:
#    type: redis

  redis:
    host: 127.0.0.1
    port: 6379

3.启动类上开启缓存

@SpringBootApplication
@MapperScan("com.upb.dao")
@EnableCaching //开启缓存
public class UpbApplication {

    public static void main(String[] args) {
        SpringApplication.run(UpbApplication.class, args);
    }

}

4.编写控制器层

@Controller
@Api( tags = "登录接口")
public class LoginController {
    @Autowired
    Result result;
    @Autowired
    UserService userService;


    @ResponseBody
    //说明是什么方法(可以理解为方法注释)
    @RequestMapping("/login")
    @Cacheable(value = "r-test")
    public Result login( @RequestBody User user){
        System.out.println(userService.getAll());
        result.Successful("cg");
        return result;
    }

}

5.由于刚学redis,没用过注解,百度了很多,最后自己才知道@Cacheable中的value=“你redis中的key”‘,就相当于给你主动存到了redis中。

 欧克记录一下,其它的详细作用可以看看其他文档。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值