Redis: Five data types and common operations

table of Contents

1. String

2. Hash

3. List (list)

4. Redis set (Set)

5. Redis sorted set


Redis supports five data types: string (string), hash (hash), list (list), set (collection) and zset (sorted set: ordered set).

1. String

String is the most basic type of redis. You can understand it as exactly the same type as Memcached. A key corresponds to a value.

The string type is binary safe. This means that the redis string can contain any data. Such as jpg images or serialized objects .

The string type is the most basic data type of Redis, and the value of the string type can store up to 512MB .

The following table lists commonly used redis string commands:

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 subcharacter of the string value in the key
4 GETSET key value
sets the value of the given key to value and returns the old value of the key.
5 GETBIT key offset
obtains the bit at the specified offset for the string value stored in the key.
6 MGET key1 [key2..]
Get all (one or more) values ​​of a given key.
7 SETBIT key offset value
sets or clears the bit at the specified offset to the string value stored in the key.
8 SETEX key seconds value
associates the value with the key, and sets the expiration time of the key to seconds (in seconds).
9 SETNX key value
can only set the value of the key when the key does not exist.
10 SETRANGE key offset value Overwrites
the string value stored in the given key with the value parameter, starting from the offset offset.
11 STRLEN key
returns the length of the string value stored in the key.
12 MSET key value [key value ...]
Set one or more key-value pairs at the same time.
13 MSETNX key value [key value ...]
Set one or more key-value pairs at the same time, if and only if all the given keys do not exist.
14 The PSETEX key milliseconds value
command is similar to the SETEX command, but it sets the lifetime of the key in milliseconds instead of seconds as in the SETEX command.
15 INCR key
increments the numeric value stored in the key by one.
16 INCRBY key increment
adds the value stored in the key to the given increment value (increment).
17 INCRBYFLOAT key increment
adds the value stored in the key to the given floating point increment value (increment).
18 The DECR key
reduces the numeric value stored in the key by one.
19 DECRBY key decrement
key The stored value minus the given decrement value (decrement).
20 APPEND key value
If the key already exists and is a character string, the APPEND command appends the specified value to the end of the original value of the key.

2. Hash

Hash is a mapping table between field and value of string type. Hash is especially suitable for storing objects.

Each hash in Redis can store 2^{32} -1 key-value pair (more than 4 billion).

The following table lists the basic redis hash related commands:

Serial number Command and description
1 HDEL key field1 [field2]
Delete one or more hash table fields
2 HEXISTS key field
Check whether the specified field exists in the hash table key.
3 HGET key field
gets the value of the specified field stored in the hash table.
4 HGETALL key
Get all the fields and values ​​of the specified key in the hash table
5 HINCRBY key field increment
is the integer value of the specified field in the hash table key plus the increment 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]
迭代哈希表中的键值对。

设置一个名称为baichao的hash表

获取hash表baichao中filed值为baichao1对应的value

获取hash表baichao中所有的filed和value

列举出哈希表baichao 中所有的filed值

列举出哈希表baichao 中所有的value值

删除哈希表baichao 的某一个field值和对应的value值

设置baichao的过期时间,并查看baichao的过期时间

删除baichao的过期时间

3、List(列表)

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

一个列表最多可以包含2^{32}​​​​​​​- 1 个元素 (4294967295, 每个列表超过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
为已存在的列表添加值

清空list:

4、Redis 集合(Set)

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

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

集合中最大的成员数为 2^{32} - 1 (4294967295, 每个集合可存储40多亿个成员)。

下表列出了 Redis 集合基本命令:

序号 命令及描述
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]
迭代集合中的元素

5、Redis 有序集合(sorted set)

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

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

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

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

下表列出了 redis 有序集合的基本命令:

序号 命令及描述
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 ...]
计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination 中
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]
迭代有序集合中的元素(包括元素成员和元素分值)

虽然influxdb是先插入的,但是因为因为它的分数值为2比较大,所以排序在libevent后面。

Guess you like

Origin blog.csdn.net/weixin_40179091/article/details/114867602