WebFlux的使用

article/2025/8/29 2:01:24

什么是WebFlux

        springWebFlux 是 SpringFrameworlk5.0 添加的新功能,它是完全非阻塞的,支持Reactive Stream及背压,可以运行于Netty、Undertow等服务器,及Servlet 3.1+容器。

        webflux主要在如下两方面体现出独有的优势:

1)非阻塞式

         其实在servlet3.1提供了非阻塞的API,WebFlux提供了一种比其更完美的解决方案。使用非阻塞的方式可以利用较小的线程或硬件资源来处理并发进而提高其可伸缩性

2) 函数式编程端点

        老生常谈的编程方式了,Spring5必须让你使用java8,那么函数式编程就是java8重要的特点之一,而WebFlux支持函数式编程来定义路由端点处理请求。

WebFlux与SpringMVC

 区别:

  1. WebFlux是完全异步非阻塞的,SpringMVC是同步阻塞的。
  2. WebFlux采用异步响应式编程,SpringMVC采用命令式编程。
  3. WebFlux由于完全异步,所有操作数据库的框架,以及数据库也都要求是支持异步的,所以目前不支持Mybatis、不支持Oracle数据库。

WebFlux学习步骤

使用WebFlux需要有以下的技术基础,才能快速上手。

  1. Java8的新特性(Lambda表达式、Stream API、函数式编程)
  2. Java9(Reactive Stream)
  3. Project reactor
  4. WebFlux

怎么用

Java8的新特性

        请看这篇博客:Java8新特性

Java9的Reactive Stream

       reactive stream就是一个异步stream处理的标准,它的特点就是非阻塞的back pressure。reactive stream需要做哪些事情:

  1. 能够处理无效数量的消息
  2. 消息处理是有顺序的
  3. 可以异步的在组件之间传递消息
  4. 一定是非阻塞和backpressure的

        为了实现这4个功能,reactive stream定义了4个接口,Publisher,Subscriber,Subscription,Processor。这四个接口实际上是一个观察者模式的实现。

JDK中reactive stream的实现:

        在JDK中java.util.concurrent.Flow就是reactive stream语义的一种实现。Flow从JDK9就开始有了。我们看下它的结构: 

package com.zx.webflux.reactive;

import org.junit.Test;

import java.util.concurrent.Flow;

import java.util.concurrent.SubmissionPublisher;

/**

 * @Description jdk9 reactive stream的使用

 * @Author zx

 * @Date 2021/9/13 9:03

 */

public class ReactiveDemo {

    @Test

    public void test1(){

        //创建发布者

        SubmissionPublisher<String> publisher = new SubmissionPublisher<String>();

        //创建订阅者

        Flow.Subscriber subscriber = new Flow.Subscriber() {

            private Flow.Subscription subscription;

            @Override

            public void onSubscribe(Flow.Subscription subscription) {

                //subscription 订阅协议

                System.out.println("建立订阅关系第一次调用!");

                this.subscription = subscription;

//可控制请求数量

                this.subscription.request(1);

            }

            @Override

            public void onNext(Object item) {

                System.out.println("接受数据:" +item);

                this.subscription.request(1);

                //this.subscription.cancel();

            }

            @Override

            public void onError(Throwable throwable) {

                System.out.println("onError");

            }

            @Override

            public void onComplete() {

                System.out.println("数据接收完成!");

            }

        };

        //建立订阅关系

        publisher.subscribe(subscriber);

        try {

            //发数据

            for (int i = 0; i < 10; i++) {

                System.out.println("开始发送数据:" + "reactive" + i);

                publisher.submit("hello reactive" + i);

            }

            Thread.currentThread().join();

        } catch (Exception e) {

            e.printStackTrace();

        }finally {

            //结束

            publisher.close();

        }

    }

}

