Home >Database >Redis >How to use Python to operate Redis under Windows

How to use Python to operate Redis under Windows

PHPz
PHPzforward
2023-05-29 10:21:261848browse

First of all, let’s talk about installing redis under windows. The installation package can be found on the official website. You can download the msi installation file or the zip compressed file.

How to use Python to operate Redis under Windows

After downloading the zip file, unzip it. After unzipping, these files are:

How to use Python to operate Redis under Windows

The windows service documentation.docx inside is A document with installation instructions and usage instructions.

You can also directly download the msi installation file and install it directly. These files are also in the installation directory after installation, and you can configure redis accordingly.

After the installation is complete, you can test redis. Double-click redis-cli.exe. If no error is reported, you should be connected to the local redis for a simple test:

How to use Python to operate Redis under Windows

The default installation is port 6379, and the test was successful.

You can also enter help to view the help:

127.0.0.1:6379> help 
redis-cli 3.2.100 
to 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 of possible help topics 
   "quit" to exit 
to set redis-cli perferences: 
   ":set hints" enable online hints 
   ":set nohints" disable online hints 
set your preferences in ~/.redisclirc

Let’s talk about using python to operate redis. If you use python to install redis, you need to install the redis-py library

1. Install redis-py

easy_install redis You can also use pip install redis to install, or download and execute python setup.py install to install

2. Install parser installation

parser can control how to parse the content of redis response. redis-py contains two parser classes, pythonparser and hiredisparser. By default, redis-py will use hiredisparser if the hiredis module is installed, otherwise pythonparser will be used. hiredisparser is written in C and maintained by the redis core team. Its performance is more than 10 times higher than that of pythonparser, so it is recommended to use it. Installation method, use easy_install:

easy_install hiredis or pip install hiredis

3. Use python to operate redis

redis- py provides two classes, redis and strictredis, for implementing redis commands. strictredis is used to implement most official commands and uses official syntax and commands (for example, the set command corresponds to the strictredis.set method). redis is a subclass of strictredis for backward compatibility with older versions of redis-py.

import redis 
r = redis.strictredis(host=&#39;127.0.0.1&#39;, port=6379) 
r.set(&#39;foo&#39;, &#39;hello&#39;) 
r.rpush(&#39;mylist&#39;, &#39;one&#39;) 
print r.get(&#39;foo&#39;) 
print r.rpop(&#39;mylist&#39;)

redis-py uses connection pool to manage all connections to a redis server, avoiding the overhead of establishing and releasing connections each time. By default, each redis instance maintains its own connection pool. You can directly create a connection pool and then use it as parameter redis, so that multiple redis instances can share a connection pool.

pool = redis.connectionpool(host=&#39;127.0.0.1&#39;, port=6379) 
r = redis.redis(connection_pool=pool) 
r.set(&#39;one&#39;, &#39;first&#39;) 
r.set(&#39;two&#39;, &#39;second&#39;) 
print r.get(&#39;one&#39;) 
print r.get(&#39;two&#39;)

The redis pipeline mechanism can execute multiple commands in one request, thus avoiding multiple round-trip delays.

pool = redis.connectionpool(host=&#39;127.0.0.1&#39;, port=6379)  
r = redis.redis(connection_pool=pool)  
pipe = r.pipeline()  
pipe.set(&#39;one&#39;, &#39;first&#39;)  
pipe.set(&#39;two&#39;, &#39;second&#39;)  
pipe.execute()  
pipe.set(&#39;one&#39;. &#39;first&#39;).rpush(&#39;list&#39;, &#39;hello&#39;).rpush(&#39;list&#39;, &#39;world&#39;).execute()

redis-py defaults to atomic operations in a pipeline. To change this method, you can pass in transaction=false

pipe = r.pipeline(transaction=false)

The above is the detailed content of How to use Python to operate Redis under Windows. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete