Preliminary use of the Redis python client redis-py

Source: Internet
Author: User
Tags connection pooling

1. Installation
sudo Install Redis sudo Install Hiredis

parser can control how the contents of a Redis response are parsed. Redis-py contains two parser classes, Pythonparser and Hiredisparser. By default, if the Hiredis module is already installed, Redis-py will use Hiredisparser, otherwise pythonparser will be used. Hiredisparser is written in C, and is maintained by the Redis core team, with performance up to 10 times times higher than pythonparser, so it is recommended.

2. Use

Redis-py provides two classes of Redis and Strictredis for implementing Redis commands, Strictredis is used to implement most of the official commands, and uses the official syntax and commands (for example, the SET command corresponds to the Strictredis.set method). Redis is a subclass of Strictredis for backwards compatibility with older versions of Redis-py. Simply put, the official recommendation is to use the Strictredis method.

Redis class is not recommended because he and we in the redis-cli operation somewhat different, the main difference is the following three aspects.

·  Lrem: The order of the parameters ' num ' and ' value ' is exchanged for a bit, the CLI is lrem queuename 0 ' string '. Here's 0 o'clock all the meaning. But the Redis class, the control and the string swapped.

· Zadd: The order of score and value was not carefully reversed when implemented, then someone used it, that's all.

· The order of Setex:time and value is reversed.

. Pool: Connection Pooling

ImportRedis R= Redis. Strictredis (host='127.0.0.1', port=9212) R.set ('Foo','Hello') R.rpush ('MyList',' One')  PrintR.get ('Foo')  PrintR.rpop ('MyList')

If you do not specify a connection pool connection pool when you create an Strictredis instance, it is automatically created. We can build the good one connection pool first, specify the connection pool when the Strictredis is created, so that multiple Strictredis instances can share a connection pool. This saves resource consumption such as memory.

ImportRedis Pool= Redis. ConnectionPool (host='127.0.0.1', port=9212) r_1= Redis. Strictredis (Connection_pool =pool)
r_2 = Redis.Strictredis(Connection_pool = Pool)
R_1.set (' One',' First')
R_2.set (' Both','Second')
PrintR_1.get (' One')
PrintR_2.get (' Both')

The Redis pipeline mechanism, which corresponds to a transaction in Redis, can execute multiple commands in a single request, thus avoiding multiple round-trip delays.

Reference:

Pits that are encountered when using redis-py two classes of Redis and Strictredis

Redis's Python client redis-py

Preliminary use of the Redis python client redis-py

Related Article

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.