spring cache学习 - @CacheEvict使用

作用:清除缓存

value:缓存位置名称,不能为空
key:缓存的key,默认为空
condition:触发条件,只有满足条件的情况才会清除缓存,默认为空,支持SpEL
allEntries:true表示清除value中的全部缓存,默认为false

demo用例

//allEntries是boolean类型,表示是否需要清除缓存中的所有元素.默认为false,表示不需要.
//当指定了allEntries为true时,Spring cache将忽略指定的key
//有的时候我们需要Cache清除所有的元素,这比一个一个清除元素更有效率
@CacheEvict(value="users", allEntries=true) 
public void delete(Integer id) {
	System.out.println("delete user by id: " + id);
}
    //清除掉指定key的缓存  
    @CacheEvict(value="andCache",key="#user.userId + 'findById'")  
    public void modifyUserRole(SystemUser user) {  
             System.out.println("hello andCache delete"+user.getUserId());  
    }  
      
    //清除掉全部缓存  
    @CacheEvict(value="andCache",allEntries=true)  
    public final void setReservedUsers(String[] reservedUsers) {  
        System.out.println("hello andCache deleteall");  
    }  

你可能感兴趣的:(java,spring,开发语言)