gpt4 book ai didi

redis - 用Redis命令搜索具有特定字段值的条目是什么?

转载 作者:行者123 更新时间:2023-12-03 06:42:30 26 4
gpt4 key购买 nike

在Redis中,我有一个分支(cache:ID)和其中的多个哈希(例如一些键:cache:123457cache:563457)。

条目具有以下字段:id(又名主键)和sign(仅标签)。

我想在Redis中搜索并找到所有字段sign等于a的条目。性能不是问题。我只需要验证这些条目是否存在。我了解我需要某种方式遍历所有条目,但是无法为此找到正确的命令。我检查了KEYS(KEYS高速缓存:*-但是下一步是什么?),SCAN,但运气不佳。在SQL中,我将执行以下操作:select * from cache where sing='a'。因此,我不想扫描键,而是想扫描所有条目的字段。

我该怎么办?有什么建议么?

最佳答案

Redis不能像RDBMs一样工作,如果您想像select * from cache where sing='a'一样进行查询,则可以创建secondary indexes
创建新的hash时,还添加到以set命名的hash value中。我将使用sign前缀。

127.0.0.1:6379> HSET firsthash id 1 sign a
(integer) 2
127.0.0.1:6379> SADD sign:a firsthash
(integer) 1
127.0.0.1:6379> HSET secondhash id 2 sign b
(integer) 2
127.0.0.1:6379> SADD sign:b secondhash
(integer) 1
127.0.0.1:6379> HSET thirdhash id 3 sign a
(integer) 2
127.0.0.1:6379> SADD sign:a thirdhash
(integer) 1
127.0.0.1:6379> HSET fourthhash id 4 sign aa
(integer) 2
127.0.0.1:6379> SADD sign:aa fourthhash
(integer) 1
127.0.0.1:6379> SMEMBERS sign:a
1) "thirdhash"
2) "firsthash"
删除哈希时-还要从集合中删除。
127.0.0.1:6379> DEL firsthash
(integer) 1
127.0.0.1:6379> SREM sign:a firsthash
(integer) 1
127.0.0.1:6379> SMEMBERS sign:a
1) "thirdhash"
当您需要更新哈希字段的值时,请先从集合中删除,然后再添加到新字段中
127.0.0.1:6379> SREM sign:a thirdhash
(integer) 1
127.0.0.1:6379> HSET thirdhash sign newa
(integer) 0
127.0.0.1:6379> HGETALL thirdhash
1) "id"
2) "3"
3) "sign"
4) "newa"
127.0.0.1:6379> SADD sign:newa thirdhash
(integer) 1
127.0.0.1:6379> SMEMBERS sign:newa
1) "thirdhash"

关于redis - 用Redis命令搜索具有特定字段值的条目是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62261245/

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