Introduction to Redis and common commands

Introduction to Redis

What is Redis

Redis is a key-value storage system. Similar to Memcached, it supports relatively more stored value types, including string, list, set, zset (sorted set) and hash. These data types support push/pop, add/remove, intersection, union, difference and other richer operations, and these operations are atomic. On this basis, redis supports sorting in various ways. Like memcached, data is cached in memory to ensure efficiency. The difference is that redis will periodically write updated data to disk or write modification operations to additional record files, and on this basis, master-slave (master-slave) synchronization is realized.

Redis download and installation

Linux

$ wget http://download.redis.io/releases/redis-5.0.3.tar.gz
$ tar xzf redis-5.0.3.tar.gz
$ cd redis-5.0.3
$ make

At this point, the executable file is in the src directory, run the following command to start redis

$ src/redis-server

Use the built-in client tools to interact with redis:

$ src/redis-cli
redis> set name qimi
OK
redis> get name
"q1mi"

Windows

 Download the windows version --> I am the download link, please click me

 

 

 

Close the above cmd window to close the redis service.

redis as a windows service startup method
redis-server --service-install redis.windows.conf
start service: redis-server --service-start
stop service: redis-server --service-stop

Start the built-in client to connect to the redis service:

Python operation Redis

Install the tool for python to connect to redis

pip install redis

Basic use of redis module

connection method

redis-py provides two classes Redis and StrictRedis to implement Redis commands. StrictRedis is used to implement most official commands and use official syntax and commands. Redis is a subclass of StrictRedis for backward compatibility with older versions redis-py.

import redis 

r = redis.Redis()   #Default local connection 
# r = redis.Redis(host='11.12.13.14', port=6379) #Configurable IP and port to connect 
r.set( ' name ' , ' q1mi ' )
 print (r. get( ' name ' ))

connection pool

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 a parameter Redis, so that multiple Redis instances can share a connection pool.

copy code
import redis

pool = redis.ConnectionPool()

r = redis.Redis(connection_pool=pool)
r.set('age', 18)
print(r.get('age'))
copy code

operate

Redis key

The redis keys command is used to manage redis keys.

serial number command and description
1 DEL key
This command is used to delete the key when the key exists.
2 DUMP key
serializes the given key and returns the serialized value.
3 EXISTS key
checks if the given key exists.
4 EXPIRE key seconds
sets the expiration time for the given key, in seconds.
5 EXPIREAT key timestamp
The function of EXPIREAT is similar to that of EXPIRE, both are used to set the expiration time for the key. The difference is that the time parameter accepted by the EXPIREAT command is a UNIX timestamp (unix timestamp).
6 PEXPIRE key milliseconds
sets the expiration time of the key in milliseconds.
7 PEXPIREAT key milliseconds-timestamp
Set the timestamp of the key expiration time (unix timestamp) in milliseconds
8 KEYS pattern
finds all keys matching the given pattern.
9 MOVE key db
moves the key of the current database to the given database db.
10 PERSIST key
removes the expiration time of the key, and the key will be kept persistently.
11 PTTL key
returns the key's remaining expiration time in milliseconds.
12 TTL key
returns the remaining time to live (TTL, time to live) of the given key in seconds.
13 RANDOMKEY
returns a key randomly from the current database.
14 RENAME key newkey
Modify the name of the key
15 RENAMENX key newkey
Rename key to newkey only if newkey does not exist.
16 TYPE key
returns the type of value stored by key.

 

Redis String operation

String operation, the String in redis is stored in memory according to a name corresponding to a value.

 

