Redis learning-redis five data types

Introduction to the five data types

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 .
That is, the k1 v1, k2 v2
string types in the example are 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. The string value in a redis can be up to 512M

Hash (hash, similar to Map in java)

Redis hash is a collection of key-value pairs .
Redis hash is a mapping table between field and value of string type, and hash is particularly suitable for storing objects.
Similar to Map<String,Object> in Java

List

Redis lists are simple lists of strings , sorted in the order of insertion . You can add an element to the head (left) or tail (right) of the list .
Its bottom layer is actually a linked list

Set

Redis Set is an unordered collection of string type . It is achieved through HashTable . Disorder and no duplication

Zset(sorted set: ordered set)

Redis zset , like set, is also a collection of string type elements, and duplicate members are not allowed .
The difference is that each element is associated with a double type score .
Redis uses scores to sort the members of the collection from small to large . The members of zset are unique, but the score can be repeated.

Where to get redis common data type operation commands
https://redis.io/commands
http://redisdoc.com/

key keyword

keys *:
View the keys of all key-value pairs. Exists key
name:
Determine whether a key exists.
Insert picture description here
Move key db: The
current library is gone. It is removed, and the
Insert picture description here
expire key can be found in the specified library. Seconds:
Set the expiration time for the given key.
ttl key:
Check how many seconds are left to expire, -1 means never expire, -2 means expired
Insert picture description here
Set the expiration time of k2 here, which is equivalent to deleting k2

type key:
Check what type your key is

String

Commonly used

command description
SET key value Set the value of the specified key
GET key Get the value of the specified key.
GETRANGE key start end Returns the subcharacter of the string value in key
GETSET key value Set the value of the given key to value and return the old value of the key.
GETBIT key offset Obtain the bit at the specified offset for the string value stored in key.
MGET key1 [key2…] Get all (one or more) values ​​of a given key.
SETBIT key offset value For the string value stored in key, set or clear the bit at the specified offset.
SETEX key seconds value Associate the value value with the key, and set the expiration time of the key to seconds (in seconds).
SETNX key value Only set the value of the key when the key does not exist.
SETRANGE key offset value Use the value parameter to overwrite the string value stored in the given key, starting from the offset offset.
STRLEN key Returns the length of the string value stored in key.
MSET key value [key value …] Set one or more key-value pairs at the same time.
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.
PSETEX key milliseconds value This 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.
INCR key Increase the numeric value stored in the key by one.
INCRBY key increment Add the value stored in the key to the given increment (increment).
INCRBYFLOAT key increment Add the value stored in the key to the given floating point increment (increment).
DECR key Decrease the numeric value stored in the key by one.
DECRBY key decrement The value stored in key minus the given decrement value (decrement).
APPEND key value If the key already exists and is a string, the APPEND command appends the specified value to the end of the original value of the key.

Single value

Case

set/get/del/append/strlen
Insert picture description here
Incr/decr/incrby/decrby: It must be a number to add and subtract.
Insert picture description here
INCR means add 1
each time INCRBY means add the specified number each time

setex(set with expire) key seconds value/setnx(set if not exist)
setex means set survival time, setnx means if it does not exist, add a key-value pair, otherwise, if it exists, it will not overwrite

Insert picture description here
Insert picture description here
the return value here 0 means ineffective, return value 1 means effective
getrange/setrange
getrange: get the value in the specified range, similar to the relationship between...and
Insert picture description here
setrange sets the value in the specified range, the format is setrange key value, specific value Insert picture description here
mset/mget/msetnx
sets multiple key-value pairs at the same time.
Insert picture description here
When setting msetnx, when some of the multiple key-value pairs already exist, all of them will be invalid.

List

Commonly used

command description
BLPOP key1 [key2 ] timeout Move out and get the first element of the list. If there are no elements in the list, the list will be blocked until the waiting timeout or an element that can be popped is found.
BRPOP key1 [key2 ] timeout Move out and get the last element of the list. If there are no elements in the list, the list will be blocked until the waiting timeout or an element that can be popped is found.
BRPOPLPUSH source destination timeout Pop a value from the list, insert the popped element into another list and return it; if there are no elements in the list, the list will be blocked until the waiting timeout or a pop-up element is found.
LINDEX key index Get the elements in the list by index
LINSERT key BEFORE/AFTER pivot value Insert elements before or after the elements of the list
FILL key Get list length
LPOP key 移出并获取列表的第一个元素
LPUSH key value1 [value2] 将一个或多个值插入到列表头部
LPUSHX key value 将一个值插入到已存在的列表头部
LRANGE key start stop 获取列表指定范围内的元素
LREM key count value 移除列表元素
LSET key index value 通过索引设置列表元素的值
LTRIM key start stop 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。
RPOP key 移除列表的最后一个元素,返回值为移除的元素。
RPOPLPUSH source destination 移除列表的最后一个元素,并将该元素添加到另一个列表并返回
RPUSH key value1 [value2] 在列表中添加一个或多个值
RPUSHX key value 为已存在的列表添加值

单值多value

案例

lpush/rpush/lrange
 LPUSH key value1 [value2] 将一个或多个值插入到列表头部
 LPUSHX key value 将一个值插入到已存在的列表头部
 LRANGE key start stop 获取列表指定范围内的元素

