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

springboot的缓存和redis缓存,入门级别教程

一、springboot(如果没有配置)默认使用的是jvm缓存

1、Spring框架支持向应用程序透明地添加缓存。抽象的核心是将缓存应用于方法,从而根据缓存中可用的信息减少执行次数。缓存逻辑是透明地应用的,对调用者没有任何干扰。只要使用@EnableCaching注释启用了缓存支持,Spring Boot就会自动配置缓存基础结构。

2、在Spring Boot中,默认情况下,它会根据一定的顺序去侦测缓存提供者,包括Generic、JCache(JSR-107)、EhCache 2.x、Hazelcast、Infinispan、Redis、Guava和Simple等 如果探测不到用的则是它会默认使用SimpleCacheConfiguration,即使用ConcurrentMapCacheManager来实现缓存,这是一种基于JVM内存的缓存。

具体代码案例:

依赖:

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

MyMathService:

package dev.farhan.movies.cache;import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;@Component
public class MyMathService {@Cacheable("piDecimals")public int computePiDecimal(int precision) throws InterruptedException {if (precision==2){Thread.sleep(2000);return 45;}Thread.sleep(2000);return 23;}}
MyMathController类:
package dev.farhan.movies.controller;import dev.farhan.movies.cache.MyMathService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class MyMathController {@AutowiredMyMathService myMathService;@GetMapping("/math")public void getMath() throws InterruptedException {System.out.println(myMathService.computePiDecimal(2));System.out.println(myMathService.computePiDecimal(89));}}

结果:访问localhost:8080/math

就会发现,第一次响应用了4秒多

而第二次响应则是用了4毫秒,明显走了缓存

二、我们比较常用的是redis的缓存

缓存抽象并不提供实际的存储,而是依赖于org.springframework.cache.Cache和org.springframework.cache.CacheManager接口实现的抽象。

如果Redis可用且已配置,则自动配置RedisCacheManager。通过设置spring.cache,可以在启动时创建额外的缓存。cache-names属性和cache默认值可以通过spring.cache.redis配置。*属性。例如,下面的配置创建了cache1和cache2缓存,它们的生存时间为10分钟:

1、这里我们先配置一下redis

引入依赖:

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

第二步:配置文件:

spring:data:redis:host: #主机ippassword:cache:type: rediscache-names: "cache1,cache2"redis:time-to-live: "10m"

然后启动项目:并访问localhost:8080/math

观察redis里面:

刚好存了piDecimals   可以对上

至于存的内容,2和89,则是:它的参数

http://www.lryc.cn/news/213089.html

相关文章:

  • 语雀P0级时间爆发,留给运维的时间不多了?
  • LeetCode 2401.最长优雅子数组 ----双指针+位运算
  • NOIP2023模拟6联测27 无穷括号序列
  • java spring cloud 工程企业管理软件-综合型项目管理软件-工程系统源码
  • openEuler 22.03 x86架构下docker运行arm等架构的容器——筑梦之路
  • 【Java】HashMap常见的面试题
  • openpnp - src - 配置文件载入过程的初步分析
  • 中国各城市土地利用类型(城市功能)数据集(shp)
  • Linux网络编程:数据链路层
  • python 线程 超时时间
  • LeetCode:274. H 指数、275. H 指数 II(C++)
  • 多线程及锁
  • C++ 写一个Data类的注意问题
  • postman做接口测试
  • hdlbits系列verilog解答(always块)-29
  • uniapp实现瀑布流
  • 15. 机器学习 - 支持向量机
  • 如何根据进程号查询服务的端口号
  • 2.10、自定义量化优化过程
  • MySQL如何添加自定义函数
  • 超融合数据库:解锁全场景数据价值的钥匙
  • Pap.er for Mac:高清壁纸应用打造你的专属视觉盛宴
  • AI:46-基于深度学习的垃圾邮件识别
  • 【骑行贝丘渔场】一场与海的邂逅,一段难忘的旅程
  • 消息中间件——RabbitMQ(一)Windows/Linux环境搭建(完整版)
  • Mysql 表读锁与表写锁
  • 目标检测概述
  • 10月31日星期二今日早报简报微语报早读
  • 【Linux】虚拟机项目部署与发布
  • 边缘计算技术的崭新篇章:赋能未来智能系统