Common Operations and pipelines, commonly used operation Pipelines

Source: Internet
Author: User

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

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.