Project reactor

        Project Reactor是一个运行在JVM上的反应式编程基础库,以“背压”的形式管理数据处理,提供了可组合的异步序列APIFlux和Mono。同时,它也实现了Reactive Streams 规范。

        Flux<T>是一个标准的Reactive Streams规范中的Publisher<T>,它代表一个包含了[0…N]个元素的异步序列流。在Reactive Streams规范中,针对流中每个元素,订阅者将会监听这三个事件:onNext、onComplete、onError。

        Mono<T>是一个特殊的Flux<T>,它代表一个仅包含1个元素的异步序列流。因为只有一个元素,所以订阅者只需要监听onComplete、onError。

Maven依赖:

<dependency>

<groupId>io.projectreactor</groupId>

<artifactId>reactor-bom</artifactId>

<version>Bismuth-RELEASE</version>

<type>pom</type>

</dependency>

<dependency>

<groupId>io.projectreactor</groupId>

<artifactId>reactor-core</artifactId>

<version>3.3.16.RELEASE</version>

</dependency>

<dependency>

<groupId>io.projectreactor</groupId>

<artifactId>reactor-test</artifactId>

<version>3.3.16.RELEASE</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>1.18.12</version>

</dependency>

package com.zx.webflux.reactor;

import org.junit.Test;

import reactor.core.publisher.Flux;

import reactor.core.publisher.Mono;

import java.util.Arrays;

/**

 * @Description ReactorDemo

 * @Author zx

 * @Date 2021/9/13 9:56

 */

public class ReactorDemo {

    @Test

    public void test1(){

        //发布者

        Mono<String> mono = Mono.just("hello mono");

        //订阅

        mono.subscribe((x) -> System.out.println("1个元素:" + x));

//        System.out.println("------------------------------");

//        Mono<String> mono1 = Mono.empty();

//        mono1.subscribe((x) -> System.out.println("空的元素:" + x));

        System.out.println("------------------------------");

        //Flux

        Flux<String> flux1 = Flux.just("hello flux1","hello flux2","hello flux3");

        flux1.subscribe((x) -> System.out.println("多个元素:" + x));

        System.out.println("------------------------------");

        //Flux.fromIterable

        Flux<String> flux2 = Flux.fromIterable(Arrays.asList("flux1","flux2","flux3"));

        flux2.subscribe((x) -> System.out.println("多个元素:" + x));

        System.out.println("------------------------------");

        //通过程序创建

        Flux<String> flux3 = Flux.generate(() -> 0,

                (state, sink) -> {

                    sink.next("3 x " + state + " = " + 3 * state);

                    if (state == 10) {

                        sink.complete();

                    }

                    return state + 1;

                });

        flux3.subscribe(System.out::println);

    }

}

WebFlux

Maven依赖

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-webflux</artifactId>

</dependency>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<optional>true</optional>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>io.projectreactor</groupId>

<artifactId>reactor-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connection-java</artifactId>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-r2dbc</artifactId>

</dependency>

<dependency>

<groupId>dev.miku</groupId>

<artifactId>r2dbc-mysql</artifactId>

<scope>runtime</scope>

</dependency>

WebFlux的2种实现方式

注解的方式

package com.zx.demo.annotate;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import reactor.core.publisher.Mono;

/**

 * @Description TODO

 * @Author zx

 * @Date 2021/9/15 10:16

 */

@SpringBootApplication

@RestController

@RequestMapping("annt")

public class AnnoControllerAppliaction {

    public static void main(String[] args) {

        SpringApplication.run(AnnoControllerAppliaction.class, args);

    }

    @GetMapping("/get")

    public Mono<String> get(){

        return Mono.just("webflux annotation");

    }

}

编码的方式

package com.zx.demo.function;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.http.MediaType;

import org.springframework.web.reactive.function.server.*;

/**

 * @Description 路由  (相当于配置映射关系(url --> 业务方法))

 * @Author zx

 * @Date 2021/9/15 10:26

 */

@Configuration

public class WebFluxRouting {

