一,安装redis-py
- pip install redis
- easy_install redis
二,简单用法
import redis# 连接redis服务器def conn_redis(): r = redis.StrictRedis(host='10.21.25.196', port=6379, db=0) return r#hashdef test_hash(): r = conn_redis() # 连接redis数据库 result = r.hgetall('1861098') # 获取该字典中所有k-V对 print result.keys() # 获取所有key print result.values() # 获取所有value for key, value in result.items(): # 遍历key,value对 print key, '------>', value print result.items() # 输出所有key,value对(以元组的方式) print result.has_key('count') # 判断时候存在某个键值 return result def test_set(): r = conn_redis() # 连接redis数据库 result = r.smembers('test') # 获取该集合中所有元素# print len(result)#求集合长度# print list(result)#将set转化成list# for key in result:# print key return list(result)def test_simple(): r = conn_redis()# r.set('foo','bar') print r.get('foo') def test_pipe(): r = conn_redis() pipe = r.pipeline(transaction=False) result = pipe.set('foo', 'lky').get('foo').execute() print result def test_transaction(): r = conn_redis() pipe = r.pipeline() pipe.set('foo', 'bar') pipe.get('foo') result = pipe.execute() print result