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.
After downloading the zip file, unzip it. After unzipping, these files are:
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:
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
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')
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')
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()
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!