当前位置:   article > 正文

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

springboot可用的缓存技术

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

可以在自己编写的配置类或者启动类上加上注解@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>
  • 1
  • 2
  • 3
  • 4
  • 5

配置文件不需要做什么修改。启动类依然加上 @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))
  • 1
  • 2

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

启动类开启缓存

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

使用相关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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

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

@Cached( name = "category:", key = "#categoryId", expire = 100, cacheType = CacheType.REMOTE, keyConvertor = KeyConvertor.FASTJSON, serialPolicy = SerialPolicy.JAVA)
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/704111
推荐阅读
相关标签
  

闽ICP备14008679号