我在这里关注一篇文章,我想我想去哈希路线,但我很困惑,如果我有多个taxi_car
<?php
$redis->hset("taxi_car", "brand", "Toyota");
$redis->hset("taxi_car", "model", "Yaris");
$redis->hset("taxi_car", "license number", "RO-01-PHP");
$redis->hset("taxi_car", "year of fabrication", 2010);
$redis->hset("taxi_car", "nr_starts", 0);
/*
$redis->hmset("taxi_car", array(
"brand" => "Toyota",
"model" => "Yaris",
"license number" => "RO-01-PHP",
"year of fabrication" => 2010,
"nr_stats" => 0)
);
*/
echo "License number: " .
$redis->hget("taxi_car", "license number") . "<br>";
// remove license number
$redis->hdel("taxi_car", "license number");
// increment number of starts
$redis->hincrby("taxi_car", "nr_starts", 1);
$taxi_car = $redis->hgetall("taxi_car");
echo "All info about taxi car";
echo "<pre>";
var_dump($taxi_car);
echo "</pre>";
如何在redis中创建一个包含有关taxi_cars的所有数据的数据库。现在我知道redis中没有数据库而是密钥,但我在这里使用关系术语来表达自己。如果我有1000辆出租车,我不想要1000个初始钥匙。它必须是某种东西的子集。我不知道怎么解释这个。
修改
所以我想说,在品牌下我有丰田,本田,铃木 然后在那些我有不同的风格,如塔科马,雅阁等
我将如何继续插入数据并检索它。 感谢
答案 0 :(得分:2)
您不应该尝试将关系模型概念映射到像Redis这样的NoSQL存储,而应该考虑数据结构和访问路径。
以下是一些简单的例子:
how to have relations many to many in redis
在这里,您要存储汽车模型的记录。您首先需要一些东西来识别它们(即关系术语中的主键)。然后,您可以将这些记录存储在Redis的顶级词典中。
你不应该存储:
taxi_car => hash{ brand" => "Toyota", "model" => "Yaris", etc ... }
但:
taxi_car:1 => hash{ brand" => "Toyota", "model" => "Yaris", etc ... }
taxi_car:2 => hash{ brand" => "Toyota", "model" => "Prius", etc ... }
taxi_car:3 => hash{ brand" => "Tesla", "model" => "Model_S", etc ... }
现在,您需要预测访问路径。例如,要检索每个品牌和型号的汽车,您需要添加额外的集合(用作索引):
brand:Toyota => set{ 1 2 }
brand:Tesla => set{ 3 }
model:Yaris => set{ 1 }
model:Prius => set{ 2 }
model:Model_S => set{ 3 }
所以你可以检索:
# Per brand
SMEMBERS brand:Toyota
-> returns 1 2
HGETALL taxi_car:1
HGETALL taxi_car:2
# Per model
SMEMBERS model:Prius
-> returns 2
HGETALL taxi_car:2
# Per brand and per model (a bit useless here), plus associated data
# sort is used to get all the taxi_car data in one shot
sinterstore tmp brand:Toyota model:Prius
sort tmp by nosort get taxi_car:*->brand get taxi_car:*->model etc ...