英文:
Issue with HSET command in Rust Redis
问题
我目前正在尝试将以下Redis命令转换为Rust:
HSET book_store_list 1 1,2,3
基本上,创建bookstore_list哈希,具有字段(1)和值(1,2,3),在Redis GUI中可以正常工作。
问题在于rust redis中的hset API似乎不按照我的预期工作。
例如:
let vector = vec![1,2,3]
hset("book_store_list", 1, &vector)
导致book_store_list哈希中有两个字段值条目
字段1 值1
字段2 值3
问题在于redis rust库似乎将[1,2,3]向量解释为单独的值(2和3成为新的字段值对)而不是一个值。只是想知道是否有人在之前遇到过这个问题?
英文:
I am currently trying to convert the following Redis command into rust:
HSET book_store_list 1 1,2,3
Essentially, create the bookstore_list Hash, with a Field (1) and value (1,2,3) which works fine within the Redis GUI.
The issue is that the hset API in rust redis does not seem to work as I envisioned.
For example:
let vector = vec![1,2,3]
hset("book_store_list", 1, &vector)
Results in two Field-Value entries within the book_store_list hash
Field 1 Value 1
Field 2 Value 3
The issue is that the redis rust crate seems to interpret the [1,2,3] vector to be separate values (with 2 and 3 being a new field value pair) to be cached in instead of one value. Just wondering if anyone has faced this issue before?
答案1
得分: 0
相信我已经找到了答案。为了使 Redis 将 [1,2,3] 视为一个单一值,我们必须像这样从 serde_json
crate 调用 to_string
API:
let value = serde_json::to_string(&vector).unwrap();
我猜这个教训就是在调用 Rust Redis 中的任何 HSET 命令之前将所有值转为字符串!
英文:
Believe that I have found the answer. In order for Redis to accept [1,2,3] as one singular value, we must call the to_string
api from the serde_json
crate like so:
let value = serde_json::to_string(&vector).unwrap();
I guess the lesson of this is to stringify all values before invoking any HSET commands in rust redis!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论