SpringBoot系列:Spring Boot集成Spring Cache

一、关于Spring Cache

缓存在现在的应用中越来越重要,
Spring从3.1开始定义了org.springframework.cache.Cache和org.springframework.cache.CacheManager接口来统一不同的缓存技术,并支持使用JCache(JSR-107)注解简化我们开发。

通过SpringCache,可以快速嵌入自己的Cache实现,主要是@Cacheable、@CachePut、@CacheEvict、@CacheConfig、@Caching等注解来实现。

  • @Cacheable:作用于方法上,用于对于方法返回结果进行缓存,如果已经存在该缓存,则直接从缓存中获取,缓存的key可以从入参中指定,缓存的value为方法返回值。
  • @CachePut:作用于方法上,无论是否存在该缓存,每次都会重新添加缓存,缓存的key可以从入参中指定,缓存的value为方法返回值,常用作于更新。
  • @CacheEvict:作用于方法上,用于清除缓存。
  • @CacheConfig:作用在类上,统一配置本类的缓存注解的属性。
  • @Caching:作用于方法上,用于一次性设置多个缓存。
  • @EnableCaching:作用于类上,用于开启注解功能。

二、演示示例

欲使用Spring Cache,需要先引入Spring Cache的依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Spring Cache依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

然后在启动类上,我们需要使用@EnableCaching来声明开启缓存。

@EnableCaching //开启缓存
@SpringBootApplication
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }

}

这样就可以使用注解来操作缓存了,创建CacheService类,其中dataMap的Map存储数据,省去了数据库的操作。

@Slf4j
@Service
public class CacheService {

    private Map<Integer, User> dataMap = new HashMap <Integer, User>(){
        {
            for (int i = 1; i < 100 ; i++) {
                User u = new User("code" + i, "name" + i);
                put(i, u);
            }
        }
     };

    // 获取数据
    @Cacheable(value = "cache", key = "'user:' + #id")
    public User get(int id){
        log.info("通过id{}查询获取", id);
        return dataMap.get(id);
    }

    // 更新数据
    @CachePut(value = "cache", key = "'user:' + #id")
    public User set(int id, User u){
        log.info("更新id{}数据", id);
        dataMap.put(id, u);
        return u;
     }
     
    //删除数据
    @CacheEvict(value = "cache", key = "'user:' + #id")
    public User del(int id){
        log.info("删除id{}数据", id);
        dataMap.remove(id);
        return u;
    }

}

get方法模拟查询,@Cacheable用于添加缓存,set方法用于修改,@CachePut更新缓存,del方法用于删除数据, @CacheEvict删除缓存。需要注意的是,注解的value表示缓存分类,并不是指缓存的对象值。

然后在创建CacheApi,用于调用CacheService进行测试。

@RestController
@RequestMapping("cache")
public class CacheApi {

    @Autowired
    private CacheService cacheService;

    @GetMapping("get")
    public User  get(@RequestParam int id){
        return cacheService.get(id);
    }

    @PostMapping("set")
    public User  set(@RequestParam int id, @RequestParam String code, @RequestParam String name){
        User u = new User(code, name);
        return cacheService.set(id, u);
    }

    @DeleteMapping("del")
    public void  del(@RequestParam int id){
        cacheService.del(id);
    }

}

然后我们打开swagger-ui界面(http://localhost:10900/swagger-ui.html)进行测试,多次调用查询,可以看到, CacheService的get方法,对于同一id仅仅执行一遍。然后再调用更新,再次get时,即可发现数据已经更新,而调用del,则可以清除缓存,再次查询又会调用方法。

源码地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache

猜你喜欢

转载自www.cnblogs.com/imyanger/p/11723560.html