@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
@Service@CacheConfig(cacheNames = "role")public class RoleServiceImpl implements IRoleService {/ * .@Cacheable:* 1、方法运行之前,先去查询Cache(缓存组件),按照cacheNames指定的名字获取;* (CacheManager先获取相应的缓存),第一次获取缓存如果没有Cache组件会自动创建。* 2、去Cache中查找缓存的内容,使用一个key,默认就是方法的参数;* key是按照某种策略生成的;默认是使用keyGenerator生成的,默认使用SimpleKeyGenerator生成key;* SimpleKeyGenerator生成key的默认策略;* 如果没有参数;key=new SimpleKey();* 如果有一个参数:key=参数的值* 如果有多个参数:key=new SimpleKey(params);* 3、没有查到缓存就调用目标方法;* 4、将目标方法返回的结果,放进缓存中* <p>* <p>* 核心:* 1)、使用CacheManager【ConcurrentMapCacheManager】按照名字得到Cache【ConcurrentMapCache】组件* 2)、key使用keyGenerator生成的,默认是SimpleKeyGenerator* <p>* 几个属性:* a)cacheNames/value:指定缓存组件的名字* cacheNames = {"role"}可以使用多个参数,是数组的形式,可以指定多个缓存* b)key:缓存数据使用的key,可以用他来指定。默认是使用方法参数的值* 编写SpEl: #id #a0,#po,#argrs[0] "0"代表参数的索引* #result 方法执行后的返回值* #root.methodName 方法名* key = "#root.methodName+'['+#id+']'"* c)keyGenerator: key的生成器、可以自己指定key的生成器的组件Id* key/keyGenerator 二选一* keyGenerator = "myKeyGenerator"* d)cacheManager:指定缓存管理器或者cacheResolver:获取解析器* cacheManager/cacheResolver 二选一* e)condition:指定符合缓存的条件* condition = "#id>0 and #root.methodName eq 'aaa'" 可以多条件判断* f)unless: 否定缓存,当unless的条件为true,方法结果不会被缓存,可以获取结果进行判断* unless = "#result==null",结果为null,就不缓存* g)sync:是否使用异步模式* 默认false 同步* 为true时,unless不支持*/@Autowiredprivate RoleMapper roleMapper;@Override@Cacheable(key = "#id",condition = "#id>0",unless = "#result==null")public Role findById(Integer id) {return roleMapper.selectByPrimaryKey(id);}// @Cacheable(keyGenerator = "myKeyGenerator",key = "findAllRole")@Cacheable(key = "#root.method.getName()")@Overridepublic CommonResult findAllRole() {List<Role> roleList = roleMapper.findAll();return CommonResult.success(roleList);}}
@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缓存层 