    @Bean

    public RouterFunction<ServerResponse> route(WebFluxHandler webFluxHandler){

        return RouterFunctions.route(RequestPredicates.GET("/hello")

                .and(RequestPredicates

                        .accept(MediaType.TEXT_PLAIN)), webFluxHandler::hello);

    }

}

package com.zx.demo.function;

import org.springframework.http.MediaType;

import org.springframework.stereotype.Component;

import org.springframework.web.reactive.function.BodyInserters;

import org.springframework.web.reactive.function.server.ServerRequest;

import org.springframework.web.reactive.function.server.ServerResponse;

import reactor.core.publisher.Mono;

/**

 * @Description

 * @Author zx

 * @Date 2021/9/15 10:25

 */

@Component

public class WebFluxHandler  {

    public Mono<ServerResponse> hello(ServerRequest serverRequest){

        return ServerResponse.ok()

                .contentType(MediaType.TEXT_PLAIN)

                .body(BodyInserters.fromValue("hello webflux"));

    }

}

事务的2种实现方式

注解的方式

编码的方式

 

 SpringBoot + WebFlux + r2dbc增删改查

项目结构

package com.zx.demo.crud.config;

import io.r2dbc.spi.ConnectionFactory;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.r2dbc.connection.R2dbcTransactionManager;

import org.springframework.transaction.ReactiveTransactionManager;

import org.springframework.transaction.reactive.TransactionalOperator;

/**

 * @Description 事务配置类

 * @Author zx

 * @Date 2021/9/16 15:16

 */

@Configuration

public class R2dbcConfiguration {

    @Bean

    public ReactiveTransactionManager transactionManager(ConnectionFactory connectionFactory){

        return (new R2dbcTransactionManager(connectionFactory));

    }

    @Bean

    public TransactionalOperator transactionalOperator(ReactiveTransactionManager transactionManager){

        return TransactionalOperator.create(transactionManager);

    }

}

package com.zx.demo.crud.config;

/**

 * @Description 所有url

 * @Author zx

 * @Date 2021/9/15 11:20

 */

public class RoutePath {

    public static final String root = "/rout/product";

    public static final String save = root + "/save";

    public static final String find = root + "/find/{productId}";

    public static final String delete = root + "/delete/{productId}";

    public static final String update = root + "/update/{productId}";

    public static final String findAll = root + "/findAll";

    public static final String findByPage = root + "/findByPage";

    public static final String saveMany = root + "/saveMany";

}

package com.zx.demo.crud.dao;

import org.springframework.data.r2dbc.repository.Query;

import org.springframework.data.repository.reactive.ReactiveCrudRepository;

import com.zx.demo.crud.domain.Product;

import reactor.core.publisher.Flux;

/**

 * @Description TODO

 * @Author zx

 * @Date 2021/9/15 10:57

 */

public interface ProductRepository extends ReactiveCrudRepository<Product,Long>{

    //分页查询

    @Query("select * from t_product where category = :category limit :page,:pageSize")

    Flux<Product> findByPage(String category,int page,int pageSize);

}

package com.zx.demo.crud.domain;

import lombok.AllArgsConstructor;

import lombok.Builder;

import lombok.Data;

import lombok.NoArgsConstructor;

import org.springframework.data.annotation.Id;

import org.springframework.data.relational.core.mapping.Table;

import java.math.BigDecimal;

/**

 * @Description TODO

 * @Author zx

 * @Date 2021/9/15 10:58

 */

@Data

@AllArgsConstructor

@NoArgsConstructor

@Builder

@Table("t_product")

public class Product {

    @Id

    private Long id;

    private BigDecimal price;

    private String name;

    private Integer stock;

    private String category;

}

package com.zx.demo.crud.domain;

import lombok.AllArgsConstructor;

import lombok.Builder;

import lombok.Data;

import lombok.NoArgsConstructor;

/**

 * @Description 分页参数

 * @Author zx

 * @Date 2021/9/15 10:58

 */

@Data

@AllArgsConstructor

@NoArgsConstructor

@Builder

public class ProductPage {

    private String category;

    private Integer page;

    private Integer pageSize;

}

package com.zx.demo.crud.reative;

import com.zx.demo.crud.config.RoutePath;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.reactive.function.server.RouterFunction;

import org.springframework.web.reactive.function.server.RouterFunctions;

import org.springframework.web.reactive.function.server.ServerResponse;

/**

 * @Description 路由

 * @Author zx

 * @Date 2021/9/15 11:17

 */

@Configuration

public class BaseRouter {

    @Bean

    public RouterFunction<ServerResponse> route(MyWebHandler myWebHandler){

        return RouterFunctions.route()

                .POST(RoutePath.save, myWebHandler::save)

                .GET(RoutePath.find, myWebHandler::find)

                .DELETE(RoutePath.delete, myWebHandler::delete)

                .PUT(RoutePath.update, myWebHandler::update)

                .GET(RoutePath.findAll, myWebHandler::findAll)

                .GET(RoutePath.findByPage, myWebHandler::findByPage)

                .POST(RoutePath.saveMany, myWebHandler::saveMany)

                .build();

    }

}

package com.zx.demo.crud.reative;

import com.zx.demo.crud.domain.Product;

import com.zx.demo.crud.domain.ProductPage;

import com.zx.demo.crud.service.ProductService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.transaction.annotation.Transactional;

import org.springframework.transaction.reactive.TransactionalOperator;

import org.springframework.web.reactive.function.server.ServerRequest;

import org.springframework.web.reactive.function.server.ServerResponse;

import reactor.core.publisher.Mono;

/**

 * @Description TODO

 * @Author zx

 * @Date 2021/9/15 11:10

 */

@Component

public class MyWebHandler {

    @Autowired

    private ProductService productService;

    @Autowired

    private TransactionalOperator transactionalOperator;

    /**

     *

     * 功能描述: 商品保存

     *

     * @param:

     * @return:

     * @auther: zx

     * @date: 2021/9/15 11:17

     */

    public Mono<ServerResponse> save(ServerRequest serverRequest){

        return serverRequest.bodyToMono(Product.class)

                .flatMap(i -> productService.save(i))

                .flatMap(p -> ServerResponse.ok().bodyValue(p));

    }

    /**

     *

     * 功能描述: 商品查询

     *

     * @param:

     * @return:

     * @auther: zx

     * @date: 2021/9/15 11:17

     */

    public Mono<ServerResponse> find(ServerRequest serverRequest){

        Long productId = Long.parseLong(serverRequest.pathVariable("productId"));

        return productService.find(productId)

                .flatMap(p -> ServerResponse.ok().bodyValue(p))

                .switchIfEmpty(ServerResponse.notFound().build());

    }

    /**

     *

     * 功能描述: 商品刪除

     *

     * @param:

     * @return:

     * @auther: zx

     * @date: 2021/9/15 11:17

     */

    public Mono<ServerResponse> delete(ServerRequest serverRequest){

        long productId = Long.parseLong(serverRequest.pathVariable("productId"));

        return productService.find(productId)

                .flatMap(p -> productService.delete(p.getId()).then(ServerResponse.ok().build()))

                .switchIfEmpty(ServerResponse.notFound().build());

    }

    /**

     *

     * 功能描述: 商品更新

     *

     * @param:

     * @return:

     * @auther: zx

     * @date: 2021/9/15 11:17

     */

    public Mono<ServerResponse> update(ServerRequest serverRequest){

        long productId = Long.parseLong(serverRequest.pathVariable("productId"));

        return productService.find(productId)

                .flatMap(p -> serverRequest.bodyToMono(Product.class)

                .flatMap(i -> productService.save(i)))

                .flatMap(p -> ServerResponse.ok().bodyValue(p))

                .switchIfEmpty(ServerResponse.notFound().build());

    }

    /**

     *

     * 功能描述: 查詢所有

     *

     * @param:

     * @return:

     * @auther: zx

     * @date: 2021/9/15 11:17

     */

    public Mono<ServerResponse> findAll(ServerRequest serverRequest){

        return ServerResponse.ok().body(productService.findAll(), Product.class);

    }

    /**

     *

     * 功能描述: 分页查询

     *

     * @param:

     * @return:

     * @auther: zx

     * @date: 2021/9/15 11:17

     */

    public Mono<ServerResponse> findByPage(ServerRequest serverRequest){

        return serverRequest.bodyToMono(ProductPage.class)

                .flatMap(p -> {

                    System.out.println(p);

                    return ServerResponse.ok().body(productService.findByPage(p), Product.class);

                });

    }

    /**

     *

     * 功能描述: 保存多个商品 --事务测试

     *

     * @param:

     * @return:

     * @auther: zx

     * @date: 2021/9/15 11:09

     */

    @Transactional

    public Mono<ServerResponse> saveMany(ServerRequest serverRequest){

        return serverRequest.bodyToFlux(Product.class)

                .flatMap(i -> productService.saveMany(i))

                .then(ServerResponse.ok().bodyValue("ok"));

               // .as(transactionalOperator::transactional);

    }

}

package com.zx.demo.crud.service;

import com.zx.demo.crud.dao.ProductRepository;

import com.zx.demo.crud.domain.Product;

import com.zx.demo.crud.domain.ProductPage;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import reactor.core.publisher.Flux;

import reactor.core.publisher.Mono;

/**

 * @Description TODO

 * @Author zx

 * @Date 2021/9/15 11:08

 */

@Service

public class ProductService {

    @Autowired

    private ProductRepository productRepository;

    /**

     *

     * 功能描述: 商品保存

     *

     * @param:

     * @return:

     * @auther: zx

     * @date: 2021/9/15 11:09

     */

    public Mono<Product> save(Product product){

        return productRepository.save(product);

    }

    /**

     *

     * 功能描述: 商品查询

     *

     * @param:

     * @return:

     * @auther: zx

     * @date: 2021/9/15 11:09

     */

    public Mono<Product> find(Long id){

        return productRepository.findById(id);

    }

    /**

     *

     * 功能描述: 商品删除

     *

     * @param:

     * @return:

     * @auther: zx

     * @date: 2021/9/15 11:09

     */

    public Mono<Void> delete(Long productId){

        return productRepository.

                existsById(productId)

                .flatMap(p -> {

                    if(p){

                       return productRepository.deleteById(productId);

                    }

                    return Mono.empty();

                }

        );

    }

    /**

     *

     * 功能描述: 所有商品

     *

     * @param:

     * @return:

     * @auther: zx

     * @date: 2021/9/15 11:09

     */

    public Flux<Product> findAll(){

        return productRepository.findAll();

    }

    /**

     *

     * 功能描述: 所有商品--分页查询

     *

     * @param:

     * @return:

     * @auther: zx

     * @date: 2021/9/15 11:09

     */

    public Flux<Product> findByPage(ProductPage productPage){

        return productRepository.findByPage(productPage.getCategory(),productPage.getPage() ,productPage.getPageSize());

    }

    /**

     *

     * 功能描述: 保存多个商品 --事务处理

     *

     * @param:

     * @return:

     * @auther: zx

     * @date: 2021/9/15 11:09

     */

    public Mono<Product> saveMany(Product product){

        return productRepository.save(product);

    }

}

/**

 * @Description TODO

 * @Author zx

 * @Date 2021/9/15 10:26

 */

@SpringBootApplication

//开启事务的注解

@EnableTransactionManagement

public class WebfluxApplication {

    public static void main(String[] args) {

        SpringApplication.run(WebfluxApplication.class,args);

    }

}