Insert picture description here
解释:
lpush添加进去的元素,右边的放在最前面,左边的放在最后面
rpush则相反
0 -1表示截取所有的元素
记住:只有RPUSH是怎么进怎么出
或者这样想,lpush是头插法,rpush是尾插法
头插法插入的顺序总是与最终的顺序是相反的。

lpop/rpop
Insert picture description here
lpop:移出并获取列表最左边的一个,即第一个元素,l是头部元素
rpop:移出并获取列表最右边的一个元素,即最后一个元素,r是尾部元素

lindex:
通过索引获取列表中的元素(从上到下)
Insert picture description here
即从索引0开始。list是从头部到尾部顺序
len
获取列表长度
Insert picture description here
lrem key 删N个value
Insert picture description here
图中是删除2个3
ltrim key开始index结束index,截取指定范围的值然后再复制给key
Insert picture description here
相当于把原来的列表中选取的一部分截取出来,然后再放到list01中,剩余的部分就删除了。
rpop lpush源列表目的列表
 相当于把源列表中的尾部元素弹出放在目的列表的头部

Insert picture description here
linsert key before/after 值1 值2

Insert picture description here

性能总结:

它是一个字符串链表,left、right都可以插入添加;
如果键不存在,创建新的链表;
如果键已存在,新增内容;
如果值全移除,对应的键也就消失了。
链表的操作无论是头和尾效率都极高,但假如是对中间元素进行操作,效率就很惨淡了。

Set

单值多value

常用

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

案例

sadd/smembers/sismember
Insert picture description here
sadd是添加,重复的会自动删除
smembers set01表示展示set01中的所有元素,set没有下标,因此不能用0 -1
sismembers表示判断集合中有没有这个元素

scard获取集合里面的元素个数
Insert picture description here
srem key value删除集合中元素
Insert picture description here
srandmember key 某个整数,随机选出几个数
Insert picture description here
spop key 随机出栈
Insert picture description here
smove key1 key2 在key1里的某个值,作用是将key1里的某个值赋值给key2值
Insert picture description here
数学集合类
差集:sdiff
在第一个set里面而不在后面任何一个set里面的项
交集:sinter
并集:sunion
Insert picture description here

哈希Hash

常用

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

KV模式不变,但V是一个键值对

案例

hset/hget/hmset/hmget/hgetall/hdel

Insert picture description here
注意,里面又是一层键值对
Insert picture description here
hlen
Insert picture description here
hexists key 在key里面的某个值key是否存在
Insert picture description here
有时1,没有是0
hkeys/hvals
Insert picture description here
获取所有哈希表中的字段和值
hincrby/hincrbyfloat
Insert picture description here
hsetnx
Insert picture description here

有序集合ZSet

在set基础上,加一个score值。 之前set是k1 v1 v2 v3, 现在zset是k1 score1 v1 score2 v2

常用

命令 描述
ZADD key score1 member1 [score2 member2] 向有序集合添加一个或多个成员,或者更新已存在成员的分数
ZCARD key 获取有序集合的成员数
ZCOUNT key min max 计算在有序集合中指定区间分数的成员数
ZINCRBY key increment member 有序集合中对指定成员的分数加上增量 increment
ZINTERSTORE destination numkeys key [key …] 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中
ZLEXCOUNT key min max 在有序集合中计算指定字典区间内成员数量
ZRANGE key start stop [WITHSCORES] 通过索引区间返回有序集合指定区间内的成员
ZRANGEBYLEX key min max [LIMIT offset count] 通过字典区间返回有序集合的成员
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] 通过分数返回有序集合指定区间内的成员
ZRANK key member 返回有序集合中指定成员的索引
ZREM key member [member …] 移除有序集合中的一个或多个成员
ZREMRANGEBYLEX key min max 移除有序集合中给定的字典区间的所有成员
ZREMRANGEBYRANK key start stop 移除有序集合中给定的排名区间的所有成员
ZREMRANGEBYSCORE key min max 移除有序集合中给定的分数区间的所有成员
ZREVRANGE key start stop [WITHSCORES] 返回有序集中指定区间内的成员,通过索引,分数从高到低
ZREVRANGEBYSCORE key max min [WITHSCORES] 返回有序集中指定分数区间内的成员,分数从高到低排序
ZREVRANK key member 返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序
ZSCORE key member 返回有序集中,成员的分数值
ZUNIONSTORE destination numkeys key [key …] 计算给定的一个或多个有序集的并集,并存储在新的 key 中
ZSCAN key cursor [MATCH pattern] [COUNT count] 迭代有序集合中的元素(包括元素成员和元素分值)

案例

zadd/zrange
Insert picture description here
value是一对一对出现的

zrangebyscore key start score and end score
withscores
(indicating that
limit is not included. The function is to return the limit. The start of the subscript step.

Insert picture description here
Limit indicates the 2 steps from 2
zrem key. The corresponding value under a certain score. The function is to delete the element
Insert picture description here
zcard/zcount key score Interval/zrank key values ​​value, the function is to obtain the subscript value/zcore key corresponding value, obtain the score
Insert picture description here
zrevrank key values ​​value, the function is to obtain the subscript value in reverse order

Guess you like

Origin blog.csdn.net/qq_39736597/article/details/111193369