您的位置:首页 > 科技 > IT业 > 专卖店vi设计公司_北京到广州动卧_网络营销策略概念_免费b站推广网站入口

专卖店vi设计公司_北京到广州动卧_网络营销策略概念_免费b站推广网站入口

2024/9/24 16:55:45 来源:https://blog.csdn.net/qq_52848267/article/details/142490488  浏览:    关键词:专卖店vi设计公司_北京到广州动卧_网络营销策略概念_免费b站推广网站入口
专卖店vi设计公司_北京到广州动卧_网络营销策略概念_免费b站推广网站入口

简介

Spring Cache 是 Spring 框架提供的一种缓存抽象,它允许开发者以一种声明式的方式来使用缓存,而不需要关心底层的缓存实现技术。Spring Cache 的核心org.springframework.cache.Cache org.springframework.cache.CacheManager 接口,它们定义了缓存操作的基本规范 。

Spring Cache 提供了一层抽象,底层可以切换不同的缓存实现,例如:

  • EHCache
  • Caffeine
  • Redis

要开始使用 Spring Cache,你需要在你的项目中添加 Spring Context 模块的依赖,如果你使用的是 Spring Boot,可以通过添加 spring-boot-starter-cache 来快速集成缓存功能 。

使用

要开始使用 Spring Cache,你需要在你的项目中添加 Spring Context 模块的依赖,如果你使用的是 Spring Boot,可以通过添加 spring-boot-starter-cache 来快速集成缓存功能 。

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

常用注解

注解说明
@EnableCaching开启缓存注解功能,通常加在启动类上
@Cacheable在方法执行前先查询缓存中是否有数据,如果有数据,则直接返回缓存数据;如果没有缓存数据,调用方法并将方法返回值放到缓存中
@CachePut将方法的返回值放到缓存中
@CacheEvict将一条或多条数据从缓存中删除

使用展示

    @PostMapping@CachePut(cacheNames = "user",key = "#user.id")//使用Spring Cache缓存数据,key的生成:user::id
//    @CachePut(value = "user",key = "#result.id")//使用Spring Cache缓存数据,key的生成:user::result.id                   result是返回值
//    @CachePut(value = "user",key = "#p0.id")//使用Spring Cache缓存数据,key的生成:user::p0.id                           p0是第一个方法参数中的id
//    @CachePut(value = "user",key = "#a0.id")//使用Spring Cache缓存数据,key的生成:user::a0                              p0是第一个方法参数中的id
//    @CachePut(value = "user",key = "#root.args[0].id")//使用Spring Cache缓存数据,key的生成:user::root.args[0].id       root.args[0].id是第一个方法参数中的idpublic User save(@RequestBody User user){userMapper.insert(user);return user;}
	@DeleteMapping@CacheEvict(cacheNames = "user",key = "#id")public void deleteById(Long id){userMapper.deleteById(id);}
	@DeleteMapping("/delAll")@CacheEvict(cacheNames = "user",allEntries = true)public void deleteAll(){userMapper.deleteAll();}
    @GetMapping@Cacheable(cacheNames = "user",key = "#id")public User getById(Long id){User user = userMapper.getById(id);return user;}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com