Spring Boot集成Spring Cache过程详解

作者:杨小格子 时间:2023-08-02 19:20:16 

一、关于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,则可以清除缓存,再次查询又会调用方法。

Spring Boot集成Spring Cache过程详解

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

来源:https://www.cnblogs.com/imyanger/p/11723560.html

标签:spring,boot,集成,cache
0
投稿

猜你喜欢

  • 详解java中接口与抽象类的区别

    2021-09-12 03:21:39
  • 解析C#彩色图像灰度化算法的实现代码详解

    2022-01-26 07:34:55
  • C#如何获取枚举的描述属性详解

    2023-02-23 13:49:59
  • Android自定义照相机Camera出现黑屏的解决方法

    2023-01-02 14:45:40
  • spring cloud 之 客户端负载均衡Ribbon深入理解

    2023-02-15 15:00:58
  • JAVA抽象类,接口,内部类详解

    2023-11-09 16:37:25
  • 解答“60k”大佬的19道C#面试题(上)

    2023-10-03 10:20:48
  • Flutter源码分析之自定义控件(RenderBox)指南

    2022-09-08 21:17:28
  • Android自定义封装banner组件

    2023-08-25 22:19:29
  • IDEA教程创建SpringBoot前后端分离项目示例图解

    2022-06-30 02:41:10
  • 浅谈BeanPostProcessor加载次序及其对Bean造成的影响分析

    2022-05-02 19:52:29
  • 详解Java多态对象的类型转换与动态绑定

    2021-10-12 06:59:59
  • flutter 怎么实现app整体灰色效果

    2023-01-24 21:23:22
  • SpringBoot Profile多环境配置方式

    2023-12-14 01:44:24
  • Java Integer及int装箱拆箱对比

    2023-04-22 04:21:11
  • java文件重命名(文件批量重命名)实例程序代码分享

    2023-07-20 06:45:06
  • 使用C#发送Http请求实现模拟登陆实例

    2023-06-22 22:25:07
  • Java SpringMVC数据响应超详细讲解

    2022-04-08 15:10:26
  • Android获取ROOT权限的实例代码

    2022-09-10 01:24:05
  • C#中datagridview使用tooltip控件显示单元格内容的方法

    2022-04-15 12:23:23
  • asp之家 软件编程 m.aspxhome.com