@Cacheable注解 从缓存中查找 如果没有就添加缓存

@Cacheable(cacheNames=”role”) 这里的cacheName表示key的key加冒号 也可以使用value表示
这个注解抽取在类名上@CacheConfig(cacheNames = “role”)

@Cacheable(key=”#id” ,condition = “#id>0”,unless = “#result==null” ) key为形参id 没有形参可以使用#root.method.getName()表示方法名 condition表示执行缓存的条件 unless表示等号后面为true时不执行缓存 #result表示查询的结果

value和cacheName表示key的key
key表示key
查询结果为redis的value

  1. @Service
  2. @CacheConfig(cacheNames = "role")
  3. public class RoleServiceImpl implements IRoleService {
  4. / * .@Cacheable:
  5. * 1、方法运行之前,先去查询Cache(缓存组件),按照cacheNames指定的名字获取;
  6. * (CacheManager先获取相应的缓存),第一次获取缓存如果没有Cache组件会自动创建。
  7. * 2、去Cache中查找缓存的内容,使用一个key,默认就是方法的参数;
  8. * key是按照某种策略生成的;默认是使用keyGenerator生成的,默认使用SimpleKeyGenerator生成key;
  9. * SimpleKeyGenerator生成key的默认策略;
  10. * 如果没有参数;key=new SimpleKey();
  11. * 如果有一个参数:key=参数的值
  12. * 如果有多个参数:key=new SimpleKey(params);
  13. * 3、没有查到缓存就调用目标方法;
  14. * 4、将目标方法返回的结果,放进缓存中
  15. * <p>
  16. * <p>
  17. * 核心:
  18. * 1)、使用CacheManager【ConcurrentMapCacheManager】按照名字得到Cache【ConcurrentMapCache】组件
  19. * 2)、key使用keyGenerator生成的,默认是SimpleKeyGenerator
  20. * <p>
  21. * 几个属性:
  22. * a)cacheNames/value:指定缓存组件的名字
  23. * cacheNames = {"role"}可以使用多个参数,是数组的形式,可以指定多个缓存
  24. * b)key:缓存数据使用的key,可以用他来指定。默认是使用方法参数的值
  25. * 编写SpEl: #id #a0,#po,#argrs[0] "0"代表参数的索引
  26. * #result 方法执行后的返回值
  27. * #root.methodName 方法名
  28. * key = "#root.methodName+'['+#id+']'"
  29. * c)keyGenerator: key的生成器、可以自己指定key的生成器的组件Id
  30. * key/keyGenerator 二选一
  31. * keyGenerator = "myKeyGenerator"
  32. * d)cacheManager:指定缓存管理器或者cacheResolver:获取解析器
  33. * cacheManager/cacheResolver 二选一
  34. * e)condition:指定符合缓存的条件
  35. * condition = "#id>0 and #root.methodName eq 'aaa'" 可以多条件判断
  36. * f)unless: 否定缓存,当unless的条件为true,方法结果不会被缓存,可以获取结果进行判断
  37. * unless = "#result==null",结果为null,就不缓存
  38. * g)sync:是否使用异步模式
  39. * 默认false 同步
  40. * 为true时,unless不支持
  41. */
  42. @Autowired
  43. private RoleMapper roleMapper;
  44. @Override
  45. @Cacheable(key = "#id",condition = "#id>0",unless = "#result==null")
  46. public Role findById(Integer id) {
  47. return roleMapper.selectByPrimaryKey(id);
  48. }
  49. // @Cacheable(keyGenerator = "myKeyGenerator",key = "findAllRole")
  50. @Cacheable(key = "#root.method.getName()")
  51. @Override
  52. public CommonResult findAllRole() {
  53. List<Role> roleList = roleMapper.findAll();
  54. return CommonResult.success(roleList);
  55. }
  56. }

@CacheEvict注解

在执行这个方法时 清除指定key的缓存 忽略key的key
一般在更新和删除数据时使用

    /**
     * .@CacheEvict 缓存清除
     *  key:指定要清除的数据
     *  allEntries:指定清除这个缓存库的所有数据,默认为false
     *  beforeInvocation:在执行方法之前清除,默认为false,在方法之后执行
     */
    @Override
    @CacheEvict(/*value = "role",*/key = "#id")
    public Integer delete(Integer id) {
        return roleMapper.deleteByPrimaryKey(id);
    }

@CachePut注解 还是会执行sql

缓存中有则更新缓存 无则添加缓存

key为key 忽略key的key

   /**
     * .@CachePut既调用方法、又更新数据,达到同步更新缓存
     * <p>
     * 运行时机:
     * 1、先调用目标方法
     * 2、将目标方法的结果缓存起来
     *
     * 条件:存取Id的key要保持一致
     *     key = "#role.id"     传入员工的Id
     *     key = "#result.id"   使用返回员工的Id
     * 注意: @Cacheable不能使用#result
     *      因为 @Cacheable在目标方法执行之前需要得到这个key,所以不能用#result
     */
    @Override
    @CachePut(key = "#result.id")
    public Role update(Role role) {
        roleMapper.updateByPrimaryKey(role);
        return role;
    }

@Caching注释 当一个方法要使用多个缓存功能

//添加user缓存的同时,移除userPage的缓存
@Caching(put =@CachePut(value = "user",key ="#userVo.id"),
evict = @CacheEvict(value = "userPage",allEntries = true))
    /**
     * .@Caching 定义复杂缓存规则
     */
    @Override
    @Caching(
            cacheable = {
                    @Cacheable(key = "#role.roleName")
            },
            put = {
                    @CachePut(key = "#role.id"),
                    @CachePut(key = "#role.roleCode")
            }
    )
    public CommonResult add(Role role) {
        role.setId(null);
        try {
            roleMapper.insert(role);
        } catch (Exception e) {
            return CommonResult.failed();
        }
        return CommonResult.success(role.getId());
    }

可以在控制层和服务层之间添加face缓存层
image.png