gpt4 book ai didi

redis - 替换redis设置值的最佳方法

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

我想存一个key-values类型的数据到redis中,values是set类型,即没有重复值。我在 redis 中是这样操作的:

127.0.0.1:6379> sadd a 1 2 3
(integer) 3
127.0.0.1:6379> smembers a
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> sadd a 4 5 6
(integer) 3
127.0.0.1:6379> smembers a
1) "3"
2) "2"
3) "4"
4) "5,"
5) "1"
6) "6"

我想要用户集 (4,5,6) 替换集 (1,2,3)。

最佳答案

你的问题不明确。

如果您打算替换所有成员:

# Setup "a" with 3 members
127.0.0.1:6379> del a
(integer) 1
127.0.0.1:6379> sadd a 1 2 3
(integer) 3

# Actions - delete entire set and add 3 new members in one go
127.0.0.1:6379> multi
OK
127.0.0.1:6379> del a
QUEUED
127.0.0.1:6379> sadd a 4 5 6
QUEUED
127.0.0.1:6379> exec
1) (integer) 1
2) (integer) 3
127.0.0.1:6379> smembers a
1) "4"
2) "5"
3) "6"

如果您打算仅替换特定成员:

# Setup "a" with 4 members
127.0.0.1:6379> del a
(integer) 1
127.0.0.1:6379> sadd a 0 1 2 3
(integer) 4

# Actions - delete 3 specific members and add 3 more, leaving others unaffected
127.0.0.1:6379> multi
OK
127.0.0.1:6379> srem a 1 2 3
QUEUED
127.0.0.1:6379> sadd a 4 5 6
QUEUED
127.0.0.1:6379> exec
1) (integer) 3
2) (integer) 3
127.0.0.1:6379> smembers a
1) "0"
2) "4"
3) "5"
4) "6"
127.0.0.1:6379>

关于redis - 替换redis设置值的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56363089/

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