Common Operations and pipelines, commonly used operation Pipelines
I. Common Operations
1.1 delete (* names)
# Print (r. get ('name') r. delete ('name') print (r. get ('name') # output B 'bigberg 'None
1.2 exists (name)
# Check whether the redis name has print (r. exists ('name') print (r. exists ('names') # output FalseTrue
1.3 keys (pattern = '*')
# Obtain the redis name based on the model # More: # KEYS * matches all KEYS in the database. # KEYS h? Llo matches hello, hallo, and hxllo. # KEYS h * llo matches hllo and heeeeello. # KEYS h [AE] llo matches hello and hallo, but does not match hilloprint (r. keys (pattern = 'n' * ') # output [B 'names', B 'num', B 'names _ dst']
1.4 expire (name, time)
# Set the timeout time print (r. get ('num') r. expire ('num', 2) time. sleep (3) print (r. get ('num') # output B '6' None
1.5 rename (src, dst)
# Rename redis name to print (r. get ('info') r. rename ('info', 'info-test') print (r. get ('info-test') # output B 'this is my test' B 'this is my Test'
1.6 move (name, db ))
# Move a value of redis to the r. move ('info-test', 3) 127.0.0.1: 6379> select 3OK127. 0.0.1: 6379 [3]> keys * 1) "info-test"
1.7 randomkey ()
# Randomly retrieve the name (not deleted) print (r. randomkey () of a redis instance # output 'login _ user'
1.8 type (name)
# Print (r. type ('login _ user') of the type for obtaining the value of name # output 'string'
Ii. Pipelines
By default, redis-py creates (the connection pool applies for connection) and disconnects (returns the connection pool) each time a request is executed. If you want to specify multiple commands in a request, you can use pipline to specify multiple commands for one request. By default, pipline is an atomic operation.
#!/usr/bin/env python# -*- coding:utf-8 -*- import redis pool = redis.ConnectionPool(host='10.211.55.4', port=6379) r = redis.Redis(connection_pool=pool) # pipe = r.pipeline(transaction=False)pipe = r.pipeline(transaction=True) pipe.set('name', 'ddt')pipe.set('role', 'superboy') pipe.execute()