Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to turn a MySQL table into a Redis equivalent?

Is there an easy way to turn a mysql table into a redis equivalent?

I have a myisam table in MySQL that is basically used as a key-value store that I want to "move" to Redis so it will be super fast. Is there an easy way to do this?

Thanks.

like image 319
eric Avatar asked Apr 12 '11 04:04

eric


1 Answers

The easiest way is to get a dump of the mysql table and parse the relevant data entries into redis commands.

For example, a data dump would produce something like the following:

CREATE TABLE carousel(
  id int(11),
  path varchar(200),
  title varchar(200),
  comments varchar(200)
);

INSERT INTO carousel VALUES (3,'7.jpg','Inspirar','inspiration');
INSERT INTO carousel VALUES (4,'d.jpg','Pilotar','pilotar');
INSERT INTO carousel VALUES (5,'8.jpg','Sentir','sentir');
INSERT INTO carousel VALUES (6,'6.jpg','Volar','volar');

First you need to decide on the key structure you want to use in redis. One idea is to use the table as the key and store the ids of each row in a set.

For each table you would need to create a key that will hold the ids, lets call them idx:$table. Using our example we would create idx:carousel.

As we parse the file, we would pull the ids from the first column of values (in this case) and store them in idx:carousel. Also we would store each INSERT as a hash. To do this we name the key carousel:$id and use the command hmset. The first INSERT in the example would be stored like this:

hmset carousel:3 path '7.jpg' title 'Inspirar' comments 'inspiration'

It probably sounds more complicated than it actually is, but it is quite straightforward. If you think it is a little too difficult I am willing to write one for you.

Hope that helps.

like image 65
Lloyd Moore Avatar answered Oct 12 '22 15:10

Lloyd Moore