SpringBoot 缓存之 @Cacheable 详细介绍
https://blog.csdn.net/zl1zl2zl3/article/details/110987968
@Cacheable设置过期时间
https://blog.csdn.net/weixin_41860719/article/details/125226096
成功的demo
class TtlRedisCacheManager(cacheWriter: RedisCacheWriter?, defaultCacheConfiguration: RedisCacheConfiguration?) :
RedisCacheManager(cacheWriter!!, defaultCacheConfiguration!!) {
override fun createRedisCache(name: String, cacheConfig: RedisCacheConfiguration?): RedisCache {
var name: String? = name
var cacheConfig = cacheConfig
val cells: Array<String> = StringUtils.delimitedListToStringArray(name, "=")
name = cells[0]
if (cells.size > 1) {
val ttl = cells[1].toLong()
// 根据传参设置缓存失效时间,默认单位是秒
cacheConfig = cacheConfig!!.entryTtl(Duration.ofSeconds(ttl))
}
return super.createRedisCache(name, cacheConfig)
}
}
/**
* 自定义RedisCacheManager,用于在使用@Cacheable时设置ttl
*/
@Bean
fun selfCacheManager(redisConnectionFactory: RedisConnectionFactory?): RedisCacheManager? {
val redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory!!)
val redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(SerializationPair.fromSerializer(GenericJackson2JsonRedisSerializer(objectMapper)))
return TtlRedisCacheManager(redisCacheWriter, redisCacheConfiguration)
}
@Cacheable(value = ["default=60"], key = "#token", unless = "#result == null",cacheManager = "selfCacheManager")
fun getSession(token: String): SysUserSession? {
val session = sysUserSessionMapper.selectById(token)
return checkToken(session)
}
标签:Cacheable,name,val,cacheConfig,cells,ttl,使用
From: https://www.cnblogs.com/cjcn/p/17089231.html