serial number command and description
1 SET key value
sets the value of the specified key
2 GET key
Gets the value of the specified key.
3 GETRANGE key start end
returns the subcharacters of the string value in key
4 GETSET key value
将给定 key 的值设为 value ,并返回 key 的旧值(old value)。
5 GETBIT key offset
对 key 所储存的字符串值,获取指定偏移量上的位(bit)。
6 MGET key1 [key2..]
获取所有(一个或多个)给定 key 的值。
7 SETBIT key offset value
对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)。
8 SETEX key seconds value
将值 value 关联到 key ,并将 key 的过期时间设为 seconds (以秒为单位)。
9 SETNX key value
只有在 key 不存在时设置 key 的值。
10 SETRANGE key offset value
用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始。
11 STRLEN key
返回 key 所储存的字符串值的长度。
12 MSET key value [key value ...]
同时设置一个或多个 key-value 对。
13 MSETNX key value [key value ...]
同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在。
14 PSETEX key milliseconds value
这个命令和 SETEX 命令相似,但它以毫秒为单位设置 key 的生存时间,而不是像 SETEX 命令那样,以秒为单位。
15 INCR key
将 key 中储存的数字值增一。
16 INCRBY key increment
将 key 所储存的值加上给定的增量值(increment) 。
17 INCRBYFLOAT key increment
将 key 所储存的值加上给定的浮点增量值(increment) 。
18 DECR key
将 key 中储存的数字值减一。
19 DECRBY key decrement
key 所储存的值减去给定的减量值(decrement) 。
20 APPEND key value
如果 key 已经存在并且是一个字符串, APPEND 命令将指定的 value 追加到该 key 原来值(value)的末尾。

Redis Hash操作

Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象。

Redis 中每个 hash 可以存储 232-1 键值对(40多亿)。

 

序号 命令及描述
1 HDEL key field1 [field2]
删除一个或多个哈希表字段
2 HEXISTS key field
查看哈希表 key 中,指定的字段是否存在。
3 HGET key field
获取存储在哈希表中指定字段的值。
4 HGETALL key
获取在哈希表中指定 key 的所有字段和值
5 HINCRBY key field increment
为哈希表 key 中的指定字段的整数值加上增量 increment 。
6 HINCRBYFLOAT key field increment
为哈希表 key 中的指定字段的浮点数值加上增量 increment 。
7 HKEYS key
获取所有哈希表中的字段
8 HLEN key
获取哈希表中字段的数量
9 HMGET key field1 [field2]
获取所有给定字段的值
10 HMSET key field1 value1 [field2 value2 ]
同时将多个 field-value (域-值)对设置到哈希表 key 中。
11 HSET key field value
将哈希表 key 中的字段 field 的值设为 value 。
12 HSETNX key field value
只有在字段 field 不存在时,设置哈希表字段的值。
13 HVALS key
获取哈希表中所有值
14 HSCAN key cursor [MATCH pattern] [COUNT count]
迭代哈希表中的键值对。

Redis List操作

Redis List是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到List的头部(左边)或者尾部(右边)

一个List最多可以包含 232-1 个元素 (4294967295, 每个List超过40亿个元素)。

 

序号 命令及描述
1 BLPOP key1 [key2 ] timeout
移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
2 BRPOP key1 [key2 ] timeout
移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
3 BRPOPLPUSH source destination timeout
从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
4 LINDEX key index
通过索引获取列表中的元素
5 LINSERT key BEFORE|AFTER pivot value
在列表的元素前或者后插入元素
6 LLEN key
获取列表长度
7 LPOP key
移出并获取列表的第一个元素
8 LPUSH key value1 [value2]
将一个或多个值插入到列表头部
9 LPUSHX key value
将一个值插入到已存在的列表头部
10 LRANGE key start stop
获取列表指定范围内的元素
11 LREM key count value
移除列表元素
12 LSET key index value
通过索引设置列表元素的值
13 LTRIM key start stop
对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。
14 RPOP key
移除列表的最后一个元素,返回值为移除的元素。
15 RPOPLPUSH source destination
移除列表的最后一个元素,并将该元素添加到另一个列表并返回
16 RPUSH key value1 [value2]
在列表中添加一个或多个值
17 RPUSHX key value
为已存在的列表添加值

Redis Set操作

Redis 的 Set 是 String 类型的无序集合。Set成员是唯一的,这就意味着Set中不能出现重复的数据。

Redis 中Set是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。

