我记得使用node-redis中的管道功能来批处理命令并一起执行它们。该功能现在已弃用吗?是否已被客户端内的自动流水线取代?
这是我如何使用管道功能的示例:
const redis = require("redis");
const client = redis.createClient();
client.on("error", (err) => {
console.error("Error:", err);
});
async function usePipelining() {
try {
await client.connect();
// Create a pipeline
const pipeline = client.pipeline();
pipeline.set("key1", "value1");
pipeline.set("key2", "value2");
pipeline.set("key3", "value3");
pipeline.get("key1");
pipeline.get("key2");
pipeline.get("key3");
// Execute the pipeline and handle results
const results = await pipeline.exec();
console.log("Pipeline results:");
results.forEach((result, index) => {
if (result[0]) {
console.error("Pipeline Error:", result[0]);
} else {
console.log(`Result ${index + 1}:`, result[1]);
}
});
} catch (err) {
console.error("Pipeline Execution Error:", err);
} finally {
client.quit();
}
}
usePipelining().catch((err) => {
console.error("Unexpected Error:", err);
});
Pipeline Execution Error: TypeError: client.pipeline is not a function
at usePipelining (/home/vinayak/test/index.cjs:13:29)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Pipeline Execution Error: TypeError: client.batch is not a function
at usePipelining (/home/vinayak/test/index.cjs:13:29)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
有人可以澄清一下管道功能是否仍然是批处理命令的推荐方式,或者我们应该依赖新版本的node-redis中的自动管道功能吗?
client.pipeline()
或 client.batch()
已被弃用吗?
我知道写在同一个“tick”上的命令会在node-redis包的新版本中自动流水线化:
const replies = await Promise.all([
client.get('a'),
client.get('b')
]);
环境详情:
node-redis 客户端的管道功能允许批处理多个命令,但不保证原子性。
Node Redis 可以自动对同一“tick”期间发出的多个请求进行分组,这意味着如果命令位于事件循环的单个周期中,则 Node-Redis 客户端可以将它们合并到单个管道中。
Node Redis 中的自动管道示例
const redis = require("redis");
const client = redis.createClient();
client.on("error", (err) => {
console.error("Redis client error", err);
});
client
.connect()
.then(() => {
console.log("Connected to Redis");
Promise.all([
client.set("key", "value"),
client.get("key"),
client.set("key1", 1),
client.incr("key1"),
client.get("key1"),
])
.then((results) => {
console.log("SET", results[0]);
console.log("GET", results[1]);
console.log("SET", results[2]);
console.log("INCR", results[3]);
console.log("GET", results[4]);
client.quit();
})
.catch((err) => {
console.error("Error setting/getting from Redis", err);
});
})
.catch((err) => {
console.error("Error connecting to Redis", err);
});
Node-redis 客户端还包含 execAsPipeline() 方法,用于手动批处理 redis 命令
使用 execAsPipeline 方法的示例
const redis = require("redis");
const client = redis.createClient();
client.on("error", (err) => {
console.error("Redis client error", err);
});
client
.connect()
.then(async () => {
console.log("Connected to Redis");
let pipeline = client.multi();
pipeline.set("key1", "value1");
pipeline.set("key2", "value2");
pipeline.get("key1");
pipeline.get("key2");
let result = await pipeline.execAsPipeline();
console.log(result);
client.quit();
})
.catch((err) => {
console.error("Error connecting to Redis", err);
});