温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot集成SpringCache的过程详解

发布时间:2021-09-14 23:08:48 来源:亿速云 阅读:134 作者:chen 栏目:编程语言

这篇文章主要讲解了“SpringBoot集成SpringCache的过程详解”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringBoot集成SpringCache的过程详解”吧!

一、关于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 //开启缓存@SpringBootApplicationpublic class SpringbootApplication {  public static void main(String[] args) {    SpringApplication.run(SpringbootApplication.class, args);  }}

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

@Slf4j@Servicepublic 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

感谢各位的阅读,以上就是“SpringBoot集成SpringCache的过程详解”的内容了,经过本文的学习后,相信大家对SpringBoot集成SpringCache的过程详解这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI