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

Redis系列之keys命令和scan命令性能对比

项目场景

Redis的keys *命令在生产环境是慎用的,特别是一些并发量很大的项目,原因是Redis是单线程的,keys *会引发Redis锁,占用reids CPU,如果key数量很大而且并发是比较大的情况,效率是很慢的,很有可能导致服务雪崩,在Redis官方的文档是这样解释的,官方的推荐是使用scan命令或者集合
在这里插入图片描述

解决方案

搭建一个工程来实践一下,项目环境:

  • JDK 1.8

  • SpringBoot 2.2.1

  • Maven 3.2+

  • Mysql 8.0.26

  • spring-boot-starter-data-redis 2.2.1

  • jedis3.1.0

  • 开发工具

    • IntelliJ IDEA

    • smartGit

新建一个SpringBoot项目
在这里插入图片描述

选择需要的依赖
在这里插入图片描述
选择Maven项目和jdk对应的版本
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot-jedis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-jedis</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.11</version>
        </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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

package com.example.jedis.configuration;

import com.example.jedis.common.JedisTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@ConditionalOnClass({GenericObjectPool.class, JedisConnection.class, Jedis.class})
@EnableRedisRepositories(basePackages = "com.example.jedis.repository")
@Slf4j
public class RedisConfiguration {


    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        return new JedisPoolConfig();
    }

    @Bean
    public JedisPool jedisPool() {
      return new JedisPool(jedisPoolConfig());
    }


    @Bean
    public RedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }

    




}

写一个工具类,实现redis scankeys *的逻辑,当然也可以直接使用RedisTemplate

import cn.hutool.core.collection.ConcurrentHashSet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.jedis.params.SetParams;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
@Component
@Slf4j
public class JedisUtil implements InitializingBean {


    @Resource
    private JedisPool jedisPool;

    private Jedis jedis;

    public JedisTemplate(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    public JedisTemplate() {

    }

    @Override
    public void afterPropertiesSet() {
        jedis = jedisPool.getResource();
    }


    public <T> T execute(Function<Jedis, T> action) {
        T apply = null;
        try {
            jedis = jedisPool.getResource();
            apply = action.apply(jedis);
        } catch (JedisException e) {
            handleException(e);
            throw e;
        } finally {
            jedis.close();
        }
        return apply;
    }

    public void execute(Consumer<Jedis> action) {
        try {
            jedis = jedisPool.getResource();
            action.accept(jedis);
        } catch (JedisException e) {
            handleException(e);
            throw e;
        } finally {
            jedis.close();
        }
    }

    public JedisPool getJedisPool() {
        return this.jedisPool;
    }
	
	 public Set<String> keys(final String pattern) {
        return execute(e->{
            return jedis.keys(pattern);
        });
    }

    public Set<String> scan(String pattern) {
        return execute(e->{
            return this.doScan(pattern);
        });
    }


    protected Set<String> doScan(String pattern) {
        Set<String> resultSet = new ConcurrentHashSet<>();
        String cursor = String.valueOf(0);
        try {
            do {
                ScanParams params = new ScanParams();
                params.count(300);
                params.match(pattern);
                ScanResult<String> scanResult = jedis.scan(cursor, params);
                cursor = scanResult.getCursor();
                resultSet.addAll(scanResult.getResult());
            } while (Integer.valueOf(cursor) > 0);
        } catch (NumberFormatException e) {
            log.error("doScan NumberFormatException:{}", e);
        } catch (Exception e) {
            log.error("doScan Exception :{}", e);
        }
        return resultSet;
    }


    protected void handleException(JedisException e) {
        if (e instanceof JedisConnectionException) {
            log.error("redis connection exception:{}", e);
        } else if (e instanceof JedisDataException) {
            log.error("jedis data exception:{}", e);
        } else {
            log.error("jedis exception:{}", e);
        }
    }

}

新增测试类


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.util.IdUtil;
import com.example.jedis.common.JedisTemplate;
import com.example.jedis.configuration.RedisConfiguration;
import com.example.jedis.model.UserDto;
import com.example.jedis.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

@SpringBootTest
//@ContextConfiguration(classes = RedisConfiguration.class)
//@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
@Slf4j
class SpringbootJedisApplicationTests {

  
    @Autowired
    JedisUtil jedisUtil;

 
    @Test
    void testCrud() {
        IntStream.range(0,100000).forEach(e->{
            final UserDto userDto = UserDto.builder()
                    .id(IdUtil.getSnowflake().nextId())
                    .name("用户1")
                    .gender(UserDto.Gender.MALE)
                    .build();
            userRepository.save(userDto);
        });
    }

 


    @Test
    void testKeys() {
        TimeInterval timeInterval = DateUtil.timer();
        Set<String> setData = jedisUitil.keys("user:*");
        System.out.println("keys use:"+timeInterval.intervalRestart()+"ms");
        Set<String> setDataScan = jedisUitil.scan("user:*");
        System.out.println("scan use:"+timeInterval.intervalRestart()+"ms");
    }
}

使用了3千多额数据,测试keys *scan其实查询效率差别不大的,scan命令效率和分多少数量一批次也有关系
在这里插入图片描述

搞到一万的数据量

在这里插入图片描述
经过测试,scan查询效率并不一定是比keys * 快多少的,跟这个数据量和count批次有关系,需要自己调试,所以对于线上的业务场景,如果key数量很多的,可以使用集合来替换keys *


http://www.kler.cn/a/159468.html

相关文章:

  • 操作系统实验:在linux下用c语言模拟进程调度算法程序
  • 前端(3)——快速入门JaveScript
  • 使用阿里云快速搭建 DataLight 平台
  • 使用c#实现TCP客户端与服务器端
  • C# Winform--SerialPort串口通讯(ASCII码发送)
  • 【代码审计】常见漏洞专项审计-业务逻辑漏洞审计
  • 【开发问题解决方法记录】04.dian 权限表单优化
  • 【图像拼接】论文精读:Quasi-Homography Warps in Image Stitching(QHW)
  • 【PTA题目】6-4 走台阶方法 分数 15
  • 【1day】Panabit 日志系统openid接口SQL注入漏洞
  • el-table全部选择和全部取消
  • 大语言模型有那些能力和应用
  • C语言--每日选择题--Day37
  • 【android开发-11】android中创建自定义布局并引入自定义布局的参考例子
  • ctfshow sql 186-190
  • css实现最简单的3d透视效果,通过旋转可以直观感受到
  • Python源码15:使用海龟画图turtle画小黄人
  • 基于SpringBoot的仓库管理系统设计与实现附带源码和论文
  • 企企通供应链专家:拥抱智能化趋势,企业如何打造智慧供应链形成竞争优势?
  • 题目:小明的彩灯(蓝桥OJ 1276)
  • 三 STM32F4使用Sys_Tick 实现微秒定时器和延时
  • quickapp_快应用_系统接口应用
  • 咨询室游戏
  • java基础之String类
  • Django回顾【三】
  • 西南科技大学C++程序设计实验七(继承与派生二)