Home > Database > Mysql Tutorial > body text

python对redis进行实时用户数统计

WBOY
Release: 2016-06-07 17:38:14
Original
1010 people have browsed it

安装redis#wgethttp://redis.googlecode.com/files/redis-2.6.14.tar.gz#tarxzfredis-2.6.14.tar.gz#cdredis-2.6.14#make执行make的时候报错,具体报错信息如下

安装redis

#wget #tar xzf redis-2.6.14.tar.gz #cd redis-2.6.14 #make

执行make的时候报错,具体报错信息如下:

zmalloc.o: In function `zmalloc_used_memory': /usr/local/redis-2.6.14/src/zmalloc.c:223: undefined reference to `__sync_add_and_fetch_4' collect2: ld returned 1 exit status make[1]: *** [redis-server] Error 1 make[1]: Leaving directory `/usr/local/redis-2.6.14/src

在make操作的时候加一个执行参数:makeCFLAGS="-march=i686"

然后就不报错了。
编译的可执行文件在src目录中,启动运行Redis:

# src/redis-server

29242873_1382003181t2Q3.jpg

如图正确启动后,安装redis-py

git clone https://github.com/andymccurdy/redis-py.git cd redis-py python setup.py install

python操作在线用户数的简单代码:

#!/usr/bin/python import redis r=redis.StrictRedis(host='192.168.39.138',port=6379) r.set('online','0') r.incr('online') r.incr('online') r.incr('online') r.decr('online') print r.get('online')

如果有新的用户登录incr插入一条记录,如果有用户下线可以使用decr或者规定一个超时时间,对不活跃的用户自动删除记录。

#./redis.py 2

模拟测试,,当前有三个用户登录变为在线,同时一个用户离线,所以显示在线用户为2人,实时性更高。

本文出自 “老徐的私房菜” 博客,谢绝转载!

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