how to sync data from Mysql to Redis?

如何从MySQL redis数据同步?

问题 (Question)

My website mostly use Django + Mysql and sometimes Redis for some frequently access data.

My problem is how to sync data from Mysql to Redis automatically when I write data to Mysql by Django admin page.

Thank you for giving me some advice. It also will be appreciated if someone can tell me how to write data into Redis directly by Django admin page

Thank you!

我的网站大多使用Django + MySQL和有时是一些经常访问的数据。

我的问题是如何从MySQL使用自动同步数据时,我写数据到MySQL Django管理页面。

谢谢你给我一些建议。它还将不胜感激如果有人可以告诉我如何写数据到redis直接由Django管理页面

谢谢你!

最佳答案 (Best Answer)

What you want to achieve looks like using Redis as a cache. The pattern is:

Always look for the data in redis

If it's not in redis, get it from MySQL and store it in Redis with an expiration date, using the expire command

Doing it like this, you have to modify you app, not the admin page. But there could be a delay between the writing of the informations in the admin page, and their availability to the clients.

You may delete the data from the Redis cache in the admin, when storing them, to ensure the newest version will always be delivered. But you will have to modify Django admin page for this.

你想达到什么看起来像使用Redis作为缓存。模式:

一直都在寻找这样的数据

如果不使用,把它从MySQL 和存储在过期使用,利用过期的命令

这样做,你必须改变你的应用程序,而不是管理页面。但也有可能是在管理页面的信息写作之间的延迟,和它们的可用性的客户。

你可以在管理使用缓存中删除数据,当它们存储,保证最新的版本总是被传递。但你必须修改此Django管理页面。

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/22123589