【问题标题】:Should I use a hash for user-specific data in Redis, and if so, how?我应该对 Redis 中的用户特定数据使用哈希吗?如果是,如何使用?
【发布时间】:2014-07-25 03:28:11
【问题描述】:

我是 Redis 新手,我正在尝试找出哪种数据类型适合存储一组特定于一个用户的数据。

数据集将特定于用户 1000,因此我可以将其称为类型

1000:MatchesMembers

然后数据类型将包含其他用户的列表,例如:

1001, photo, age, name, location, city, country
1003, photo, age, name, location, city, country
1007, photo, age, name, location, city, country
1009, photo, age, name, location, city, country

我已经读到,如果可能的话,哈希类型是最好的 - Redis set vs hash

如果哈希是最好用的,我就是这样使用它的吗?

var hash = client.GetHash<string>("1000:MatchesMembers");
hash.SetValue("key", "value");

我必须单独设置每个值还是可以批量设置?

最后是否可以从散列中弹出值?每次我会录入大约 10 个条目,并希望在使用它们后将其删除。有没有办法从散列中弹出条目,还是我需要读取和删除?不同的数据类型会更适合此功能吗?

非常感谢。

【问题讨论】:

    标签: redis


    【解决方案1】:

    我无法回答语法问题,因为您没有说明您使用的是哪个 redis 驱动程序。 所以这里是基于 Redis 命令的信息。

    使用HMSET 设置与给定用户相关的所有数据。

    HMSET 1001 photo "url of the photo" age 31 name "user name" location "location name" city "city name" country "country name"
    

    使用集合来存储用户的ID

    SADD 1000:MatchesMembers 1001 1003 1007 1009
    

    要从集合中删除用户,请使用SREM

    要优化多个成员配置文件的插入,请使用pipeling。您的 Redis 驱动程序应该会处理它。

    【讨论】:

      猜你喜欢
      • 2016-10-04
      • 1970-01-01
      • 2010-11-01
      • 2015-05-17
      • 1970-01-01
      • 2012-07-02
      • 2020-06-28
      • 2020-11-13
      • 2021-12-13
      相关资源
      最近更新 更多