http://chatgpt.dhexx.cn/article/0of1Qu8O.shtml

相关文章

webFlux入门

今天发现一个特别好的文章&#xff0c;是关于springBoot框架中响应式编程的&#xff0c;那下面就是这位博主所整理的一些干货&#x1f447; ------------------------------------------------------------ 1. WebFlux介绍 Spring WebFlux 是 Spring Framework 5.0中引入的新…

WebFlux 简介

目录 一、关于WebFlux 二、SpringMVC与SpringWebFlux 三、Reactive Spring Web HttpHandler WebHandler 四、实现WebFlux 实例 基于Annotated Controller方式实现 WebFluxConfig配置&#xff1a; Controller: Main方法&#xff1a; 函数式编程方式 集成Thymeleaf sp…

Spring Webflux 响应式编程 (二) - WebFlux编程实战

第一章 Reactive Stream 第1节 jdk9的响应式流 就是reactive stream&#xff0c;也就是flow。其实和jdk8的stream没有一点关系。说白了就一个发布-订阅模式&#xff0c;一共只有4个接口&#xff0c;3个对象&#xff0c;非常简单清晰。 什么是背压&#xff1f; 背压是指订阅者…

WebFlux 详解

今天我们开始来学习下 WebFlux&#xff0c;为什么突然要学这个东西&#xff1f; 因为我之前是想学习 Spring Cloud Gateway 来着&#xff0c;然后发现它是基于 Spring5.0SpringBoot2.0WebFlux等技术开发的。所以学之前才要来简单了解下 WebFlux 技术。 然后要学习 WebFlux 时…

Redis常用数据类型及其对应的底层数据结构

Redis数据库 Redis是一种键值(Key-Value)数据库。相较于MySQL之类的关系型数据库&#xff0c;Redis是一种非关系型数据库。Redis存储的数据只包含键和值两部分&#xff0c;只能通过键来查询值。这样简单的存储结构&#xff0c;能让Redis的读写效率非常高(HashMap读写效率都是O…

二. Redis 数据类型

2.1 Redis 字符串 (String) 2.1.1 概述 String 是 Redis 最基本的类型&#xff0c;你可以理解成与 Memcached 一模一样的类型&#xff0c;一个 key 对应一个 value。 String 类型是二进制安全的。意味着 Redis 的 string 可以包含任何数据。比如 jpg 图片或者序列化的对象。 …

Redis数据类型Hash

文章目录 Hash类型介绍hash 类型数据的基本操作 hash 类型数据操作的注意事项Hash和String类型的区别 有时候我们往往不是在缓存中存一个值&#xff0c;而是选择存一个对象&#xff0c;比如一个购物车消息&#xff0c;我们就需要使用到hash了 Hash类型介绍 新的存储需求&…

Redis数据类型String

文章目录 数据存储类型介绍String类型String类型的基本操作String单数据操作和多数据操作的选择问题string 类型数据的扩展操作数据增加指定范围的值String 设置数据指定的生命周期string 类型数据操作的注意事项 数据存储类型介绍 常用的五个数据类型: string --------------…

Redis数据类型 - 散列(Map)

文章目录 一、散列简介二、散列的基本操作三、散列与字符串比较1、散列键的优点2、字符串键的优点 一、散列简介 散列就是hash或者说Map&#xff0c;Redis的散列键会将一个键和一个散列在数据库中关联起来&#xff0c;可以在散列中设置任意多个字符串键值对&#xff0c;因此通…

redis数据类型插入输出命令

进入客户机&#xff1a;redis-cli 中文字符不能显示&#xff1a;redis-cli --raw、get Course:1:Cname 一、redis数据类型数据的添加 1、String 添加数据&#xff1a;set StringTest(数据名称) “helloword”(数据) 显示数据&#xff1a;get StringTest&#xff08;数据名称&am…

Redis数据类型及使用场景

