“This is the 20th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

preface

Before we are using Redis client to use Redis, but in the actual work, we are mostly through the code to use Redis, because xiaobian is more familiar with Python, so we will learn how to use Python to operate Redis today.

Environment to prepare

  • RedisYou need to install it first.
  • PythonInstall well (recommendedPython3).
  • RedisPythonLibrary installed (pip install redis).

Began to practice

A profound

Example: We plan to connect to Redis via Python. Then write a KV, and finally print out the queried v.

A direct connection

#! /usr/bin/python3

import redis   # import redis module

r = redis.Redis(host='localhost', port=6379, password="pwd@321", decode_responses=True)   The default redis port number is 6379
r.set('name'.'phyger-from-python-redis')  # key = "name" value = "phyger-from-python-redis
print(r['name'])  Select * from key (name)
print(r.get('name'))  Select * from key (name)
print(type(r.get('name')))
Copy the code

Get is the last command executed by the connection pool.

The connection pool

Normally, when you need to connect to redis, you create a connection, perform redIS operation based on the connection, and release the redIS operation after completion. Normally, this is fine, but with high concurrency, frequent connection creation and release can have a high impact on performance, and connection pooling comes into play.

The principle of connection pooling is as follows: Multiple connections are created in advance. When redis is performed, the created connections are directly obtained for operation. Upon completion, the connection is not released, but returned to the connection pool for subsequent Redis operations! This improves performance by avoiding continuous create and release!

#! /usr/bin/python3

import redis,time   Redis can also be used directly on the server side of the redis host

pool = redis.ConnectionPool(host='localhost', port=6379, password="pwd@321", decode_responses=True)   The default port is 6379
r = redis.Redis(connection_pool=pool)
r.set('name'.'phyger-from-python-redis')
print(r['name'])
print(r.get('name'))  Select name from key
print(type(r.get('name')))
Copy the code

You will find that direct connection works just as well as connection pooling in practice, except that there are significant differences in high concurrency.

Base operating practice

The SET command is an example of many Redis commands.

Set (name, value, ex=None, px=None, nx=False, xx=False)

The arguments to the set command in redis-py:

Parameter names paraphrase
ex <int>Expiration Time (m)
px <int>Expiration Time (ms)
nx <bool>If true, the current set operation is executed only if name does not exist
xx <bool>If true, the current set operation is executed only if name exists

ex

We plan to create a KV and set its ex to 3, expecting the v of this k to be None after 3 seconds.

#! /usr/bin/python3

import redis,time   Redis can also be used directly on the server side of the redis host

pool = redis.ConnectionPool(host='localhost', port=6379, password="pwd@321", decode_responses=True)   The default port is 6379
r = redis.Redis(connection_pool=pool)
r.set('name'.'phyger-from-python-redis',ex=3)
print(r['name'])    # should have v
time.sleep(3)
print(r.get('name'))  # should have no v
print(type(r.get('name')))
Copy the code

nx

Since the unit of PX is too short, we will not do the demonstration, and the effect is the same as ex.

We plan to repeat the name that was set before set. If not, we will fail to set if nx is true. However, if the set name1 does not exist, it will succeed.

#! /usr/bin/python3

import redis,time   Redis can also be used directly on the server side of the redis host

pool = redis.ConnectionPool(host='localhost', port=6379, password="pwd@321", decode_responses=True)   The default port is 6379
r = redis.Redis(connection_pool=pool)
r.set('name'.'phyger-0',nx=3) # set failure
print(r['name'])    # should not be valid
r.set('name1'.'phyger-1',nx=3) # set success
print(r.get('name1'))  # shall take effect
print(type(r.get('name')))
Copy the code

As shown above, you will find that the set for name is not in effect because name already exists in the database. The set for name1 is in effect, because name1 does not exist in the database.

xx

We plan to repeat the name that was set before set. If not, we will set successfully if nx is true. However, if the set name2 does not exist, it will fail.

#! /usr/bin/python3 import redis,time ConnectionPool = redis.ConnectionPool(host='localhost', port=6379, password="pwd@321", Decode_responses =True) # host R = redis.Redis(connection_pool=pool) r.set('name', Print (r['name']) print(r['name']) Print (type(r.et ('name')) print(r.et ('name'))Copy the code

That’s all for today. For more information, please refer to the official redis document.