要实现类似于类似条件的行为,可以执行以下操作:
解决方案 1:
示例数据集
In - this - case - 3
Other - items - 2
This - is - an - 5
Lorem - ipsum - 3
In - other - terms - 2
Ans 1:列表或集合可以用作数据结构,具体取决于使用情况。在您的情况下,存在重复键(“In”),因此使用列表。
Ans 2:如何使用列表:
请记住 Redis 列表的行为类似于链表。
$ redis-cli lpush In.list "In - this - case - 3"
OK
$ redis-cli lpush Other.list "Other - items - 2"
OK
$ redis-cli lpush This.list "This - is - an - 5"
OK
$ redis-cli lpush Lorem.list "Lorem - ipsum - 3"
OK
$ redis-cli lpush In.list "In - other - terms - 2"
OK
$redis-cli lrange In.list 0 -1
1) "In - other - terms - 2"
2) "In - this - case - 3"
解决方案 2:
其他解决方案将再次使用列表:
我们将有四个主要列表,它们的行为类似于数据库中的列和单独的单词列表,这些列表将存储它们在主键列表中出现的索引。
样本数据可以描述为:
索引 Column1 Column2 Column3 Column4
1 In this case 3
2 Other items " " 2
3 This is an 5
4 Lorem ipsum " " 3
5 In other terms 2
如果最多返回 4 个值,则此描述有效。我们也可以有一个动态列。
对于动态列,第 1 列将是键,第 2 键将是数字部分,其余列将具有字符串。
索引 Column1 Column2 Column3 Column4 Column5
1 In 3 this case " "
2 Other 2 items " " " "
3 This 5 an " " " "
4 Lorem 3 ipsum " " " "
5 In 2 other terms " "
6 Hello 4 world ! !
继续使用固定的 4 列解决方案:
//first row
$ redis-cli lpush column1 "In"
1
$ redis-cli lpush In.list 1
1
$ redis-cli lpush column2 "this"
1
$ redis-cli lpush column3 "case"
1
$ redis-cli lpush column4 3
1
//second row
$ redis-cli lpush column1 "Other"
2
$ redis-cli lpush Other.list 2
1
$ redis-cli lpush column2 "items"
2
$ redis-cli lpush column3 " "
2
$ redis-cli lpush column4 2
2
//on same lines add 3rd, 4th row and then 5th row
$ redis-cli lpush column1 "In"
5
$ redis-cli lpush In.list 5
2
$ redis-cli lpush column2 "items"
5
$ redis-cli lpush column3 " "
5
$ redis-cli lpush column4 2
5
To fetch data you can do something like :
$ redis-cli lrange In.list 0 -1
1) 5
2) 1
Using these to values as index query columns as
$redis-cli lindex column1 5
"In"
$redis-cli lindex column2 5
"other"
$redis-cli lindex column3 5
"terms"
$redis-cli lindex column4 5
2
但是在第二种解决方案中,我们引入了在单独的列表中插入每个字符串的成本,但是您可以使用批量操作来执行它们。
此外,我们保存空白以具有明确定义的行类型实现。
解决方案 3:
为每一行创建结构并将它们序列化并将它们存储在特定的键列表中。
第 1 行“在,这个,案例,3”
lpush In.list StructureRepresent1stRow
如果您想使用结构并且要存储复杂的值,则可以选择此解决方案。