spring cache注解的使用:
@CacheConfig 类级别的缓存注解,允许共享缓存名称
@Caching 将多种缓存操作分组
@Cacheable 触发缓存入口
@CacahePut 更新缓存
@CacheEvict 触发移除缓存
最常用的为最后三个:@Cacheable、@CacheEvict、@CacheEvict
@CacheConfig:
主要用于配置该类中会用到的一些共用的缓存配置。在这里@CacheConfig(cacheNames = "role"):配置了该数据访问对象中返回的内容将存储于名为role的缓存对象中,我们也可以不使用该注解,直接通过@Cacheable自己配置缓存集的名字来定义。
@Service
@CacheConfig(cacheNames = "role")
public class RoleService {
@Caching:
主要可以让我们在一个方法或者类上同时指定多个Spring Cache相关的注解。其拥有三个属性:cacheable、put和evict,分别用于指定@Cacheable、@CachePut和@CacheEvict。
@Caching(
cacheable = @Cacheable("role"),
evict = { @CacheEvict("role2"),
@CacheEvict(value = "role", allEntries = true)
},
put = {
@CachePut(value = "role", key = "#role.id"),
@CachePut(value = "role", key = "#role.name"),
@CachePut(value = "role", key = "#role.account")
}
)
public Role find(Role role) {
return null;
}
@Cacheable:
一般用于查询操作,根据key查询缓存.
- 如果key不存在,查询db,并将结果更新到缓存中。
- 如果key存在,直接查询缓存中的数据。
- 需要注意的是当一个支持缓存的方法在对象内部被调用时是不会触发缓存功能的。
- @Cacheable可以指定三个属性,value、key和condition
4.1 value属性指定Cache名称(如果通过@CacheConfig设置了缓存区域,此处可省略)
value属性是必须指定的,其表示当前方法的返回值是会被缓存在哪个Cache上的,对应Cache的名称。其可以是一个Cache也可以是多个Cache,当需要指定多个Cache时其是一个数组。
@Cacheable("cache1")//Cache是发生在cache1上的
public Role find(Integer id) {
returnnull;
}
@Cacheable({"cache1", "cache2"})//Cache是发生在cache1和cache2上的
public Role find(Integer id) {
returnnull;
}
4.2 使用key属性自定义key
key属性是用来指定Spring缓存方法的返回结果时对应的key的。该属性支持SpringEL表达式。当我们没有指定该属性时,Spring将使用默认策略生成key。
4.2.1 默认生成策略:(也可自定义默认生成策略,通过配置文件,没用过,可自行百度)
默认的key生成策略是通过KeyGenerator生成的,其默认策略如下:
- 如果方法没有参数,则使用0作为key。
- 如果只有一个参数的话则使用该参数作为key。
- 如果参数多于一个的话则使用所有参数的hashCode作为key。
4.2.2 自定义策略:
自定义策略是指我们可以通过Spring的EL表达式来指定我们的key。这里的EL表达式可以使用方法参数及它们对应的属性。使用方法参数时我们可以直接使用“#参数名”或者“#p参数index”。下面是几个使用参数作为key的示例:
@Cacheable(value="role", key="#id")
public Role find(Integer id) {
return null;
}
@Cacheable(value="role", key="#p0")
public Role find(Integer id) {
return null;
}
@Cacheable(value="role", key="#role.id")
public Role find(Role role) {
return null;
}
@Cacheable(value="role", key="#p0.id")
public Role find(Role role) {
return null;
}
除了上述使用方法参数作为key之外,Spring还为我们提供了一个root对象可以用来生成key。通过该root对象我们可以获取到以下信息。
属性名称 |
描述 |
示例 |
methodName |
当前方法名 |
#root.methodName |
method |
当前方法 |
#root.method.name |
target |
当前被调用的对象 |
#root.target |
targetClass |
当前被调用的对象的class |
#root.targetClass |
args |
当前方法参数组成的数组 |
#root.args[0] |
caches |
当前被调用的方法使用的Cache |
#root.caches[0].name |
当我们要使用root对象的属性作为key时我们也可以将“#root”省略,因为Spring默认使用的就是root对象的属性。如:
@Cacheable(value={"role", "xxx"}, key="caches[1].name")
public Rolefind(Role role) {
return null;
}
4.3 condition 属性指定发生的条件
有的时候我们可能并不希望缓存一个方法所有的返回结果。通过condition属性可以实现这一功能。condition属性默认为空,表示将缓存所有的调用情形。其值是通过SpringEL表达式来指定的,当为true时表示进行缓存处理;当为false时表示不进行缓存处理,即每次调用该方法时该方法都会执行一次。如下示例表示只有当role不为 null 时才会进行缓存
@Cacheable(value = "role",key = "#roleId",condition = "#role != null")
public Role selectRole(Integer roleId){
Role role = new Role();
role.setId(roleId);
return roleMapper.selectOne(role);
}
@CacahePut
@CachePut也可以声明一个方法支持缓存功能。与@Cacheable不同的是使用@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。
@CachePut也可以标注在类上和方法上。使用@CachePut时我们可以指定的属性跟@Cacheable是一样的
@CachePut(value = "role",key = "#role.id",condition = "#role != null")
public Role operateRole(Role role){
Integer count = 0;
if(role.getId() == null){
count = roleMapper.insertSelective(role);
}else{
count = roleMapper.updateByPrimaryKeySelective(role);
}
if(count > 0){
return roleMapper.selectByPrimaryKey(role.getId());
}else{
return null;
}
}
@CacheEvict
@CacheEvict是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。@CacheEvict可以指定的属性有value、key、condition、allEntries和beforeInvocation。其中value、key和condition的语义与@Cacheable对应的属性类似。下面我们来介绍一下新出现的两个属性allEntries和beforeInvocation:
1.allEntries:表示是否需要清除缓存中所有的元素(非必需,默认为false。当为true时,会移除所有数据)
2.beforeInvocation:清除操作默认是在方法成功执行之后触发的(非必需,默认为false,会在调用方法之后移除数据。当为true时,会在调用方法之前移除数据)
@CacheEvict(value = "role",key = "#roleId",allEntries = true,beforeInvocation = true)
public Integer deleteRole(Integer roleId){
return roleMapper.deleteByPrimaryKey(roleId);
}