Redis python client redis-py installation use documentation _redis

Source: Internet
Author: User
Tags documentation redis install redis redis server in python

1. Installation

Redis-py is the Python interface to the Redis Key-value database, installed as follows, and we'll talk about Hiredis this library

Copy Code code as follows:

$ sudo pip install Redis
$ sudo pip install Hiredis

2. Getting Started

Copy Code code as follows:

>>> Import Redis

>>> pool = Redis. ConnectionPool (host= ' localhost ', port=6379, db=0)

>>> r = Redis. Strictredis (Connection_pool = Pool)

>>> r.set (' foo ', ' Bar ')

True

>>> r.get (' foo ')

' Bar '

3.API Reference

Redis The Official document explains each command (http://redis.io/commands) in detail. REDIS-PY provides two client classes that implement these commands. The Strictredis class tries to comply with the official command syntax, but there are a few exceptions:

· SELECT: Not implemented. See the "Thread Safety" section below for an explanation.

· Del: ' del ' is a reserved keyword in the Python syntax. Therefore redis-py uses "delete" instead.

· CONFIG get| SET: Implemented separately with Config_get and Config_set.

· Multi/exec: Implemented as part of the Pipeline class. If you specify use_transaction=true when the pipeline method is invoked, the pipeline operation is encapsulated with MULTI and EXEC when the pipeline is executed. See the Pipeline section below.

· Subscribe/listen: Similar to pipeline, PubSub is implemented as a separate class because of the need for lower-level connections to remain state. The PubSub method that invokes the Redis client returns an instance of a pubsub that can subscribe to the channel or listen for messages. Two classes (Strictredis and PubSub classes) can publish (PUBLISH) messages.

In addition to the changes above, the Strictredis subclass Redis provides compatibility for older versions of Redis-py:

· Lrem: The order of the parameter ' num ' and ' value ' is exchanged so that ' num ' can provide a default value of 0.

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

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

Note: It is best not to use Redis, this class is just for compatibility

4. Detailed description

4.1 Connection Pool

In the background, Redis-py uses a connection pool (ConnectionPool) to manage the connection to the Redis server. By default, each Redis instance creates its own connection pool. You can also use the Connection_pool parameter of the Redis class to pass the created connection pool. In this way, you can achieve client fragmentation or precise control of the connection management:

Copy Code code as follows:

>>> pool = Redis. ConnectionPool (host= ' localhost ', port=6379, db=0)

>>> r = Redis. Strictredis (Connection_pool=pool)


4.2 Connections

ConnectionPool manages a set of Connection instances. Redis-py offers two kinds of Connection. By default, Connection is a normal TCP connection. Unixdomainsocketconnection allows clients running on the same device as the server to connect through UNIX sockets. To use a unixdomainsocketconnection connection, you only need to pass a string of UNIX socket files through the Unix_socket_path parameter. Also, make sure that the redis.conf file is configured with the Unixsocket parameter (which is commented out by default):

Copy Code code as follows:

>>> r = Redis. Strictredis (unix_socket_path= '/tmp/redis.sock ')

You can also create a Connection subclass yourself. This feature can be used to control the behavior of the socket when using an asynchronous frame. To initialize the client class with your own connection, you need to create a connection pool and pass the Connection_class parameter to the class. Other keyword parameters that are passed are passed to the custom class at initialization time:
Copy Code code as follows:

>>> pool = Redis. ConnectionPool (Connection_class=yourconnectionclass, your_arg= ' ... ', ...)

4.3 Analyzer

The analysis class provides a way to control how the response to a Redis server is analyzed. Redis-py provides two analytic classes, Pythonparser and Hiredisparser. By default, if the Hiredis module is installed, Redis-py will try to use Hiredisparser or Pythonparser.

Hiredis is a C library maintained by the Redis core team. Pieter Noordhuis created the implementation of Python. When analyzing the response of a Redis server, Hiredis can provide 10 times times the speed of ascension. Performance improvements are obvious when you get a lot of data, such as Lrange and smembers operations.

Like Redis-py, Hiredis is available in Pypi and can be installed via PIP or Easy_install:

Copy Code code as follows:

$ pip Install Hiredis

Or:
Copy Code code as follows:

$ Easy_install Hiredis

4.4 Response callback function

The client class uses a series of callback functions to convert the Redis response to the appropriate Python type. Some callback functions are defined in the dictionary response_callbacks of the Redis client class.

The Set_response_callback method allows you to add a custom callback function to a single instance. This method takes two parameters: a command name and a callback function. The callback function added by this method is valid only for the object that is added to. To globally define or overload a callback function, you should create a subclass of the Redis client and add the callback function to the response_callbacks of the class (in the original error redis_callbacks).

The response callback function has at least one parameter: The response of the Redis server. You can also use keyword parameters to further control how the response is interpreted. These keyword parameters are specified when a command call to Execute_command is invoked. With the "withscores" argument, Zrange demonstrates how the callback function uses keyword parameters.

4.5 Thread Safety

Redis client instances can be securely shared across threads. From an internal implementation, the connection instance is obtained only when the command is executed, and returns directly to the connection pool upon completion, and the command never modifies the state of the client instance.

However, one thing to note: the SELECT command. The SELECT command allows you to toggle the database used by the current connection. The new database remains selected until another database is selected or the connection is closed. This can cause the connection to specify a different database when the connection pool is returned.

Therefore, Redis-py does not implement the SELECT command in the client instance. If you want to use multiple Redis databases in the same application, you should create separate client instances (and possibly separate connection pools) for the first database.

It is not safe to pass PubSub and Pipeline objects between threads.

4.6 Pipeline

Pipeline is a subclass of the Strictredis class that supports multiple commands that are buffered in a single request. By reducing the number of packets between the client and the server, the performance of the command group can be greatly improved.

The use of Pipeline is very simple:

Copy Code code as follows:

>>> r = Redis. Redis (...)

>>> r.set (' Bing ', ' Baz ')

>>> use the pipeline () method to create a pipeline instance

>>> pipe = R.pipeline ()

>>> # The following SET commands are buffered

>>> pipe.set (' foo ', ' Bar ')

>>> pipe.get (' Bing ')

>>> # The EXECUTE call sends all bufferred commands to the server, returning

>>> # A list of responses, one for each command.

>>> Pipe.execute ()

[True, ' Baz ']

For ease of use, all commands buffered to pipeline return the pipeline object itself. So the call can be chained up:

Copy Code code as follows:

>>> pipe.set (' foo ', ' Bar '). Sadd (' Faz ', ' Baz '). INCR (' Auto_number '). Execute ()

[True, True, 6]


In addition, pipeline can also guarantee that the buffered command group is an atomic operation. The default is this pattern. To use command buffering, but prohibit pipeline atomic manipulation properties, you can turn off the transaction:

>>> pipe = r.pipeline (Transaction=false)
A common problem is that you need to get the data in the transaction from the Redis before you do atomic transaction operations. For example, suppose the INCR command doesn't exist, but we need to create an atomic version of INCR with Python.

An immature implementation is to fetch the value (GET), add one in Python, and set the new value (set). However, this is not an atomic operation because multiple clients may do it at the same time, each getting the same value through get.

The WATCH command provides the ability to monitor one or more keys before starting a transaction. If any of these keys changes before the transaction is executed, the entire transaction is canceled and the Watcherror exception is thrown. To implement our Customer INCR command, you can do this by doing the following:

Copy Code code as follows:

>>> with R.pipeline () as pipe:

. While 1:

... try:

... # The key to the serial number is WATCH

... pipe.watch (' Our-sequence-key ')

. # WATCH execution, pipeline is set to execute immediately until we notify it

... # re-start the buffer command.

... # This allows us to get the value of the serial number.

... current_value = pipe.get (' Our-sequence-key ')

... next_value = Unicode (int (current_value) + 1)

... # Now we can use the MULTI command to set the pipeline to buffer mode.

... Pipe.multi ()

... pipe.set (' Our-sequence-key ', next_value)

... # Finally, execute pipeline (SET command)

... Pipe.execute ()

... # if executed without throwing watcherror, what we have just done is really "atomic ground"

... # it's done.

.. break

... except Watcherror:

... # It must be the other client. We modified the pipeline between the beginning of WATCH and the execution of the

... # ' Our-sequence-key ', our best option is to retry

... continue


Note that because Pipeline must be bound to a connection throughout the WATCH process, the Reset () method must be called to ensure that the connection returns the connection pool. If Pipeline is used as the context Manager (as shown in the example above), reset () is invoked automatically. Of course, you can also explicitly call reset () in a manual way:
Copy Code code as follows:

>>> pipe = R.pipeline ()

>>> while 1:

... try:

... pipe.watch (' Our-sequence-key ')

... current_value = pipe.get (' Our-sequence-key ')

... next_value = Unicode (int (current_value) + 1)

... Pipe.multi ()

... pipe.set (' Our-sequence-key ', next_value)

... Pipe.execute ()

.. break

... except Watcherror:

... continue

... finally:

... pipe.reset ()

Focus (Translator Note):

· After WATCH executes, the pipeline is set to execute immediately.

• Set pipeline to buffer mode with MULTI command

• Either use with or explicitly call reset ()

There is a simple method called "transaction" to handle this kind of processing and the mode of Watcherror retry. Its argument is an executable object and a key to WATCH any number, where the executable object accepts a pipeline object as a parameter. The client INCR command above can be rewritten as follows (more readable):

Copy Code code as follows:

>>> def CLIENT_SIDE_INCR (pipe):

... current_value = pipe.get (' Our-sequence-key ')

... next_value = Unicode (int (current_value) + 1)

... Pipe.multi ()

... pipe.set (' Our-sequence-key ', next_value)

>>>

>>> r.transaction (client_side_incr, ' Our-sequence-key ')

4.7 Version Plan

Redis-py follows Redis release version. such as Redis-py 2.0.0 should support all commands of Redis 2.0.0.

4.8 Author

Redis-py is developed and maintained by Andy McCurdy (sedrik@gmail.com). Project address in: Http://github.com/andymccurdy/redis-py

Special acknowledgements:

· Ludovico Magnocavallo, the original author of the Python Redis client, some of the socket codes are still in use.

· Alexander Solovyov provides the idea of a universal response callback system.

· Paul Hubbard for initial packaging support.

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.