Reference article: https://blog.csdn.net/fgf00/article/details/52917154
I. Introduction of Redis
Redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set), Zset (sorted set– ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis.
1. Redis Installation and basic use
Redis and Python operating module installation and startup
Yum Install Redis
PIP3 Install Redis
Systemctl Start Redis.service
1
2
3
View version:
REDIS-CLI Info # Redis Details
redis-cli–version | Redis-cli-v
redis-server–version | Redis-server-v
1
2
3
Simple to use:
[Root@localhost ~]# redis-cli # Enter the Redis CLI window
127.0.0.1:6379> set name FGF # setting value
Ok
127.0.0.1:6379> Set Age 02
Ok
127.0.0.1:6379> Keys * # current all key
1) "Age"
2) "Name"
127.0.0.1:6379> set Sex M Ex 2 # Sets a value that only survives 2 seconds
Ok
127.0.0.1:6379> Get sex # Get value
"M"
127.0.0.1:6379> FLUSHDB # Clears all key values under the current DB
Ok
127.0.0.1:6379> Flushall # Clears key values from all DB
Ok
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2. Python Operation Redis
1) Operating mode
Redis-py provides two classes of Redis and Strictredis for implementing Redis commands, Strictredis is used to implement most of the official commands, and using official syntax and commands, Redis is a subclass of Strictredis, Used for backwards compatibility with older versions of Redis-py.
Import Redis
R = Redis. Redis (host= ' 127.0.0.1 ', port=6379)
R.set (' foo ', ' Bar ')
Print (R.get (' foo '))
1
2
3
4
5
2) Connection Pool
Redis-py uses connection pool to manage all connections to a Redis server, avoiding the overhead of each establishment and release of the connection. By default, each Redis instance maintains its own pool of connections. You can create a connection pool directly, and then as a parameter Redis, you can implement multiple Redis instances to share a single connection pool.
Import Redis
Pool = Redis. ConnectionPool (host= ' 127.0.0.1 ', port=6379)
R = Redis. Redis (Connection_pool=pool)
R.set (' foo ', ' Bar ')
Print r.get (' foo ')