【问题标题】:confused about Redis and how it will work对 Redis 及其工作方式感到困惑
【发布时间】:2012-07-07 15:57:34
【问题描述】:

我在这里关注一篇文章,我想我想去哈希路由,但我很困惑如果我有多个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 个taxi_cars,我不想有 1000 个初始键。它必须是某些东西的子集。我什至不知道如何解释这一点。

编辑

可以这么说,在品牌下我有丰田、本田、铃木 然后在那些我有不同的风格,比如塔科马,雅阁等

我将如何继续插入该数据并检索它。 谢谢

【问题讨论】:

    标签: redis


    【解决方案1】:

    您不应尝试将关系模型概念映射到 Redis 之类的 NoSQL 存储,而应考虑数据结构和访问路径。

    这里有一些简单的例子:

    Porting from SQLite to Redis

    Work with keys in redis

    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 ...
    

    【讨论】:

    • 感谢您的详细解释。在 mysql 中,我们有一组用于不同项目的数据库,Radis 是否有类似的东西。我不希望我的测试与 Redis 服务器中的其他内容混在一起。如何为不同的项目分配单独的空间。有点像我们通过使用不同的数据库对 mysql 所做的事情。谢谢
    • 默认情况下,每个实例有 16 个不同的数据库。使用 SELECT 命令在数据库之间切换。您还可以生成更多 Redis 实例(这是一种非常有效的数据隔离方式),并使用不同的连接。
    • 嗯不确定如何启动新服务器。您能否解释或指导文档。谢谢
    • 我可以不使用键空间吗?如果我使用像 dbTest 这样的键空间:那将如何改变上述解决方案?
    • 您当然可以使用键前缀,但它并不能真正隔离数据。使用 : 作为前缀分隔符是一种纯粹的约定。
    猜你喜欢
    • 2012-08-24
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    相关资源
    最近更新 更多