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

2025-07-25 19:23:45
admin

Spring Cache

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

org.springframework.boot

spring-boot-starter-data-redis

3.2.5

org.springframework.boot

spring-boot-starter-cache

3.1.1

<-- 当然也可以用redisson -->

com.alicp.jetcache

jetcache-starter-redis

2.5.11

redis.clients

jedis

2.9.1

启动类开启缓存

// 开启在方法上使用缓存注解

@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)

Copyright © 2088 2015女排世界杯_法国世界杯夺冠 - pgcnz.com All Rights Reserved.
友情链接