import redis 通过python操作redis缓存
pool = redis.ConnectionPool(host='192.168.19.130', port=6379) host是redis主机,需要redis服务端和客户端都起着 redis默认端口是6379
r = redis.Redis(connection_pool=pool)
r.set('foo', 'Bar') key是"foo" value是"bar" 将键值对存入redis缓存
print r.get('foo') Bar 取出键foo对应的值
订阅和发布
# coding=utf-8
'''
Created on 2015-9-9
@author: kwsy
'''
import redis
pool=redis.ConnectionPool(host='192.168.1.149',port=6379,db=1)
r = redis.StrictRedis(connection_pool=pool)
p = r.pubsub()
p.subscribe('excelFile')
for item in p.listen():
print item
if item['type'] == 'message':
data =item['data']
r.set('s',32)
print data
if item['data']=='over':
break;
p.unsubscribe('spub')
print '取消订阅'
发布:
# coding=utf-8
'''
Created on 2015-9-9
@author: Administrator
'''
import redis
pool=redis.ConnectionPool(host='192.168.1.142',port=6379,db=0)
r = redis.StrictRedis(connection_pool=pool)
while True:
input = raw_input("publish:")
if input == 'over':
print '停止发布'
break;
r.publish('spub', input)
阅读(2232) | 评论(0) | 转发(0) |