Set 中最大的成员数为 232-1 (4294967295, 每个set可存储40多亿个成员)。

 

序号 命令及描述
1 SADD key member1 [member2]
向集合添加一个或多个成员
2 SCARD key
获取集合的成员数
3 SDIFF key1 [key2]
返回给定所有集合的差集
4 SDIFFSTORE destination key1 [key2]
返回给定所有集合的差集并存储在 destination 中
5 SINTER key1 [key2]
返回给定所有集合的交集
6 SINTERSTORE destination key1 [key2]
返回给定所有集合的交集并存储在 destination 中
7 SISMEMBER key member
判断 member 元素是否是集合 key 的成员
8 SMEMBERS key
返回集合中的所有成员
9 SMOVE source destination member
将 member 元素从 source 集合移动到 destination 集合
10 SPOP key
移除并返回集合中的一个随机元素
11 SRANDMEMBER key [count]
返回集合中一个或多个随机数
12 SREM key member1 [member2]
移除集合中一个或多个成员
13 SUNION key1 [key2]
返回所有给定集合的并集
14 SUNIONSTORE destination key1 [key2]
所有给定集合的并集存储在 destination 集合中
15 SSCAN key cursor [MATCH pattern] [COUNT count]
迭代集合中的元素

Redis Sorted Set操作

Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员。

不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。

有序集合的成员是唯一的,但分数(score)却可以重复。

集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。 集合中最大的成员数为 232-1 (4294967295, 每个集合可存储40多亿个成员)。

 

序号 命令及描述
1 ZADD key score1 member1 [score2 member2]
向有序集合添加一个或多个成员,或者更新已存在成员的分数
2 ZCARD key
获取有序集合的成员数
3 ZCOUNT key min max
计算在有序集合中指定区间分数的成员数
4 ZINCRBY key increment member
有序集合中对指定成员的分数加上增量 increment
5 ZINTERSTORE destination numkeys key [key ...]
计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中
6 ZLEXCOUNT key min max
在有序集合中计算指定字典区间内成员数量
7 ZRANGE key start stop [WITHSCORES]
通过索引区间返回有序集合成指定区间内的成员
8 ZRANGEBYLEX key min max [LIMIT offset count]
通过字典区间返回有序集合的成员
9 ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]
通过分数返回有序集合指定区间内的成员
10 ZRANK key member
返回有序集合中指定成员的索引
11 ZREM key member [member ...]
移除有序集合中的一个或多个成员
12 ZREMRANGEBYLEX key min max
移除有序集合中给定的字典区间的所有成员
13 ZREMRANGEBYRANK key start stop
移除有序集合中给定的排名区间的所有成员
14 ZREMRANGEBYSCORE key min max
移除有序集合中给定的分数区间的所有成员
15 ZREVRANGE key start stop [WITHSCORES]
返回有序集中指定区间内的成员,通过索引,分数从高到底
16 ZREVRANGEBYSCORE key max min [WITHSCORES]
返回有序集中指定分数区间内的成员,分数从高到低排序
17 ZREVRANK key member
返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序
18 ZSCORE key member
返回有序集中,成员的分数值
19 ZUNIONSTORE destination numkeys key [key ...]
计算给定的一个或多个有序集的并集,并存储在新的 key 中
20 ZSCAN key cursor [MATCH pattern] [COUNT count]
Iterates the elements in the ordered set (including element members and element scores)

pipeline

By default, redis-py will create (connection pool application connection) and disconnection (return connection pool) connection operation for each request. If you want to specify multiple commands in one request, you can use pipline to implement one request to specify multiple commands. commands, and by default a pipeline is an atomic operation.

copy code
import redis

pool = redis.ConnectionPool()

r = redis.Redis(connection_pool=pool)

# pipe = r.pipeline(transaction=False)
pipe = r.pipeline(transaction=True)
pipe.multi()
pipe.set('name', 'Q1mi')
pipe.set('role', 'JPG')

pipe.execute()
copy code

 

Guess you like

Origin blog.csdn.net/bruce_van/article/details/103152793