- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我将 ioredis 与 express (nodejs) 一起使用我知道有一种方法可以通过这样的模式删除键:
redis-cli KEYS "sample_pattern:*" | xargs redis-cli DEL
但是,有没有办法使用 ioredis 来做到这一点?
最佳答案
按模式删除键最直接的方法是使用keys
命令获取与模式匹配的键,然后将它们一一删除,这类似于您提供的命令行示例。下面是一个使用 ioredis 实现的示例:
var Redis = require('ioredis');
var redis = new Redis();
redis.keys('sample_pattern:*').then(function (keys) {
// Use pipeline instead of sending
// one command each time to improve the
// performance.
var pipeline = redis.pipeline();
keys.forEach(function (key) {
pipeline.del(key);
});
return pipeline.exec();
});
然而,当您的数据库有大量键(比如一百万)时,keys
将阻塞数据库几秒钟。在这种情况下,scan
更有用。 ioredis 具有 scanStream
功能,可帮助您轻松地遍历数据库:
var Redis = require('ioredis');
var redis = new Redis();
// Create a readable stream (object mode)
var stream = redis.scanStream({
match: 'sample_pattern:*'
});
stream.on('data', function (keys) {
// `keys` is an array of strings representing key names
if (keys.length) {
var pipeline = redis.pipeline();
keys.forEach(function (key) {
pipeline.del(key);
});
pipeline.exec();
}
});
stream.on('end', function () {
console.log('done');
});
不要忘记查看scan
命令的官方文档以获取更多信息:http://redis.io/commands/scan .
关于node.js - ioredis 按模式删除所有键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35968537/
我正在遵循 Jest 文档 here 中的手动模拟示例 我正在尝试将此示例扩展到我自己的项目和 ioredis 的手动模拟 (mocks/ioredis.js)。我正在尝试用我自己的 ioredis
我正在遵循 Jest 文档 here 中的手动模拟示例 我正在尝试将此示例扩展到我自己的项目和 ioredis 的手动模拟 (mocks/ioredis.js)。我正在尝试用我自己的 ioredis
当使用 ioredis ( https://github.com/luin/ioredis ) 连接到 redis 集群时,您只需要指定一个节点,例如三节点集群 127.0.0.1:7000127.0
我正在尝试优雅地处理 Redis 错误,以便绕过错误并改为执行其他操作,而不是让我的应用程序崩溃。 但到目前为止,我不能只捕获 ioredis 抛出的异常,它绕过了我的 try/catch 并终止了当
我将 ioredis 与 express (nodejs) 一起使用我知道有一种方法可以通过这样的模式删除键: redis-cli KEYS "sample_pattern:*" | xargs re
我目前正在尝试从运行我的应用程序的服务器连接到存储在另一个实例上的我的 Redis 集群。我正在使用 IoRedis 在我的应用程序和我的 Redis 实例之间建立接口(interface),并且当只
这是我的代码: const Redis = require('ioredis'); const client = new Redis(); // multi set client.mset({'key
我正在使用ioredis。 为了防止巨大的缓冲区可能会使我的应用程序崩溃,我想在redis关闭时忽略请求,并捕获这些请求。 有什么办法可以实现? 最佳答案 您可以使用circuit breaker d
我想用键匹配模式“LOGIN::”搜索 Redis 数据库。我在我的应用程序中使用 ioredis。昨天我搜索了整个网络,我得到了一些可以完成这项工作的选项,如下所示: 按键 扫描流 问题: impo
我正在尝试为 ioredis nodejs 中的一组值设置过期时间。我可以使用此命令将一组添加到 reids redis.sadd('set', 1, 3, 5, 7); 但是我需要为这个集合设置一个
我很难在我的 typescript 测试中模拟第三方库。 我正在基于这个 typescript-starter 创建一个库图书馆。它使用 ava 进行测试。 在我的例子中,我试图模拟 ioredis
我正在使用 ioredis 模块。 var Redis = require('ioredis'); var redis = new Redis(); 在执行 new Redis() 时,与 Redis
在我设置或获取 key 之前,是否有任何方法可以检查 weather redis 是否可用或由于某些问题而终止?我怎样才能做到这一点? 我正在使用 ioredis 模块。 最佳答案 var Redi
const Redis = require('ioredis'); const sub = new Redis(); const pub = new Redis(); sub.on('subscrib
我将 ioredis 客户端 (@4.6.2) 与 node.js 一起使用,我需要做很多位操作(它们不相互依赖)。像这样: import * as ioredis from "ioredis"; .
我有一个基于 Laravel 构建的应用程序。我正在使用 Laradock 并尝试使用 Redis 容器,但我遇到了连接问题。 使用命令: docker inspect laradock_redis_
我正在使用 ioredis我想返回下面示例中的路径和值,一直到匿名函数。 console.log( function (jsonGraphArg) { return Redis
我正在尝试在 Node 脚本的 ioredis 中使用 hget 和 hset,我查看了文档,但找不到如何操作,知道如何操作吗? 谢谢, 最佳答案 这已在评论中得到解答,但对于 future 的搜索引
我想使用 Redis 位图来表示值。 如果我有一个整数值,那么我希望能够将位图的相应索引设置为 1或 true . 我还希望能够检索整个位图并遍历它以确定“1”的位置。 有一个bitfield dat
我们希望我们的 Redis 更具可扩展性,并且我们希望能够添加更多读取实例。 我正在尝试使用这个新的阅读器端点:https://aws.amazon.com/about-aws/whats-new/2
我是一名优秀的程序员,十分优秀!