python使用pipeline()与execute()批量读写redis

python使用pipeline()与execute()批量读写redis

1、插入数据

>>> 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()

2、批量读取数据

>>> 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']   #有序的列表

你可能感兴趣的:(python基础)