python cache MySQL_Python Redis [How to] : Cache Python MySQL Result using Redis

本文介绍如何利用Redis的内存存储特性来缓存Python从MySQL获取的数据,以降低I/O操作并提高应用速度。通过检查Redis中是否存在查询键,如果存在则直接返回,否则执行MySQL查询并将结果序列化后存入Redis。使用cPickle进行序列化是因为其速度较快。这种方法可以显著加快新闻网站爬虫的速度。
摘要由CSDN通过智能技术生成

Cache Python MySQL Result using Redis

We’re often using MySQL so much, like read data from DB, calculate something, write data with index. And I bet You know, that process is also using your i/o machine. I prefer my CPU usage 100% than my i/o 50%.

Redis is live in memory, so it won’t do anything like read / write to disk that make your i/o process high (untill your max memory, if it’s not enough, redis create virtual memory, CMIIW). I plan to make my application faster and faster with caching using redis, and I will improve my news web crawler to going faster with redis as cache.

The code

Explanation

Check Redis Key, if exists, just return it

If not exists, do MySQL query, and set the key, then return it

Why I use serialization with cPickle? because Redis just store the string with SET Command, so, our MySQL result must serialize to string, and cPickle is faster than pickle or marshal (cmiiw).

Conclusion

This simple post is just to show how to use cache python mysql result using redis. And in my opinion, redis just awesome

Better code view

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值