Model
To request data from Redis in the same way as:
select * from mytable where snapshot = ? select * from mytable where id = ?
You will need the model below.
Note: select * from mytable where snapshot = ? and id = ? select * from mytable where snapshot = ? and id = ? doesn't make much sense here, as it matches select * from mytable where id = ? .
Type and purpose of names
[Key Type] [Key name pattern] HASH d:{id} ZSET d:ByInsertionDate SET d:BySnapshot:{id}
Note. I used d: as a namespace, but you can rename it with the name of your domain model.
Data insertion
Insert a new line from Mysql into Redis:
hmset d:2989 id 2989 email example-2989@example.com name fake-name-2989 ... snapshot 1134 zadd d:ByInsertionDate {current_timestamp} d:2989 sadd d:BySnapshot:1134 d:2989
Another example:
hmset d:2990 id 2990 email example-2990@example.com name fake-name-2990 ... snapshot 1134 zadd d:ByInsertionDate {current_timestamp} d:2990 sadd d:BySnapshot:1134 d:2990
Cron
Here is an algorithm that should run every day or week depending on your requirements:
for key_name in redis(ZREVRANGEBYSCORE d:ByInsertionDate -inf {timestamp_one_week_ago})
Using
select * from my_other_table where snapshot = 1134; will either:
{snapshot_id} = 1134 for key_name in redis(smembers d:BySnapshot:{snapshot_id}) print(redis(hgetall {keyname}))
or write a lua script to do it right on the redis side. Finally:
select * from my_other_table where id = 2989; will be:
{id} = 2989 print(redis(hgetall d:{id}))
Import
This part is pretty simple, just read the table and follow the model above. Depending on your requirements, you can import all (or part) of your data using the hourly / daily / weekly cron.