在Rust Redis中的HSET命令出现问题。

huangapple go评论50阅读模式
英文:

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]向量解释为单独的值(23成为新的字段值对)而不是一个值。只是想知道是否有人在之前遇到过这个问题?
英文:

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!

huangapple
  • 本文由 发表于 2023年5月28日 11:45:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定