将自定义对象列表添加到 Redis

Add custom object list to Redis

我是Redis的新手。我想在 Redis 缓存中存储和搜索自定义对象列表。 自定义对象有 4 个属性

  1. 配置键
  2. 配置范围
  3. 值类型
  4. 配置值

示例自定义对象

{"configScope":"country","configValue":"india","configKey":"country","valueType":"string"}
{"configScope":"integer","configValue":"3","configKey":"integer","valueType":"string"}
{"configScope":"sport","configValue":"sport","configKey":"sport","valueType":"string"}
{"configScope":"country","configValue":"india","configKey":"country","valueType":"string"}

无法理解如何存储这些对象,因为我可以有效地搜索基于字符串的 configKey 或 configScope 或 configValue。

已经编写了示例代码,但它只是根据确切的键给出结果

for (CustomObject model : list) {
    CustomObject ec = (CustomObject) model;
    syncCommands.lpush("orgId:EC:"+count++, ec.toString());
}

KeyScanCursor<String> cursor = syncCommands.scan(ScanArgs.Builder.limit(50).match("orgId:EC:10"));

while (!cursor.isFinished()) {
    for (String key : cursor.getKeys()) {
        List<String> value = syncCommands.lrange(key, 0, 50);
        System.out.println(key + " - " + value);
    }
   cursor = syncCommands.scan(cursor, 
   ScanArgs.Builder.limit(50).match("orgId:EC:10"));
}

任何想法或参考都会有所帮助。

您可以尝试看看 redis 词典索引是否对您的情况有帮助,例如下面的示例文档可以存储在 redis 排序集中并对其进行 Lex 搜索。

{"configScope":"country","configValue":"india","configKey":"country","valueType":"string"}

{"configScope":"country","configValue":"russia","configKey":"country","valueType":"string"}

127.0.0.1:6379> zadd cs:country 0 cv:russia:ck:country:vt:string 0 ck:country:cv:russia:vt:string
(integer) 2
127.0.0.1:6379> zadd cs:country 0 cv:india:ck:country:vt:string 0 ck:country:cv:india:vt:string
(integer) 2

现在要搜索 configScope country 和 configValue india,您可以执行以下搜索

127.0.0.1:6379> zrangebylex cs:country "[cv:india" "(cv:india~"
1) "cv:india:ck:country:vt:string"

并且类似于使用 configKey country

搜索 configScope country
127.0.0.1:6379> zrangebylex cs:country "[ck:country" "(ck:country~"
1) "ck:country:cv:india:vt:string"
2) "ck:country:cv:russia:vt:string"

我希望这能帮助您开始使用这种方法,以获取有关 redis 中词典索引的更多信息Secondary Indexing with Redis