转自&#xff1a; http://www.kubiji.cn/juhe-id7106.html Redis数据类型及使用场景 来源&#xff1a; WQTech阅读&#xff1a; 2936 时间&#xff1a;2 小时前 摘要&#xff1a;Redis相比其它的KV数据库,其一大特点是支持丰富的数据类型.它一共支持5种数据类型,下面逐一介绍这…

Redis数据类型与操作命令

1. 键值对数据库 1.1 redis数据结构 redis的数据是 key-value 形式的键值对&#xff0c;其中 key 其实都是字符串的形式&#xff0c;而 value 的数据类型&#xff0c;也就是数据的保存形式&#xff0c;底层实现的方式就用到了数据结构。 所以我们一直说的“redis五种数据结构…

Redis数据类型

文章目录 STRINGLISTSETHASHZSET Redis主要有5种数据类型&#xff0c;包括String&#xff0c;List&#xff0c;Set&#xff0c;Zset&#xff0c;Hash&#xff0c;满足大部分的使用要求&#xff0c;Redis各数据类型的使用场景可以参考Redis使用场景 数据类型可以存储的值操作ST…

redis数据类型(5种)和底层实现

redis数据类型(5种)和底层实现 Redis的特点 要用好Redis&#xff0c;首先要明白它的特点&#xff1a; 读写速度快。redis官网测试读写能到10万左右每秒。速度快的原因这里简单说一下&#xff0c;第一是因为数据存储在内存中&#xff0c;我们知道机器访问内存的速度是远远大于…

Redis数据类型及编码

Redis数据类型及编码 说到Redis的数据类型&#xff0c;我们大概会很快想到Redis的5种常见的数据类型&#xff1a;字符串(String)、列表(List)、散列(Hash)、集合(Set)、有序集合(Sorted Set)&#xff0c;以及他们的特点和运用场景及常用命令。不过在讲五大数据类型之前&#x…

Redis 基础 -- Redis数据类型之set

文章目录 1. Redis数据类型之set1.1 set类型介绍1.2 set类型基本操作1.3 set 类型数据的扩展操作&#xff1a;获取随机的数据1.4 set 类型数据的扩展操作&#xff1a;集合的交、并、差集1.4.1 sinter命令1.4.2 sunion命令1.4.3 sdiff命令1.4.4 sinterstore命令1.4.5 sunionstor…

Redis 数据类型

1、string类型 &#xff08;1&#xff09;存储的数据&#xff1a;单个数据&#xff0c;最简单的数据存储类型&#xff0c;也是最常用的数据存储类型。 string&#xff0c;他就是存一个字符串儿&#xff0c;注意是value那一部分是一个字符串&#xff0c;它是redis中最基本、最…

NoSQL数据库之Redis(三):常用五大数据类型

目录 Redis键(key)常用命令 Redis字符串(String)常用命令原子性数据结构 Redis列表(List)常用命令数据结构 Redis集合(Set)常用命令数据结构 Redis哈希(Hash)常用命令数据结构 Redis有序集合Zset(sorted set)常用命令数据结构跳跃表&#xff08;跳表&#xff09; redis常见数据…

redis的五种数据类型

🏆作者简介:哪吒,CSDN2022博客之星Top1、CSDN2021博客之星Top2、多届新星计划导师✌、博客专家💪 ,专注Java硬核干货分享,立志做到Java赛道全网Top N。 🏆本文收录于,Java基础教程系列,目前已经700+订阅,CSDN最强Java专栏,包含全部Java基础知识点、Java8新特性、…

Redis 的五种基本数据类型

一、String 类型 1.1 介绍 String 类型&#xff0c;也就是字符串类型&#xff0c;是 Redis 中最简单的存储类型。其 value 是字符串&#xff0c;不过根据字符串的格式不同&#xff0c;又可以分为 3 类&#xff1a; string&#xff1a;普通字符串int&#xff1a;整数类型&…