当前位置: 首页 > news >正文

网站的备案号在哪公众号开发专业

网站的备案号在哪,公众号开发专业,id注册网站,赔率网站怎么做Spring Cache 文章目录 Spring Cache1、Spring Cache介绍2、Spring Cache常用注解2.1、EnableCaching注解2.2、CachePut注解2.3、CacheEvict注解2.4、Cacheable注解 3、Spring Cache使用方式--redis 1、Spring Cache介绍 Spring Cache是一个框架,实现了基于注解的缓…

Spring Cache

文章目录

    • Spring Cache
      • 1、Spring Cache介绍
      • 2、Spring Cache常用注解
        • 2.1、EnableCaching注解
        • 2.2、CachePut注解
        • 2.3、CacheEvict注解
        • 2.4、Cacheable注解
      • 3、Spring Cache使用方式--redis

1、Spring Cache介绍

  • Spring Cache是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能.
  • Spring Cache提供了一层抽象,底层可以切换不同的cache实现。具体就是通过CacheManager接口来统一不同的缓存技术。CacheManager是Spring提供的各种缓存技术抽象接口。

针对不同的缓存技术需要实现不同的CacheManager:

CacheManager描述
EhCacheCacHManager使用EhCache作为缓存技术
GuavaCacheManager使用Google的GuavaCache作为缓存技术
RedisCacheManager使用Redis作为缓存技术

2、Spring Cache常用注解

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

在spring boot项目中,使用缓存技术只需在项目中导入相关缓存技术的依赖包,并在启动类上使用@EnableCaching开启缓存支持即可。

例如,使用Redis作为缓存技术,只需要导入Spring data Redis的maven坐标即可。

  • 我们在使用简单的缓存技术的时候不用单独映入相应的依赖,使用spring-boot-starter-web这个包即可,但是要使用redis作为缓存就需要额外的引入依赖:

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

2.1、EnableCaching注解

在启动类上加上注解@EnableCaching

2.2、CachePut注解

@Autowired
private CacheManager cacheManager;@Autowired
private UserService userService;/***  CachePut:将方法的返回值放入缓存*  value:缓存名称  每个缓存可以有多个key*  key:缓存的key  #result.id:动态生成keyName进行缓存***/
@CachePut(value = "userCache",key="#result.id")
//    @CachePut(value = "userCache",key="#user.name")  //将user->name作为缓存名
//    @CachePut(value = "userCache",key="#user.id")   //将user->id作为缓存名
@PostMapping
public User save(User user){userService.save(user);return user;
}

key值参数引用(SpEL动态获取)的说明:

image-20230823104302676

该注解在没有缓存配置(如redis)的时候,会将数据缓存在ConcurrentMap里面,但是此缓存操作不具有持久化的储存,在服务重启之后会数据丢失。解决方法是使用Redis等缓存中间件。

2.3、CacheEvict注解

  • DELETE
/***  CacheEvict:清理指定缓存*  value:缓存名称  每个缓存可以有多个key*  key:缓存的key  #result.id:动态生成keyName进行缓存*/
@CacheEvict(value = "userCache",key = "#id")
//@CacheEvict(value = "userCache",key = "#p0")
//@CacheEvict(value = "userCache",key = "#root.args[0]")
// 上述三种方式都是相同的效果,动态获取id的参数作为缓存的唯一标识
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id){userService.removeById(id); 
}
  • UPDATE
@CacheEvict(value = "userCache",key = "#user.id")
//@CacheEvict(value = "userCache",key = "#p0.id")
//@CacheEvict(value = "userCache",key = "#root.args[0].id")
//@CacheEvict(value = "userCache",key = "#result.id")
@PutMapping
public User update(User user){userService.updateById(user);return user;
}

2.4、Cacheable注解

    /*** Cacheable:在方法执行前spring先查看缓存中是否有数据,* 如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中;* value:缓存名称  每个缓存可以有多个key* key:缓存的key  #result.id:动态生成keyName进行缓存* condition:满足条件就进行缓存,无法使用#result* unless:满足条件就不进行缓存,可以使用#result-->看源码注解*/ @Cacheable(value = "userCache", key = "#id", condition = "#unless == null")@GetMapping("/{id}")public User getById(@PathVariable Long id) {User user = userService.getById(id);return user;}

在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中。

@Cacheable(value = "userCache", key = "#user.id+'_'+#user.name")
//多个变量的拼接
@GetMapping("/list")
public List<User> list(User user) {LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(user.getId() != null, User::getId, user.getId());queryWrapper.eq(user.getName() != null, User::getName, user.getName());List<User> list = userService.list(queryWrapper);return list;
}

3、Spring Cache使用方式–redis

在Spring Boot项目中使用Spring Cache的操作步骤(使用redis缓存技术):

  • 1、导入maven坐标
    spring-boot-starter-data-redisspring-boot-starter-cache
       <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>

2、配置application.yml

spring:redis:host: 127.0.0.1port: 6379password: 123456database: 0cache:redis:time-to-live: 1800000 #设置缓存过期时间,可选

3、在启动类上加入@EnableCaching注解,开启缓存注解功能
4、在Controller的方法上加入@Cacheable、@CacheEvict等注解,进行缓存操作

Demo项目源码:SpringCache-Demo

http://www.yayakq.cn/news/983736/

相关文章:

  • 广东营销网站建设服务外包加工网收费
  • 便宜的seo官网优化西安seo顾问
  • 莆田网站制作计划济南建站公司网站
  • 微网站建设使用程序石家庄高端网站开发
  • 2019年建设银行安徽招聘网站视频网站建设框架
  • 镇江网站优化哪家好传奇游戏排行榜
  • 怎么做电影网站app爱客源
  • 网站企业快速备案流程浙江特种作业证件查询
  • 做科技申报看什么网站泉州确诊人员名单最新
  • 广州网站建设费四川外国语大学网站建设
  • 有关建筑企业的网站有哪些昆山建设监察大队网站
  • 信用门户网站建设网站建设的域名和空间价位
  • 海淘科技上海网站设计嘉兴高端网站建设公司
  • pc网站建设费用北京企业网站建设制作
  • 网站建设教学方法探究wordpress 怎么加入插件
  • 济南网站建设开发与制作网站服务器做缓存
  • 网站的后台管理wordpress 注册连接
  • 益阳营销网站建设企业网站建设合同模板
  • 装饰公司营销型网站设计电子商务网站建设分析
  • 湖北网站郑州网站建设的软件
  • 西安网站建设哪个好天眼查企业入口免费
  • 科技网站有哪些大型公司网站建设目标
  • 学院网站设计方案吴江区经济开发区建设工程网站
  • 网站中的ppt链接怎么做搜索引擎网站
  • 载网站源码 怎么下载不了免费网站空间女人
  • 网站建设计划 文库最新网游网络游戏手游
  • 建设网站培训学校公司网址怎么做出来的
  • 网站建站网站实现用户登录
  • 建筑工人招聘网站怎么做网站前端代码有哪些问题
  • 怎么用程序做网站上海外贸网站优化