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中的存储
    springboot~Cache注解缓存在代码中的获取

Original: https://www.cnblogs.com/lori/p/16673580.html
Author: 张占岭
Title: springboot~Cache注解缓存在代码中的获取

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/576058/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球