spring cache

Spring Cache是Spring 3.1以后引入的新技术。它并不像正常缓存那样存储数据,其核心思想是这样的:当我们在调用一个缓存方法时,会把该方法参数和返回结果作为一个键值对存放在缓存中,等到下次利用同样的参数来调用该方法时将不再执行该方法,而是直接从缓存中获取结果进行返回,从而实现缓存的功能。

 

注解

在Spring中提供了3个注解来使用Spring Cache

1. @Cacheable

@Cacheable注解用于标记缓存,也就是对使用@Cacheable注解的位置进行缓存。@Cacheable可以在方法或者类上进行标记,当对方法进行标记时,表示此方法支持缓存;当对类进行标记时,表明当前类中所有的方法都支持缓存。在支持Spring Cache的环境下,对于使用@Cacheable标记的方法,Spring在每次调用方法前都会根据key查询当前Cache中是否存在相同key的缓存元素,如果存在,就不再执行该方法,而是直接从缓存中获取结果进行返回,否则执行该方法并将返回结果存入指定的缓存中。在使用@Cacheable时通常会搭配3个属性进行使用,分别介绍如下。

• value:在使用@Cacheable注解的时候,value属性是必须要指定的,这个属性用于指定Cache的名称,也就是说,表明当前缓存的返回值用于哪个缓存上。

• key:和名称一样,用于指定缓存对应的key。key属性不是必须指定的,如果我们没有指定key,Spring就会为我们使用默认策略生成的key。其中默认策略是这样规定的:如果当前缓存方法没有参数,那么当前key为0;如果当前缓存方法有一个参数,那么以key为参数值;如果当前缓存方法有多个参数,那么key为所有参数的hashcode值。当然,我们也可以通过Spring提供的EL表达式来指定当前缓存方法的key。通常来说,我们可以使用当前缓存方法的参数指定key,一般为“#参数名”。如果参数为对象,就可以使用对象的属性指定key。

• condition:主要用于指定当前缓存的触发条件。很多情况下可能并不需要使用所有缓存的方法进行缓存,所以Spring Cache为我们提供了这种属性来排除一些特定的情况。以属性指定key为user.id为例,比如我们只需要id为偶数才进行缓存

 

 2. @CachePut

从名称上来看,@CachePut只是用于将标记该注解的方法的返回值放入缓存中,无论缓存中是否包含当前缓存,只是以键值的形式将执行结果放入缓存中。

3. @CacheEvict

Spring Cache提供了@CacheEvict注解用于清除缓存数据,与@Cacheable类似,不过@CacheEvict用于方法时清除当前方法的缓存,用于类时清除当前类所有方法的缓存。@CacheEvict除了提供与@Cacheable一致的3个属性外,还提供了一个常用的属性allEntries,这个属性的默认值为false,如果指定属性值为true,就会清除当前value值的所有缓存。

 

配置Spring Cache依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

 

测试运行

import com.xc.xcspringboot.model.area.TbArea;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试 spring cache
 * <p>
 * Created by xc on 2020/11/07
 */
@RestController
@Api(tags = "TbAreaCacheController", description = "tb_area表 controller")
@RequestMapping("/tb_area2")
public class TbAreaCacheController {

    private static final Logger log = LoggerFactory.getLogger(TbAreaCacheController.class);

    @ApiOperation("保存")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    //@CachePut,将返回值存放于缓存中。
    @CachePut(value = "tbArea", key = "#area.areaId")
    public TbArea save(TbArea area) {
        log.info("save.area:{}", area);
        TbArea tbArea = new TbArea();
        tbArea.setAreaId(area.getAreaId());
        tbArea.setAreaName("areaName_save");
        return tbArea;
    }

    @ApiOperation(value = "根据id查询", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaId", value = "主键id", dataType = "int", paramType = "query", required = true, defaultValue = "1"),
    })
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    //@Cacheable,这个注解在执行前会查看是否已经存在缓存,如果存在,就直接返回;如果不存在,就将返回值存入缓存后再返回。
    @Cacheable(value = "tbArea", key = "#areaId")
    public TbArea query(Integer areaId) {
        log.info("query.areaId:{}", areaId);
        TbArea tbArea = new TbArea();
        tbArea.setAreaId(areaId);
        tbArea.setAreaName("areaName");
        return tbArea;
    }


    @ApiOperation("删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    //@CacheEvict,用于删除对应的缓存。
    @CacheEvict(value = "tbArea", key = "#area.areaId")
    public int delete(TbArea area) {
        log.info("delete.area:{}", area);
        return 1;
    }

    @RequestMapping(value = "/deleteCache", method = RequestMethod.POST)
    //配置了allEntries属性,用于删除所有value为tbArea的缓存。
    @CacheEvict(value = "tbArea", allEntries = true)
    public void deleteCache() {
        log.info("deleteCache");
    }

}

 

参考文章:Spring Boot 2实战之旅.5.1 使用Spring Cache

posted @ 2020-11-07 10:08  草木物语  阅读(334)  评论(0编辑  收藏  举报