SpringBoot的三种缓存技术(Spring Cache、Layering Cache 框架、Alibaba JetCache 框架)

Spring Cache

Spring Cache 是 Spring 自带的缓存方案,使用简单,既可以使用本地缓存,也可以使用 Redis
导包:

<dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis</artifactId>
          <version>3.2.5</version>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-cache</artifactId>
          <version>3.1.1</version>
      </dependency>
      <-- 当然也可以用redisson -->
<!--        <dependency>-->
<!--            <groupId>org.redisson</groupId>-->
<!--            <artifactId>redisson</artifactId>-->
<!--            <version>3.30.0</version>-->
<!--        </dependency>

可以在自己编写的配置类或者启动类上加上注解@EnableCaching
在这里插入图片描述
二选一在这里插入图片描述
同时也可以补充相关的配置:
在这里插入图片描述
后面就可以在方法上使用了,例如:
在这里插入图片描述

触发将数据保存到缓存的操作:
@Cacheable: Triggerscache population.:cxche eviction.:触发将数据从缓存删除的操作
@CacheEvict: Triggers
@CachePut: Updates the cache without interfering with the method execution.:不影响方法执行更新缓存
@Caching: Regroups multiple cache operations to be applied on a method.:组合以上多个操作@Cacheconfig:Shares some common cache-related settings at class-level.:在类级别共享存的相同配置

Layering Cache 框架

导入依赖:

<dependency>
  <groupId>com.github.xiaolyuh</groupId>
  <artifactId>layering-cache-starter</artifactId>
  <version>版本号</version>
</dependency>

配置文件不需要做什么修改。启动类依然加上 @EnableCaching 注解。
然后使用 layering 包中的 @Cacheable @CachePut @CatchEvict 三个注解来替换 Spring Cache 的默认注解,例如:

@Cacheable(value = "user", key = "#userId",firstCache = @FirstCache(expireTime = 5, timeUnit = TimeUnit.MINUTES),
 secondaryCache = @SecondaryCache(expireTime = 10, preloadTime = 3, forceRefresh = true, isAllowNullValue = true, timeUnit = TimeUnit.MINUTES))

Alibaba JetCache 框架

阿里的这个缓存框架相比于Spring的自带缓存来说是更加方便,功能更加的完善,提供两级缓存和异步cacheAPI操作,以及其他的TTL等。使用起来可以说是让开发者如鱼得水,接下来简单地说一说它的使用及可能出现的问题。
首先还是需要引进jetcache的包,必不可少的流程走一走:

<!--jetcache缓存-->
	<dependency>
		<groupId>com.alicp.jetcache</groupId>	
		<artifactId>jetcache-starter-redis</artifactId>
		<version>2.5.11</version>
	</dependency>
<!-- 如果出现版本包问题,则需要引入-->
	<dependency>
		<groupId>redis.clients</groupId>
		<artifactId>jedis</artifactId>
		<version>2.9.1</version>
	</dependency>

启动类开启缓存

// 开启在方法上使用缓存注解
@EnableMethodcache(basePackages =**.***.***")
// 开启使用注解方式创建cache
@EnableCreatecacheAnnotation

使用相关yml配置:

jetcache:
  statIntervalMinutes: 15 # 统计间隔,默认0:表示不统计
  areaInCacheName: false  # areaName是否作为缓存key前缀,默认True
  local:
    default: # 默认default,可以配置更多的area
      type: linkedhashmap # 已支持可选:linkedhashmap、caffeine
      keyConvertor: fastjson # key转换器
  remote:
    default:
      type: redis
      keyConvertor: fastjson
      valueEncoder: java # 序列化器,只有remote需要
      valueDecoder: java # 序列化器,只有remote需要
      poolConfig:
        minIdle: 5
        maxIdle: 20
        maxTotal: 50
      host: 127.0.0.1
      port: 6379

配置说明
在这里插入图片描述
之后就可以在方法,或者接口上面使用了,例如

@Cached( name = "category:", key = "#categoryId", expire = 100, cacheType = CacheType.REMOTE, keyConvertor = KeyConvertor.FASTJSON, serialPolicy = SerialPolicy.JAVA)
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值