Python access to Redis

Source: Internet
Author: User
Tags install redis redis server

First of all, to install Redis under Windows, the installation package can be found in https://github.com/MSOpenTech/redis/releases, you can download the MSI installation file, or you can download the zip archive.

After downloading the zip file, unzip the files after extracting them:

Inside this Windows Service Documentation.docx is a document that has instructions for installation and how to use it.

You can also download the MSI installation files directly, install them directly, and the files in the installation directory after installation, which can be configured for Redis.

After the installation is complete, you can test Redis, double-click the Redis-cli.exe, and if you do not make an error, you should connect to local Redis for a simple test:

The default installation is Port 6379, and the test is successful.

You can also enter Help to view assistance:



  1. 127.0.0.1:6379> Help

  2. REDIS-CLI 3.2.100

  3. To get help about Redis commands type:

  4. ' Help @<Group>' to get a list of commands in <Group>

  5. "Help <command>" for Help on <command>

  6. "Help <tab>" to get a list of possible Help topics

  7. "Quit" to exit

  8. To set REDIS-CLI perferences:

  9. ": Set hints" Enable online hints

  10. ": Set nohints" Disable online hints

  11. Set your preferences in ~/.REDISCLIRC

127.0.0.1:6379> helpredis-cli 3.2.100To Get help about Redis commands type: ' Help @<group> ' to get a list of Commands in <group> ' help <command> ' for ' help ' on <command> ' help <tab> ' to get a list o F Possible Help topics "quit" to Exitto set REDIS-CLI perferences: ": Set hints" Enable online hints ": Set N Ohints "Disable online Hintsset your preferences in ~/.REDISCLIRC




Here's how to use Python for Redis, install Redis using python and install Redis-py Library

1, installation Redis-py

Easy_install Redis can also be installed using PIP install, or https://github.com/andymccurdy/redis-py download and execute Python setup.py Install installation

2, install parser installation

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. Installation method, using Easy_install:

Easy_install Hiredis or pip install Hiredis

3. Using Python to operate Redis

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.



    1. import  redis  

    2.   

    3. R = redis. Strictredis (Host= ' 127.0.0.1 ' ,  port=6379 )    

    4. R.set ( ' foo ' ,   ' hello ' )    

    5. R.rpush ( ' MyList ' ,   ' one ' )   

    6. print  r.get ( ' foo ' )   

    7. print   r.rpop ( ' MyList ' )   

Import REDISR = Redis. Strictredis (host= ' 127.0.0.1 ', port=6379) r.set (' foo ', ' Hello ') r.rpush (' mylist ', ' one ') print r.get (' foo ') print R.rpop (' MyList ')



Redis-py uses connection pool to manage all connections to a Redis server, avoiding the overhead of each establishment and release of the connection. By default, each Redis instance maintains its own pool of connections. You can create a connection pool directly, and then as a parameter Redis, you can implement multiple Redis instances to share a single connection pool.


    1. Pool = redis. ConnectionPool (Host= ' 127.0.0.1 ' ,  port=6379 )    

    2. R = redis. Redis (Connection_pool=pool)   

    3. R.set ( ' one ' ,  )   

    4. R.set ( ' n ' ,   ' second ' )   

    5. print  r.get ( ' one ' )   

    6. print  r.get ( "")    

Pool = Redis. ConnectionPool (host= ' 127.0.0.1 ', port=6379) R = Redis. Redis (Connection_pool=pool) r.set (' One ', ' first ') R.set (' Both ', ' second ') Print r.get (' one ') print r.get (' one ')


The Redis pipeline mechanism allows multiple commands to be executed in a single request, thus avoiding multiple round-trip delays.



  1. Pool = Redis. ConnectionPool (host=' 127.0.0.1 ', port=6379)

  2. R = Redis. Redis (Connection_pool=pool)

  3. Pipe = R.pipeline ()

  4. Pipe.set (' One ', ' first ')

  5. Pipe.set (' both ', ' second ')

  6. Pipe.execute ()

  7. Pipe.set (' one '). ' first '). Rpush ('list ', ' Hello '). Rpush (' list ', ' World '). Execute ()

Pool = Redis. ConnectionPool (host= ' 127.0.0.1 ', port=6379) R = Redis. Redis (connection_pool=pool) pipe = R.pipeline () pipe.set (' One ', ' first ') Pipe.set (' One ', ' second ') Pipe.execute () p Ipe.set (' one '). ' first '). Rpush (' list ', ' Hello '). Rpush (' list ', ' World '). Execute ()


Redis-py by default in a single pipeline operation is atomic, to change this way, you can pass in Transaction=false

    1. Pipe = R.pipeline (transaction=False)


Python access to Redis

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.