sudo apt-get install libevent-dev然后检查是否安装成功
ll /usr/lib | grep libevent2.安装memcached
sudo apt-get install memcached
启动
memcached -d -m 64 -p 11211 -u memcache -l 127.0.0.1
各个参数的含义:
-d:以daemon的方式在后台启动运行一个守护进程 -m:分配给Memcache使用的最大内存数量,单位是MB,默认是64MB -u:设定运行Memcache的用户(memcache默认不允许以root用户登录) -l:监听的服务器IP地址 -p:设置Memcache监听的TCP端口,默认是11211(p为小写) -c:设置最大并发连接数,默认是1024 -P:设置保存Memcache的pid文件路径(P为大写) -h:显示帮助然后,查看是否启动成功:
ps -e | grep memcached关闭时使用kill命令.
3.安装python-memcached
pip install python-memcached
代码示例:
这里有一个memcache与mysql一起使用的例子,直接粘贴过来,例子还是很好懂的,可以仿照着写自己的代码:
import sys
import MySQLdb
import memcache
memc = memcache.Client(['127.0.0.1:11211'], debug=1);
try:
conn = MySQLdb.connect (host = "localhost",
user = "sakila",
passwd = "password",
db = "sakila")
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
popularfilms = memc.get('top5films')
if not popularfilms:
cursor = conn.cursor()
cursor.execute('select film_id,title from film order by rental_rate desc limit 5')
rows = cursor.fetchall()
memc.set('top5films',rows,60)
print "Updated memcached with MySQL data"
else:
print "Loaded data from memcached"
for row in popularfilms:
print "%s, %s" % (row[0], row[1])
参考:
1.http://superuser.com/questions/80724/how-to-install-libevent-via-aptitude
2.http://stackoverflow.com/questions/7636108/installing-memcached-for-a-django-project
3.http://stackoverflow.com/questions/868690/good-examples-of-python-memcache-memcached-being-used-in-python
没有评论:
发表评论