Java 将自定义对象列表添加到Redis
我是Redis的新手。我想在Redis缓存中存储和搜索自定义对象列表。 自定义对象有4个属性Java 将自定义对象列表添加到Redis,java,redis,lettuce,Java,Redis,Lettuce,我是Redis的新手。我想在Redis缓存中存储和搜索自定义对象列表。 自定义对象有4个属性 配置键 配置范围 值类型 配置值 示例自定义对象 {"configScope":"country","configValue":"india","configKey":"country","valueType":"string"} {"configScope":"integer","configValue":"3","configKey":"integer","valueType":"string"}
{"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"));
}
for(自定义对象模型:列表){
CustomObject ec=(CustomObject)模型;
lpush(“orgId:EC:+count++,EC.toString());
}
KeyScanCursor cursor=syncCommands.scan(ScanArgs.Builder.limit(50.match)(“orgId:EC:10”);
而(!cursor.isFinished()){
for(字符串键:cursor.getKeys()){
列表值=syncCommands.lrange(键,0,50);
System.out.println(键+“-”+值);
}
游标=同步命令。扫描(游标,
ScanArgs.Builder.limit(50.match)(“原代码: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国家/地区和configValue印度,您可以执行以下搜索
127.0.0.1:6379> zrangebylex cs:country "[cv:india" "(cv:india~"
1) "cv:india:ck:country:vt:string"
类似地,使用configKeycountry搜索configScopecountry
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中词典索引的更多信息您希望如何搜索,我指的是通过configKey或configKey与configscope的组合进行搜索?列出与结构相关的查询基本上所有字段都是可搜索的,并且彼此独立。任何一个用户都可以通过configKey、configValue或configScope进行搜索。查看redisjson模块,如果您不想使用模块,则必须使用redis散列对其进行单独索引,每个查询类型一个用于ex,hashset键可以是configScope:country,它的键是country id,值可以序列化为configobject@tabreaz您是否有关于如何单独索引它们的示例代码。从您的代码中,结果完全匹配,因为您在搜索词后面缺少*。匹配(“orgId:EC:10*”)。在搜索关键字syncCommands时,我的范围是多少。zrangebylex(关键字,范围)如答案中所述,您的范围将是“[ck:country”下限和“(ck:country~”上限,以便关键字cs:country获得configScope country.syncCommands.zrangebylex中的所有configKey国家/地区(“cs:country”,新范围)([ck:country”,“ck:country~”)long addSize1=syncCommands.zadd(“cs:country”,ScoredValue.from(1,可选的of(“cv:rusia:ck:country:vt:string”)),ScoredValue.from(2,可选的of(“ck:country:cv:rusia:vt:string”)),ScoredValue.from(4,可选的of(“cv:india:ck:country:vt:string”)),ScoredValue.from(5,可选的of(“ck:country:india:vt:string”));System.out.println(“zset=“+syncCommands.zrangebylex(“cs:country”,Range.create”([ck:country”,“ck:country~”)));`显示空结果。当使用sortedSet进行词典索引时,分数应始终为0。即使我给分数为0,仍然会得到空结果long addSize1=syncCommands.zadd(“cs:country”,ScoredValue.from(0,可选。of(“cv:russia:ck:country:vt:string”)),ScoredValue.from(0,可选。of(“ck:country:cv:russia:vt:string”)),ScoredValue.from(0,可选。of(“ck:country:cv:india:vt:string”)),System.out.println(“addSize1=”+addSize1);System.out.println(“zset=“+syncCommands.zrangebylex”(“cs:country”,Range.create(“ck:country”,“ck:country~”))));