安装 :pip install redis
#coding=utf-8
#导入
import redis
#隐式链接
r = redis.StrictRedis()
#显式链接
# r = redis.StrictRedis(host='127.0.0.1',port=6379,db=0)
#get 和set设置
r.set('color','red')
r.get('color')
#HMSET 和 HGETALL 使用
r.hmset('dict',{'name':'anaf'})
people = r.hgetall('dict')
print people #显示出字典{'name':'anaf'}
#管道的使用方式和事务相同,但是创建的时候需要加上参数transaction=False
pipe = r.pipeline(transaction=False)
#事务和管道还支持链式调用
result = r.pipeline().set('color','red'),.get('color').execute()
在线好友
online_user.py
#coding=utf-8
'''
实现在线用户的统计 源自http://flask.pocoo.org/snippets/71/
只是显示了统计ip 并没有显示个数 还没有count个数
time:2017-06-19
author:anaf
'''
from app import redis_store
from datetime import datetime
import time
from flask import current_app
def mark_online(user_id):
now = int(time.time())
expires = now + (current_app.config['ONLINE_LAST_MINUTES'] * 60) + 10
all_users_key = 'online-users/%d' % (now // 60)
user_key = 'user-activity/%s' % user_id
p = redis_store.pipeline()
p.sadd(all_users_key, user_id)
p.set(user_key, now)
p.expireat(all_users_key, expires)
p.expireat(user_key, expires)
p.execute()
def get_user_last_activity(user_id):
last_active = redis_store.get('user-activity/%s' % user_id)
if last_active is None:
return None
return datetime.utcfromtimestamp(int(last_active))
def get_online_users():
current = int(time.time()) // 60
minutes = xrange(current_app.config['ONLINE_LAST_MINUTES'])
return redis_store.sunion(['online-users/%d' % (current - x) for x in minutes])
main.py
@main.route('/online')
def online():
return Response('Online: %s' % ', '.join(get_online_users()),mimetype='text/plain')
效果:Online: 192.168.2.17, 127.0.0.1