gpt4 book ai didi

redis - 将 mget 与 redis-rs 一起使用时参数数量错误

转载 作者:IT王子 更新时间:2023-10-29 06:06:25 25 4
gpt4 key购买 nike

我正在尝试通过以下方式使用 Rust 访问 Redis:

extern crate redis;

use redis::{Client, Commands, Connection, RedisResult};

fn main() {

let redis_client = Client::open("redis://127.0.0.1/").unwrap();
let redis_conn = redis_client.get_connection().unwrap();

let mut keys_to_get = vec![];
keys_to_get.push("random_key_1".to_string());
keys_to_get.push("random_key_2".to_string());
let redis_result: String = redis_conn.get(keys_to_get).unwrap();
}

当我运行 cargo run 时,我得到:

     Running `target/debug/test_resdis`
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: An error was signalled by the server: wrong number of arguments for 'get' command', ../src/libcore/result.rs:746
note: Run with `RUST_BACKTRACE=1` for a backtrace.
error: Process didn't exit successfully: `target/debug/test_resdis` (exit code: 101)

我是做错了什么,还是一个错误?

最佳答案

针对 netcat 服务器运行您的程序会显示以下请求:

*3
$3
GET
$12
random_key_1
$12
random_key_2

GET 命令应该是 MGET


我相信这是一个 bug in the implementation :

impl<T: ToRedisArgs> ToRedisArgs for Vec<T> {
fn to_redis_args(&self) -> Vec<Vec<u8>> {
ToRedisArgs::make_arg_vec(self)
}
}

impl<'a, T: ToRedisArgs> ToRedisArgs for &'a [T] {
fn to_redis_args(&self) -> Vec<Vec<u8>> {
ToRedisArgs::make_arg_vec(*self)
}

fn is_single_arg(&self) -> bool {
ToRedisArgs::is_single_vec_arg(*self)
}
}

在幕后,该库使用 ToRedisArgs::is_single_arg 检查键类型以了解它是否是多值的,它具有 true 的默认实现。

如您所见,切片实现了 ToRedisArgs::is_single_arg,但 Vec 没有。这也提出了一种解决方法:将向量视为切片:

redis_conn.get(&*keys_to_get)

This issue has now been filed with the library .

关于redis - 将 mget 与 redis-rs 一起使用时参数数量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37995426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com