《Spring实战》-第十三章:缓存数据(2)-在方法上使用缓存

慢来比较快,虚心学技术

 Spring 的缓存抽象在很大程度上是围绕切面构建的。在 Spring 中启用缓存时,会创建一个切面,它触发一个或更多的 Spring 的缓存注解,Spring提供的缓存注解主要有以下几个:

注  解

描  述

@Cacheable

表明 Spring 在调用方法之前,首先应该在缓存中查找方法的返回值。如果这个值能够找到,就会返回缓存的值。否则的话,这个方法就会被调用,返回值会放到缓存之中

@CachePut

表明 Spring 应该将方法的返回值放到缓存中。在方法的调用前并不会检查缓存,方法始终都会被调用

@CacheEvict

表明 Spring 应该在缓存中清除一个或多个条目

@Caching

这是一个分组的注解,能够同时应用多个其他的缓存注解

Ⅰ、填充缓存

由上述注解可知,@Cacheable@CachePut注解可以往缓存填充内容,两者的共有属性有:

属  性

类  型

描  述

value

String[]

要使用的缓存名称

condition

String

SpEL 表达式,如果得到的值是 false 的话,不会将缓存应用到方法调用上

key

String

SpEL 表达式,用来计算自定义的缓存 key

unless

String

SpEL 表达式,如果得到的值是 true 的话,返回值不会放到缓存之中

在最简单的情况下,在 @Cacheable 和 @CachePut 的这些属性中,只需使用 value 属性指定一个或多个缓存即可

上一篇文章中我们介绍了Spring整合Redis的过程,我们依旧使用Redis缓存了解Spring对缓存的抽象

我们事先编写一个BaseDao作为操作基准

@Component
public class BaseDao {
    
    /**
     * 根据id获取信息
     **/
    @Cacheable(value = "myCache")
    public String findOne(Integer id){
        System.out.println("执行findOne方法。。。。");
        return "我是BaseDao"+id;
    }
    
    /**
     * 根据id更改信息
     **/
    @CachePut(value = "myCache")
    public String save(Integer id){
        System.out.println("执行save方法。。。。。");
        return "BaseDao"+id;
    }
    
    /**
     * 根据id移除信息
     **/
    @CacheEvict(value = "myCache")
    public void remove(Integer id){
        System.out.println("执行remove方法。。。。。");
    }
}

编写测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {RedisCacheConfig.class})
public class AppTest {
    @Autowired
    private BaseDao baseDao;
}

测试使用@Cacheable存取数据

我们知道当缓存中没有对应数据的时候,会执行使用了@Cacheable注解的方法,并将结果存入缓存

如果缓存中已经存在对应数据,则直接将缓存数据返回:

@Test
public void testCacheAble(){
    System.out.println(this.baseDao.findOne(0));
    System.out.println(this.baseDao.findOne(0));
}

此处执行两次findOne(0),测试结果:

执行findOne方法。。。。
我是BaseDao0
我是BaseDao0

可以看到,此处只是实际上只有一次真正进入了findOne方法内,第二次从缓存中获取数据,以下是redis-cli中看到的结果:

在缓存中,缓存的key值默认为 缓存名称::传参值

测试使用@CachePut更新数据

由于使用@CachePut注解默认每次都会进入方法并使用返回值更新缓存,所以该注解在实际业务中一般用在更新数据的方法上

@Test
public void testCachePut(){
    this.baseDao.save(0);
    this.baseDao.save(0);
    System.out.println(this.baseDao.findOne(0));
}

测试结果:

执行save方法。。。。。
执行save方法。。。。。
BaseDao0

可以看到,此处两次执行save方法都进入了,执行save之后再调用findOne方法,依旧直接从缓存取值,缓存已更新

自定义缓存的key

@Cacheable @CachePut 都有一个名为 key 属性,这个属性能够替换默认的 key ,它是通过一个 SpEL 表达式计算得到的

表 达 式

描  述

#root.args

传递给缓存方法的参数,形式为数组

#root.caches

该方法执行时所对应的缓存,形式为数组

#root.target

目标对象

#root.targetClass

目标对象的类,是#root.target.class的简写形式

#root.method

缓存方法

#root.methodName

缓存方法的名字,是#root.method.name的简写形式

#result

方法调用的返回值(不能用在@Cacheable注解上)

#Argument

任意的方法参数名(如#argName)或参数索引(如#a0或#p0)

如将上述findOne()和save()方法缓存的key定义为BaseDao的class

@Cacheable(value = "myCache",key = "#root.targetClass")
public String findOne(Integer id){
    System.out.println("执行findOne方法。。。。");
    return "我是BaseDao"+id;
}

@CachePut(value = "myCache",key = "#root.targetClass")
public String save(Integer id){
    System.out.println("执行save方法。。。。。");
    return "BaseDao"+id;
}

再次执行测试testCacheAble()方法:

执行findOne方法。。。。
我是BaseDao0
我是BaseDao0

可以看到缓存如下:使用class com.my.spring.dao.BaseDao作为缓存的key

Ⅱ、移除缓存

使用@CacheEvict测试移除缓存

@Test
public void testCacheEvict(){
    this.baseDao.remove(0);
}

执行测试结果:

执行remove方法。。。。。

缓存已经清除:

此时再去访问findOne(),结果:

执行findOne方法。。。。
我是BaseDao0
我是BaseDao0

猜你喜欢

转载自blog.csdn.net/Laiguanfu/article/details/89113894
今日推荐