Chinaunix首页 | 论坛 | 博客
  • 博客访问: 91490
  • 博文数量: 13
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 190
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-18 14:35
文章分类
文章存档

2017年(1)

2016年(5)

2015年(1)

2013年(6)

我的朋友

分类: Python/Ruby

2017-01-14 10:12:23

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)


阅读(2192) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~