gpt4 book ai didi

database - Redis中SADD的使用方法

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

我找到了这个很好的例子,我正在尝试在哈希之间建立关系,然后获取一些数据。

# Here are my categories
> hmset category:1 name cinema ... more fields ...
> hmset category:2 name music ... more fields ...
> hmset category:3 name sports ... more fields ...
> hmset category:4 name nature ... more fields ...

# Here are my users
> hmset user:1 name Jack ... more fields ...
> hmset user:2 name John ... more fields ...
> hmset user:3 name Julia ... more fields ...

# Let's establish the many-to-many relationship
# Jack likes cinema and sports
# John likes music and nature
# Julia likes cinema, music and nature

# For each category, we keep a set of reference on the users
> sadd category:1:users 1 3
> sadd category:2:users 2 3
> sadd category:3:users 1
> sadd category:4:users 2 3

# For each user, we keep a set of reference on the categories
> sadd user:1:categories 1 3
> sadd user:2:categories 2 4
> sadd user:3:categories 1 2 4


# Categories of Julia
> smembers user:3:categories
1) "1"
2) "2"
3) "4"

如果我想获取类别名称怎么办?

smembers user:3:categories name

没用?

最佳答案

在一次往返中完成此操作的一种简单方法是使用:

SORT user:3:categories BY nosort GET category:*->name

关于database - Redis中SADD的使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28034085/

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