python - flask使用redis做缓存的话,怎么和数据库同步或者定期更新到数据库?
PHPz
PHPz 2017-04-17 11:05:02
0
1
922

打算用redis来做缓存,但是之前没弄过类似的东西,有个疑惑就是用什么东西来让redis里的数据更新到mysql或者sqlite数据库中去呢?
是需要自己写脚本定期执行更新?还是redis能自带一些功能?

我打算每10分钟或者一定的时间内,或者每天固定的时候比如半夜2点,把redis的数据更新到mysql或者sqlite中。 有什么简单的办法嘛。

PHPz
PHPz

学习是最好的投资!

reply all(1)
刘奇

In your case, if you can accept regular import from redis to mysql, it basically means that your business does not need mysql, because redis is not just a cache, the data stuffed into it is persisted to the hard disk. You can just read it directly from redis next time.

As for cache, it is generally read cache (write cache is very verbose to implement and not very reliable), and the synchronization strategy with the database needs to be added to your own code logic.

Suppose your original code logic is as follows:

$data = get_from_db($condition);

Now you need to change get_from_db to this

function get_from_db($condition)
{
    $data = get_from_cache($condition);
    if (!$data)
    {
        $data = get_from_db_directly($condition);
        set_to_cache($condition, $data);
    }
    return $data;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template