springboot~Cache注解缓存在代码中的获取
对于springboot中基于方法的缓存Caching来说,我们直接以声明的方式添加,删除这些缓存,而它们在redis这种持久化产品中,通过value::key
的方法组成一个redis.key,在业务方法中,可以通过这种key来手动获取它们。
- 注解声明
@CacheEvict(value = CacheConstants.USER_DETAILS, key = "#sysUser.username")
public Boolean removeUserById(SysUser sysUser) {
sysUserRoleMapper.deleteByUserId(sysUser.getUserId());
this.removeById(sysUser.getUserId());
return Boolean.TRUE;
}
- 业务代码中获取
Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS);
if (cache != null && cache.get(username) != null) {
return (PigUser) cache.get(username).get();
}
- redis中的存储