Home  >  Q&A  >  body text

如何删除redis的hash结构中第一个域?

例如:

user:1 name "zhangsan"
user:1 num "001"

user:2 name "lisi"
user:2 num "002"

user:3 name "wang"
user:3 num "003"

在不指定user:1的情况下删除第一条域user:1。
其实需求类似list中的lpop,但是还是现在需要hash这种数据结构来记录id。

PHP中文网PHP中文网2736 days ago723

reply all(3)I'll reply

  • 大家讲道理

    大家讲道理2017-04-24 09:14:30

    The hash structure has no order, so you don’t know what the first item is. How did you delete the first item? The first item you think is just the first item in the order you inserted, but I’m sorry that hash does not Sort according to your insertion order, so even if you traverse the hash, you may not be able to get the result you want.
    If you want the traversal order to remain consistent with the insertion order, you can use list.
    Your needs can be realized through two structures. One list is used to maintain the collection order, and the other hash is used to save k-v data. When deleting, pop out a data from the list, and then delete it according to the key in the hash.

    reply
    0
  • PHPz

    PHPz2017-04-24 09:14:30

    Well, we can only use hKeys to retrieve all key values, then retrieve the first key at the application layer, and then hget and hdel at the same time

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-24 09:14:30

    Hash linked list, //Delete a single entity
    $redis->hDel('hashkey', 'key1');

    //Delete the entire hash
    $redis->del('hashkey');

    To delete a redis key, use the del method. Whether it is string, hash, list, set, etc., it is the same. RPOP can also be taken out.

    reply
    0
  • Cancelreply