首页 > 数据库 >springboot中使用cache和redis

springboot中使用cache和redis

时间:2023-06-05 17:08:30浏览次数:35  
标签:缓存 springboot max cache redis id

知识点:springboot中使用cache和redis

 (1)springboot中,整合了cache,我们只需要,在入口类上加 @EnableCaching 即可开启缓存

 例如:在service层使用@Cacheable和CacheEvict

 

//添加缓存
@Cacheable(cacheNames = "TestCACHE",key = "#root.methodName +'_'+ #id")
public  Map<String, Object> testSetCache(Integer id){
    Map<String, Object>  user = userMapper.findUserById(id);
    return user;
}

//清除缓存
@CacheEvict(cacheNames = "TestCACHE", allEntries = true)
public Boolean testEvictCache(){
    return true;
}

(2)引入redis依赖,将Cache中的数据放到redis数据库中,然后从redis中取数据

 a.引入依赖
<!--加入redis依赖-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
b.application.yml中设置redis连接等配置
redis:
  host: 192.10.21.237
  port: 6379
  database: 5
  password:
  timeout: 1800000
  jedis:
    pool:
      max-idle: 10
      min-idle: 0
      max-active: 10
      max-wait: 1000

存入redis的数据如下

springboot中使用cache和redis_spring


之后会总结1.cache其他用法 2.springboot中的原理



标签:缓存,springboot,max,cache,redis,id
From: https://blog.51cto.com/u_4018548/6417822

相关文章

  • 使用powermock写springboot2.7业务类的测试用例
    1,引入powermock依赖<dependency><groupId>org.powermock</groupId><artifactId>powermock-core</artifactId><version>2.0.9</version><scope>test</......
  • 【SpringBoot】如何配置静态资源的地址与访问路径
    静态资源,例如HTML文件、JS文件,设计到的SpringBoot配置有两项,一是“spring.mvc.static-path-pattern”,一是“spring.resources.static-locations”,很多人都难以分辨它们之间的差异,所以经常出现的结果就是404错误,无法找到静态资源。1.spring.mvc.static-path-patternspring.mvc.sta......
  • 为SpringBoot Admin监控的服务端加上登录认证
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>packagecom.ciih.refineinner.config;importlombok.extern.slf4......
  • 为SpringBoot Admin加上登录认证
    依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>配置server:port:8000spring:security:user:n......
  • SpringBoot Admin的基本使用(单体应用)
    springboot项目和springbootadmin项目不建议放在一起,因为目的是为了监控,如果放在一起的话,一旦springboot挂了,springbootadmin也就一起挂了,监控就失去意义.搭建监控项目:<dependencies><dependency><groupId>org.springframework.boot</groupId>......
  • SpringBoot2.x跨域问题(CrossOrigin失效问题)
    方法一SpringBoot版本的不同,CrossOrigin失效了,正确配置如下:@CrossOrigin(originPatterns="*",allowCredentials="true",maxAge=3600)方法二如果以上方法还是不生效,最后的终极方法可以进行硬编码进行跨域设置:对需要跨域的接口,进行Response对象设置可跨域URL设置(*代表......
  • kettle web springboot mvn dockerfile
    远程构建dcokerfileFROMopenjdk:8-jdk-alpineasTEMP_BUILD_IMAGERUNset-eux&&sed-i's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g'/etc/apk/repositoriesRUNapkupdate&&\apkadd--no-cachebashcurlwget&&......
  • SpringBoot2 缓存之王caffeine
    <dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId><version>2.9.0</version></dependency>顺便写了个工具类配合SpringBoot使用:packag......
  • SpringBoot配置多个RabbitMq
    YMLrabbitmq:first:username:${app.appkey}password:${app.appkey}virtual-host:${app.appid}addresses:x.x.x.x:5672,x.x.x.x:5672#集群second:username:guestpassword:guestvirtual-host:/host:......
  • NineData,稳定、高效的Redis数据同步解决方案
    在DB-Engines网站的排名中,Redis在Key-value存储的NoSQL领域连续霸榜多年,是目前最流行的键值对存储数据库,被广泛用于缓存、队列、实时分析等多种高并发的场景中。在生产环境中,我们会遇到对Redis进行版本升级和架构的扩缩容的操作,这些操作都会涉及到Redis数据同步操作,所以,一个......