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

简介: 本文介绍了Spring Boot中的缓存机制,包括使用默认的JVM缓存和集成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 {

    @Autowired
    MyMathService 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: #主机ip
      password:
  cache:
    type: redis
    cache-names: "cache1,cache2"
    redis:
      time-to-live: "10m"

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

观察redis里面:

刚好存了piDecimals 可以对上

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

目录
相关文章
|
1天前
|
存储 缓存 NoSQL
数据的存储--Redis缓存存储(二)
数据的存储--Redis缓存存储(二)
10 2
数据的存储--Redis缓存存储(二)
|
1天前
|
NoSQL Java Redis
redis的基本命令,并用netty操作redis(不使用springboot或者spring框架)就单纯的用netty搞。
这篇文章介绍了Redis的基本命令,并展示了如何使用Netty框架直接与Redis服务器进行通信,包括设置Netty客户端、编写处理程序以及初始化Channel的完整示例代码。
9 1
redis的基本命令,并用netty操作redis(不使用springboot或者spring框架)就单纯的用netty搞。
|
1天前
|
Java API Apache
Springboot+shiro,完整教程,带你学会shiro
这篇文章提供了一个完整的Apache Shiro与Spring Boot结合使用的教程,包括Shiro的配置、使用以及在非Web和Web环境中进行身份验证和授权的示例。
6 2
Springboot+shiro,完整教程,带你学会shiro
|
1天前
|
Cloud Native Java C++
Springboot3新特性:开发第一个 GraalVM 本机应用程序(完整教程)
文章介绍如何在Spring Boot 3中利用GraalVM将Java应用程序编译成独立的本机二进制文件,从而提高启动速度、减少内存占用,并实现不依赖JVM运行。
17 1
Springboot3新特性:开发第一个 GraalVM 本机应用程序(完整教程)
|
1天前
|
前端开发 Java Apache
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个
本文详细讲解了如何整合Apache Shiro与Spring Boot项目,包括数据库准备、项目配置、实体类、Mapper、Service、Controller的创建和配置,以及Shiro的配置和使用。
20 1
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个
|
1天前
|
存储 缓存 NoSQL
数据的存储--Redis缓存存储(一)
数据的存储--Redis缓存存储(一)
12 1
|
Java API 数据库
springboot webflux r2dbc入门案例
springboot webflux r2dbc入门案例
286 0
|
druid Java 关系型数据库
SpringBoot基础知识入门详细介绍&基于SpringBoot的SSMP整合案例(2)
(一)快速上手SpringBoot SpringBoot入门程序开发 SpringBoot是由pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程。
|
XML Java 应用服务中间件
SpringBoot基础知识入门详细介绍&基于SpringBoot的SSMP整合案例(1)
(一)快速上手SpringBoot SpringBoot入门程序开发 SpringBoot是由pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程。
|
XML JSON Java
第二篇:SpringBoot入门案例(IDEA联网版本)
第二篇:SpringBoot入门案例(IDEA联网版本)
248 0
第二篇:SpringBoot入门案例(IDEA联网版本)