
pip install redis
import redisr = redis.Redis(host='127.0.0.1', port=6379, db=1, password=None, decode_responses=True)
import redispool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=1, max_connections=100, password=None, decode_responses=True)r = redis.Redis(connection_pool=pool)
flask 用的话不用在create_app注册,import下写就好,我也不知道为什么,能用就好
>>> import redis>>> conn = redis.Redis(host='192.168.8.176',port=6379)>>> pipe = conn.pipeline()>>> pipe.hset("hash_key","leizhu900516",8)Pipeline<ConnectionPool<Connection<host=192.168.8.176,port=6379,db=0>>>>>> pipe.hset("hash_key","chenhuachao",9)Pipeline<ConnectionPool<Connection<host=192.168.8.176,port=6379,db=0>>>>>> pipe.hset("hash_key","wanger",10)Pipeline<ConnectionPool<Connection<host=192.168.8.176,port=6379,db=0>>>>>> pipe.execute()[1L, 1L, 1L]>>>
这里有点质疑,应该是用连接池连接
>>> pipe.hget("hash_key","leizhu900516")Pipeline<ConnectionPool<Connection<host=192.168.8.176,port=6379,db=0>>>>>> pipe.hget("hash_key","chenhuachao")Pipeline<ConnectionPool<Connection<host=192.168.8.176,port=6379,db=0>>>>>> pipe.hget("hash_key","wanger")Pipeline<ConnectionPool<Connection<host=192.168.8.176,port=6379,db=0>>>>>> result = pipe.execute()>>> print result['8', '9', '10'] #有序的列表>>>
redis_db = redis.Redis(host='127.0.0.1',port=6379)data = ['zhangsan', 'lisi', 'wangwu']with redis_db.pipeline(transaction=False) as pipe:for i in data:pipe.zscore(self.key, i)result = pipe.execute()print result# [100, 80, 78]
pipe =conn.pipeline(transaction=False)
pipeline取值3500条数据,大约需要900ms