编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

具有匹配模式的ioredis键:ioredis Key with matching Pattern

FistOfFury redis 2022-5-11 11:03 9人围观

腾讯云服务器
具有匹配模式的ioredis键的处理方法

我想用键匹配模式'LOGIN ::'搜索redis数据库.我在应用程序中使用了ioredis.昨天,我已经搜索了整个网络,但很少有选择可以完成此工作,如下所示:

I am want to search redis database with key matching pattern 'LOGIN::'. I am using ioredis in my application. yesterday I have searched the whole web and I got few options which are doing this job like as follows :

scanStream

scanStream

问题:

import Redis from 'ioredis' var Client = new Redis() Client.scanStream <---- Missing 

Redis实例中缺少

scanStream方法,或者如果我做错了,请告诉我.

scanStream method is missing in Redis instance or if I am doing something wrong then please let me know.

问题解答

所以这是示例代码,可以为您带来广泛的想法.因此,您可以使用redis提供的两件事.首先是KEYS函数,由于其O(N)复杂性,因此不应使用.此示例在流中使用扫描命令,即使用光标位置不同的流一个接一个地使用.它将扫描整个redis,但一次返回10个结果,不会像KEYS函数那样使redis停顿.我们会将所有键推入一个键数组,然后您可以遍历所有键并使用GET命令获取每个键的值.

so this is the example code that will give you a broad idea. So you can use two things that the redis offers. First is the KEYS function which shouldn't be used due to its O(N) complexity. This example is using the scan command in a stream i.e one by one with different cursor positions. It will scan the whole redis but will return 10 results at a time which will not stall the redis like the KEYS function would have. We will push all the keys to a keys array after which you can loop through all the keys and use GET command to get value of each key.

const redis = require('ioredis'); let stream = redis.scanStream({ match: "LOGIN::*", count: 10 }); stream.on("data", async (keys = []) => { let key; for (key of keys) { if (!keysArray.includes(key)) { await keysArray.push(key); } } }); stream.on("end", () => { res(keysArray); }); 

这篇关于具有匹配模式的ioredis键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^