python operation redis

Recently, while the project is not too busy, I have studied redis, from java operation redis to python operation redis, and python operation redis is generally similar to java operation, but let’s record it for a while (good memory is not as bad as bad writing)

First of all, of course, is to install redis, python has python to operate redis library: redis-py, this step will not go into details, the redis installation package and redis-py under windows are available on github, if pycharm is installed such as Tool, it takes a lot of trouble to install redis-py, the tool will prompt to install

The second step is to start redis. I installed redis under windows, go directly to the installation directory, and execute

redis-server.exe redis.windows.conf

When the icon appears, it means that the start of redis is successful, and the port: 6379 means that the port of redis is 6379

The third step, of course, is to write a program to operate redis. I installed pycharm on my machine, and it is very convenient to operate.

# -*- coding:utf-8 -*-
# fileName:FirstRedis.py
# The first instance of operation redis
import redis
# Local redis 127.0.0.1 port 6379
pool = redis.ConnectionPool(host="127.0.0.1",port=6379)
r = redis.Redis(connection_pool=pool)
r.set("name","顾旭华")
r.set("age",29)
print r.get("name")
print r.get("age")
The first line: 
# -*- coding:utf-8 -*-

Yes, otherwise, if there is Chinese in the python code, there will be an error:

SyntaxError: Non-ASCII character
For example, compared with the previous code, the first line of setting code is missing:
# fileName:FirstRedis.py
# The first instance of operation redis
import redis
# Local redis 127.0.0.1 port 6379
pool = redis.ConnectionPool(host="127.0.0.1",port=6379)
r = redis.Redis(connection_pool=pool)
r.set("name","顾旭华")
r.set("age",29)
print r.get("name")
print r.get("age")
But when executed, it throws an error:
C:\Python27\python.exe F:/python/workspace/com/cn/day008/SecondRedis.py
  File "F:/python/workspace/com/cn/day008/SecondRedis.py", line 2
SyntaxError: Non-ASCII character '\xe7' in file F:/python/workspace/com/cn/day008/SecondRedis.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Process finished with exit code 1

Well, now back to the python operation redis part:

first row:

import redis

The redis library will be imported. If redis.py is not installed, pycharm will prompt to install it.

pool = redis.ConnectionPool(host="127.0.0.1",port=6379)

This piece of code uses the redis connection pool to manage connections. 127.0.0.1 is the local machine, which is equivalent to: localhost, port, as mentioned above, is the port part after redis is successfully started.

The other parts are easier to understand and will not be elaborated. The results are as follows:


The corresponding redis will also have a corresponding prompt to inform that the deposit is successful:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324850903&siteId=291194637