Home> Database> Redis> body text

How to use Python to operate Redis under Windows

PHPz
Release: 2023-05-29 10:21:26
forward
1732 people have browsed it

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 @" to get a list of commands in  "help " for help on  "help " 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
Copy after login

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='127.0.0.1', port=6379) r.set('foo', 'hello') r.rpush('mylist', 'one') print r.get('foo') print r.rpop('mylist')
Copy after login

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='127.0.0.1', port=6379) r = redis.redis(connection_pool=pool) r.set('one', 'first') r.set('two', 'second') print r.get('one') print r.get('two')
Copy after login

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

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('two', 'second') pipe.execute() pipe.set('one'. 'first').rpush('list', 'hello').rpush('list', 'world').execute()
Copy after login

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

pipe = r.pipeline(transaction=false)
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!