1

I am following an article here and I think I want to go to hash route but I am confused as to what if I have multiple 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>";

how would I create a database that has all the data about taxi_cars in redis. Now I know there is no database but keys in redis, but I am using the relational lingo here to express myself. If I have 1000 taxi_cars I do not want to have 1000 initial keys. It has to be subset of something. I am not sure how to even explain this.

EDIT

so lets say that under brand I have Toyota, Honda, Suzuki and then under those I have different styles like Tacoma, Accord etc

how would I go on inserting that data and leter on retrieving it. thanks

Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221

1 Answers1

2

You should not try to map relational model concepts to a NoSQL store like Redis, but rather think in term of data structures and access paths.

Here are a number of simple examples:

Porting from SQLite to Redis

Work with keys in redis

how to have relations many to many in redis

Here you want to store records of car models. You first need something to identify them (i.e. a primary key in the relational terminology). Then you can store these records in the top level dictionary of Redis.

You should not store:

taxi_car => hash{ brand" => "Toyota", "model" => "Yaris", etc ... }

but:

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

Now, you need to anticipate the access paths. For instance to retrieve the cars per brand and model, you need to add extra sets (to be used as indexes):

brand:Toyota => set{ 1 2 }
brand:Tesla  => set{ 3 }
model:Yaris  => set{ 1 }
model:Prius  => set{ 2 }
model:Model_S => set{ 3 }

So you can retrieve:

# 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 ...
Community
  • 1
  • 1
Didier Spezia
  • 70,911
  • 12
  • 189
  • 154
  • thank you for detailed explanation. In mysql we have a set of databases for different projects, is there something like that for Radis. I do not want my test to be mixed up with other stuff in Redis server. How can I allocate a separate space for different projects. Kinda what we do with mysql by using different databases. thanks – Asim Zaidi Jul 07 '12 at 15:45
  • By default, you have 16 different databases per instance. Use the SELECT command to switch between databases. You can also spawn more Redis instances (it is a very efficient way to isolate data), and use different connections. – Didier Spezia Jul 07 '12 at 15:49
  • hmm not sure how to spin up a new server. Can you please explain or guide to a documantation. thanks – Asim Zaidi Jul 07 '12 at 16:21
  • can I not use keyspace? if I use keyspace like dbTest: how would that change the above solution? – Asim Zaidi Jul 07 '12 at 16:53
  • You can certainly use key prefixing, but it does not really isolate data. Using : as a prefix separator is a pure convention. – Didier Spezia Jul 07 '12 at 17:03
  • To run two instances on the same box, you need to duplicate the configuration file, and change the instance specific parameters (such as port, pidfile, unixsocket, dbfilename, etc ...). Launch another redis-server with your second configuration file as a parameter. – Didier Spezia Jul 07 '12 at 17:05