编写查询以向 REDIS 哈希中的键添加多个值? [英] Writing a query to add multiple values to a key in REDIS Hashes?

查看:72
本文介绍了编写查询以向 REDIS 哈希中的键添加多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了 REDIS Hashes 上的命令列表.是否可以将多个值分配给 REDIS 中的一个哈希键?例如,我试图以散列的形式表示下表.

I went through the command list on REDIS Hashes.Is it possible to assign multiple values to a hash key in REDIS? For instance,I am trying to represent the following table in form of a hash.

 Prod_Color  |   Prod_Count  |   Prod_Price   |   Prod_Info
------------------------------------------------------------
  Red        |       12      |       300      |   In Stock
  Blue       |        8      |       310      |   In Stock

我随后尝试了以下哈希命令

I tried the following hash commands subsequently

HMSET Records Prod_Color "Red" Prod_Count 12 Prod_Price 300 Prod_Info "In Stock"

HMSET Records Prod_Color "Blue" Prod_Count 8 Prod_Price 310 Prod_Info "In Stock"

但是,当我尝试使用命令 HGETALL Records 检索散列时,我只看到第二行插入的值(即 Blue,8,310,In Stock)!我知道我可以创建一个单独的散列并插入第二行值,但是,我打算将所有值插入一个散列中.

However,when I try to retrieve the hash using the command HGETALL Records, I am seeing only the second row of inserted values(i.e. Blue,8,310,In Stock)! I understand that I can create a separate hash and insert the second row of values,however, I intend to insert all the values in a single hash.

推荐答案

你可以做的,我在其他地方看到的,除了我的代码,是使用后缀来键入散列.您可能有一个后缀来标识每条记录,我将在此处使用颜色:

What you could do, and I saw this in other places besides my code, is to key the hash using a suffix. You probably have a suffix which identifies each record, I will use the colors here:

插入时间:

HMSET Records:red Prod_Color "Red" Prod_Count 12 Prod_Price 300 Prod_Info "In Stock"
HMSET Records:blue Prod_Color "Blue" Prod_Count 8 Prod_Price 310 Prod_Info "In Stock"

/* For each HMSET above, you issue SADD */
SADD Records:Ids red
SADD Records:Ids blue

查询时间:

/* If you want to get all products, you first get all members */
SMEMBERS Records:Ids

/* ... and then for each member, suppose its suffix is ID_OF_MEMBER */
HGETALL Records:ID_OF_MEMBER

/* ... and then for red and blue (example) */
HGETALL Records:red
HGETALL Records:blue

您可能希望使用 primary key 作为后缀,因为这应该可以从关系数据库记录中获得.此外,在删除哈希键(例如 DEL Records:red)时,您必须维护成员集(例如 SREM Records:Ids red).还请记住,Redis 作为改进的缓存确实非常出色,您必须对其进行良好设置以保留值(并以此保持性能).

You probably want to use the primary key as the suffix, since this should be available to you from the relational database records. Also, you must maintain the set of members (e.g. SREM Records:Ids red), when deleting hash keys (e.g. DEL Records:red). And also remember that Redis is really good as an improved cache, you must set it up well to persist values (and maintain performance with that).

这篇关于编写查询以向 REDIS 哈希中的键添加多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