Spring Cache
Spring Cache 是 Spring 自带的缓存方案,使用简单,既可以使用本地缓存,也可以使用 Redis 导包:
<-- 当然也可以用redisson -->
启动类开启缓存
// 开启在方法上使用缓存注解
@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)