Home > Database > Mysql Tutorial > body text

Python判断Memcached是否缓存MySQL结果

WBOY
Release: 2016-06-07 17:36:17
Original
979 people have browsed it

介绍一个生产环境中memcached的使用场景,主要是memcached存储关系型数据库mysql的查询结果,比如网站的下载排名等,这种查询每次

介绍一个生产环境中memcached的使用场景,主要是memcached存储关系型数据库mysql的查询结果,比如网站的下载排名等,这种查询每次从关系型数据库中查询,会增加磁盘的I/O开销,而这个排名不需要实时的更新,所以我们把这个结果存在memcached中,memcached是把数据序列化存放在内存中,我们可以设置超时时间,然后周期性的从关系型数据库查询新的结果更新到memcached中。

我用python来写个小的demo演示这个场景。首先python连接memcached和mysql需要加载对应的模块。程序的作用就是:先从memcached查询薪水最高的两个人,打印他们的姓名和薪水,如果没有返回结果,说明memcached中不存在,那么我们从关系型数据库mysql中去查询结果,然后更新在memcached中,再次请求,就会从memcached直接返回结果。

安装python-memcached和MySQL-python包

yum install -y python-memcached  MySQL-python

#!/usr/bin/python

import sys

import MySQLdb

import memcache

memc = memcache.Client(['127.0.0.1:11211'], debug=1);

key = memc.get("top2salary")

if key != None:

        print "Load data from Memcache : %s,%s" % (key[0], key[1])

else:

    print "Updating memcached data from MySQL."

conn = MySQLdb.connect (host = "127.0.0.1",

                            user = "root",

                            passwd = "123456",

                            db = "web_user")

cursor = conn.cursor()

cursor.execute('select * from emp order by salary desc limit 2')

rows = cursor.fetchall()

memc.set('top2salary',rows,10)


mysql的emp表查询结果:

mysql> select * from emp;

+--------------+--------+

| name        | salary |

+--------------+--------+

| Casillas    | 10500  |

| Fernández    | 350    |

| Varane      | 21000  |

| Pepe        | 16000  |

| Ramos        | 35000  |

| Nacho        | 4400  |

| Coentro      | 12500  |

| Marcelo      | 22000  |

| Carvajal    | 10500  |

| Arbeloa      | 6200  |

| Khedira      | 19500  |

| Casemiro    | 5300  |

| Xabi Alonso  | 10500  |

| Modric      | 35000  |

| Illarramendi | 16000  |

| Isco        | 31000  |

| Bale        | 70500  |

| Di María    | 26500  |

| Ronaldo      | 88000  |

| Jesé        | 13000  |

| Benzema      | 30000  |

| Morata      | 10500  |

+--------------+--------+

22 rows in set (0.00 sec)

查询当前薪水排名的两人,第一次执行memcached为空,立刻去myql中查询,缓存在memcached,再次查都是从memcached返回,我在代码里设置超时时间10秒,10秒后清除缓存再次从mysql查询,如果这个时间段内mysql有新的数据变化,如插入更高薪水的人,那么会返回当前最新的结果,一般mysql被缓存在memcached中都是变化不太频繁的结果。

[root@localhost ~]# ./memc.py

Updating memcached data from MySQL.

[root@localhost ~]# ./memc.py

Load data from Memcache : ('Ronaldo', '88000'),('Bale', '70500')

[root@localhost ~]# ./memc.py

Load data from Memcache : ('Ronaldo', '88000'),('Bale', '70500')

[root@localhost ~]# ./memc.py

Load data from Memcache : ('Ronaldo', '88000'),('Bale', '70500')

优点如果加载网页每次都查询mysql而且执行order by这样的sql语句对数据库的I/O负载增加,,同时会减慢打开页面的速度。

Python 的详细介绍:请点这里
Python 的下载地址:请点这里

推荐阅读:

《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码]

Python脚本获取Linux系统信息

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!