Spring Cache的基本使用与分析

概述

使用 Spring Cache 可以极大的简化我们对数据的缓存,并且它封装了多种缓存,本文基于 redis 来说明。

基本使用

1、所需依赖

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

2、配置文件

spring:
  # redis连接信息
  redis:
    host: 192.168.56.10
    port: 6379
  cache:
    # 指定使用的缓存类型
    type: redis
    # 过期时间
    redis:
      time-to-live: 3600000
      # 是否开启前缀,默认为true
      use-key-prefix: true
      # 键的前缀,如果不配置,默认就是缓存名cacheNames
      key-prefix: CACHE_
      # 是否缓存空置,防止缓存穿透,默认为true
      cache-null-values: true

3、Spring Cache 提供的注解如下,使用方法参见:官方文档,通过这些注解,我们可以方便的操作缓存数据。

  • @Cacheable:触发缓存写入的操作
  • @CacheEvict:触发缓存删除的操
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值