Redis笔记02

List常用命令

底层是一个字符串链表;可以从头或尾添加元素.

1、list全部命令

127.0.0.1:6379> help @list
#删除并获取列表中的第一个元素,或阻塞,直到有一个可用
  BLPOP key [key ...] timeout
  summary: Remove and get the first element in a list, or block until one is available
  since: 2.0.0
#删除并获取列表中的最后一个元素,或阻塞,直到有一个可用
  BRPOP key [key ...] timeout
  summary: Remove and get the last element in a list, or block until one is available
  since: 2.0.0

  BRPOPLPUSH source destination timeout
  summary: Pop a value from a list, push it to another list and return it; or block until one is available
  since: 2.2.0

  LINDEX key index
  summary: Get an element from a list by its index
  since: 1.0.0

  LINSERT key BEFORE|AFTER pivot value
  summary: Insert an element before or after another element in a list
  since: 2.2.0

  LLEN key
  summary: Get the length of a list
  since: 1.0.0

  LPOP key
  summary: Remove and get the first element in a list
  since: 1.0.0

  LPUSH key value [value ...]
  summary: Prepend one or multiple values to a list
  since: 1.0.0

  LPUSHX key value
  summary: Prepend a value to a list, only if the list exists
  since: 2.2.0

  LRANGE key start stop
  summary: Get a range of elements from a list
  since: 1.0.0

  LREM key count value
  summary: Remove elements from a list
  since: 1.0.0

  LSET key index value
  summary: Set the value of an element in a list by its index
  since: 1.0.0

  LTRIM key start stop
  summary: Trim a list to the specified range
  since: 1.0.0

  RPOP key
  summary: Remove and get the last element in a list
  since: 1.0.0

  RPOPLPUSH source destination
  summary: Remove the last element in a list, prepend it to another list and return it
  since: 1.2.0

  RPUSH key value [value ...]
  summary: Append one or multiple values to a list
  since: 1.0.0

  RPUSHX key value
  summary: Append a value to a list, only if the list exists
  since: 2.2.0

2、list常用操作

#lpush是向链表的左侧依次添加元素
127.0.0.1:6379> LPUSH k1 a b c d e 
(integer) 5
#获取链表中的数据
127.0.0.1:6379> LRANGE k1 0 -1
1) "e"
2) "d"
3) "c"
4) "b"
5) "a"
#RPUSH 向链表的右侧依次添加数据
127.0.0.1:6379> RPUSH k2 a b c d e
(integer) 5
127.0.0.1:6379> LRANGE k2 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
#k1中的元素edcba从左侧依次弹出
#同向的操作如Lpush和Lpop、rpush和rpop可以实现栈的数据结构先进后出
#反向的操作如Lpush和Rpop可以实现队列,先进先出
127.0.0.1:6379> LPOP k1
"e"
127.0.0.1:6379> LPOP k1
"d"
#获取指定索引处的元素
127.0.0.1:6379> LINDEX k1 2
"c"

127.0.0.1:6379> LPUSH k1 a b c d e
(integer) 5
#LINSERT key BEFORE|AFTER pivot value
#在某一个元素(pivot)前或后面添加一个value
127.0.0.1:6379> LINSERT k1 after c x
(integer) 6
127.0.0.1:6379> LRANGE k1 0 -1
1) "e"
2) "d"
3) "c"
4) "x"
5) "b"
6) "a"
#查看k1的长度
127.0.0.1:6379> LLEN k1
(integer) 6

#LREM key count value
#从左往右移除count个value
127.0.0.1:6379> LRANGE k1 0 -1
1) "c"
2) "a"
3) "e"
4) "d"
5) "c"
6) "x"
7) "b"
8) "a"
#从左往右移除2个a
127.0.0.1:6379> LREM k1 2 a
(integer) 2
127.0.0.1:6379> LRANGE k1 0 -1
1) "c"
2) "e"
3) "d"
4) "c"
5) "x"
6) "b"
#LSET key index value
#设置索引处的值为value
127.0.0.1:6379> LRANGE k1 0 -1
1) "c"
2) "e"
3) "d"
4) "c"
5) "x"
6) "a"
#设置索引4处的值为b
127.0.0.1:6379> LSET k1 4 b
OK
127.0.0.1:6379> LRANGE k1 0 -1
1) "c"
2) "e"
3) "d"
4) "c"
5) "b"
6) "a"

#LTRIM key start stop
#删除key的start和stop两端的数据
#删除k1 1前和-2后面的数据
127.0.0.1:6379> LTRIM k1 1 -2
OK
127.0.0.1:6379> LRANGE k1 0 -1
1) "e"
2) "d"
3) "c"
4) "b"

BLPOP和BRPOP删除并获取列表中的第一个元素,没有元素阻塞,一直直到有一个可用
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
拿到元素
在这里插入图片描述
再向k1中加元素
在这里插入图片描述
取到元素
在这里插入图片描述

2、hash常用操作

类似Java中的Map<String, Object>;是一个键值对集合;适合存储对象
1、hash全部操作

127.0.0.1:6379> help @hash

  HDEL key field [field ...]
  summary: Delete one or more hash fields
  since: 2.0.0

  HEXISTS key field
  summary: Determine if a hash field exists
  since: 2.0.0

  HGET key field
  summary: Get the value of a hash field
  since: 2.0.0

  HGETALL key
  summary: Get all the fields and values in a hash
  since: 2.0.0

  HINCRBY key field increment
  summary: Increment the integer value of a hash field by the given number
  since: 2.0.0

  HINCRBYFLOAT key field increment
  summary: Increment the float value of a hash field by the given amount
  since: 2.6.0

  HKEYS key
  summary: Get all the fields in a hash
  since: 2.0.0

  HLEN key
  summary: Get the number of fields in a hash
  since: 2.0.0

  HMGET key field [field ...]
  summary: Get the values of all the given hash fields
  since: 2.0.0

  HMSET key field value [field value ...]
  summary: Set multiple hash fields to multiple values
  since: 2.0.0

  HSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate hash fields and associated values
  since: 2.8.0

  HSET key field value
  summary: Set the string value of a hash field
  since: 2.0.0

  HSETNX key field value
  summary: Set the value of a hash field, only if the field does not exist
  since: 2.0.0

  HSTRLEN key field
  summary: Get the length of the value of a hash field
  since: 3.2.0

  HVALS key
  summary: Get all the values in a hash
  since: 2.0.0

2、hash常用操作

#设置user的name为zhangsan
127.0.0.1:6379> HSET user name zhangsan
(integer) 1
127.0.0.1:6379> hset user age 20
(integer) 1

127.0.0.1:6379> hget user name
"zhangsan"
127.0.0.1:6379> hget user age
"20"
#获得所有的user的属性
127.0.0.1:6379> HGETALL user
1) "name"
2) "zhangsan"
3) "age"
4) "20"
#获得用户value的所有key
127.0.0.1:6379> HKEYS user
1) "name"
2) "age"
#获得所有用户value的value值
127.0.0.1:6379> HVALS user
1) "zhangsan"
2) "20"

#获得用户的所有属性
127.0.0.1:6379> HGETALL user
1) "name"
2) "zhangsan"
3) "age"
4) "20"
5) "address"
6) "anyang"
7) "sex"
8) "femail"
#删除用户value为sex
127.0.0.1:6379> hdel user sex 
(integer) 1
127.0.0.1:6379> HGETALL user
1) "name"
2) "zhangsan"
3) "age"
4) "20"
5) "address"
6) "anyang"
#获取key的总value的长度
127.0.0.1:6379> HLEN user
(integer) 3
#获取某一个value的长度
127.0.0.1:6379> HSTRLEN user name
(integer) 8


127.0.0.1:6379> HMSET user name zhangsan age 18 address anyang
OK

127.0.0.1:6379> HMGET user name age address
1) "zhangsan"
2) "18"
3) "anyang"

3、set 常用操作

底层通过HashTable实现;不可重复,存取无序。

1、set所有操作

127.0.0.1:6379> help @set
#向集合增加
  SADD key member [member ...]
  summary: Add one or more members to a set
  since: 1.0.0
#获取集合长度
  SCARD key
  summary: Get the number of members in a set
  since: 1.0.0
#获取几个集合的差集
  SDIFF key [key ...]
  summary: Subtract multiple sets
  since: 1.0.0

  SDIFFSTORE destination key [key ...]
  summary: Subtract multiple sets and store the resulting set in a key
  since: 1.0.0
#交集
  SINTER key [key ...]
  summary: Intersect multiple sets
  since: 1.0.0
#获取交集并把交集内容存到目标集合中destination
  SINTERSTORE destination key [key ...]
  summary: Intersect multiple sets and store the resulting set in a key
  since: 1.0.0
#是否是集合中的元素
  SISMEMBER key member
  summary: Determine if a given value is a member of a set
  since: 1.0.0
#获取集合所有的value
  SMEMBERS key
  summary: Get all the members in a set
  since: 1.0.0
#移除
  SMOVE source destination member
  summary: Move a member from one set to another
  since: 1.0.0
#弹出
  SPOP key [count]
  summary: Remove and return one or multiple random members from a set
  since: 1.0.0
#从一个集合中获得一个或多个随机成员
  SRANDMEMBER key [count]
  summary: Get one or multiple random members from a set
  since: 1.0.0
#移除
  SREM key member [member ...]
  summary: Remove one or more members from a set
  since: 1.0.0

  SSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate Set elements
  since: 2.8.0
#并集
  SUNION key [key ...]
  summary: Add multiple sets
  since: 1.0.0

  SUNIONSTORE destination key [key ...]
  summary: Add multiple sets and store the resulting set in a key
  since: 1.0.0

2、常用操作

#向集合中加元素
127.0.0.1:6379> SADD k1 zhangsan lisi wangwu zhaoliu
(integer) 4
#查看集合的长度
127.0.0.1:6379> SCARD k1
(integer) 4
#查看集合中的元素
127.0.0.1:6379> SMEMBERS k1
1) "wangwu"
2) "zhangsan"
3) "zhaoliu"
4) "lisi"

127.0.0.1:6379> SMEMBERS k1
1) "0"
2) "1"
3) "2"
4) "3"
5) "4"
6) "6"
7) "7"
8) "9"
#SPOP key [count] 随机弹出count个元素
#随机弹出元素
127.0.0.1:6379> SPOP k1
"4"
127.0.0.1:6379> SPOP k1
"0"
#随机弹出三个元素
127.0.0.1:6379> SPOP k1 3
1) "1"
2) "3"
3) "9"

#SRANDMEMBER  key  count 
#count可以为正负数
#正数:取出一个去重的结果集、如果count大于集合长度就取出集合中的元素
#负数:取出一个带重复的结果集,一定满足你要的数量
#如果:0,不返回
127.0.0.1:6379> SRANDMEMBER k1 3
1) "1"
2) "3"
3) "7"
127.0.0.1:6379> SRANDMEMBER k1 -3
1) "0"
2) "1"
3) "1"

#移除
127.0.0.1:6379> SMEMBERS k1
1) "0"
2) "1"
3) "2"
4) "3"
5) "4"
6) "6"
7) "7"
8) "9"
#从集合中把6移除
127.0.0.1:6379> SREM k1 6
(integer) 1
127.0.0.1:6379> SMEMBERS k1
1) "0"
2) "1"
3) "2"
4) "3"
5) "4"
6) "7"
7) "9"
#交集
127.0.0.1:6379> SADD k1 3 2 1 4
(integer) 4
127.0.0.1:6379> SADD k2  8 9 3 1
(integer) 4
#k1、k2的交集是1、3
127.0.0.1:6379> SINTER k1 k2
1) "1"
2) "3"
#把k1、k2的交集放到一个目标key中
127.0.0.1:6379> SINTERSTORE inkey k1 k2
(integer) 2
127.0.0.1:6379> SMEMBERS inkey
1) "1"
2) "3"
#并集、查看k1、k2中所有的元素集合
127.0.0.1:6379> SUNION k1 k2
1) "1"
2) "2"
3) "3"
4) "4"
5) "8"
6) "9"
#差集key是有顺序的
#k1在前就是查k1中k2没有的元素
127.0.0.1:6379> SDIFF k1 k2
1) "2"
2) "4"
#k2在前就是查k2中k1没有的元素
127.0.0.1:6379> SDIFF k2 k1
1) "8"
2) "9"

4、sorted_set常用操作

类似Set;每个元素都会关联一个double类型的分数(score);Redis通过分数自动的为集合中的 成员进行从小到大的排序;成员不可重复,分数可以重复。

1、sorted_set所有操作

127.0.0.1:6379> help @sorted_set

  ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
  summary: Add one or more members to a sorted set, or update its score if it already exists
  since: 1.2.0

  ZCARD key
  summary: Get the number of members in a sorted set
  since: 1.2.0

  ZCOUNT key min max
  summary: Count the members in a sorted set with scores within the given values
  since: 2.0.0

  ZINCRBY key increment member
  summary: Increment the score of a member in a sorted set
  since: 1.2.0

  ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
  summary: Intersect multiple sorted sets and store the resulting sorted set in a new key
  since: 2.0.0

  ZLEXCOUNT key min max
  summary: Count the number of members in a sorted set between a given lexicographical range
  since: 2.8.9

  ZRANGE key start stop [WITHSCORES]
  summary: Return a range of members in a sorted set, by index
  since: 1.2.0

  ZRANGEBYLEX key min max [LIMIT offset count]
  summary: Return a range of members in a sorted set, by lexicographical range
  since: 2.8.9

  ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
  summary: Return a range of members in a sorted set, by score
  since: 1.0.5

  ZRANK key member
  summary: Determine the index of a member in a sorted set
  since: 2.0.0

  ZREM key member [member ...]
  summary: Remove one or more members from a sorted set
  since: 1.2.0

  ZREMRANGEBYLEX key min max
  summary: Remove all members in a sorted set between the given lexicographical range
  since: 2.8.9

  ZREMRANGEBYRANK key start stop
  summary: Remove all members in a sorted set within the given indexes
  since: 2.0.0

  ZREMRANGEBYSCORE key min max
  summary: Remove all members in a sorted set within the given scores
  since: 1.2.0

  ZREVRANGE key start stop [WITHSCORES]
  summary: Return a range of members in a sorted set, by index, with scores ordered from high to low
  since: 1.2.0

  ZREVRANGEBYLEX key max min [LIMIT offset count]
  summary: Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.
  since: 2.8.9

  ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
  summary: Return a range of members in a sorted set, by score, with scores ordered from high to low
  since: 2.2.0

  ZREVRANK key member
  summary: Determine the index of a member in a sorted set, with scores ordered from high to low
  since: 2.0.0

  ZSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate sorted sets elements and associated scores
  since: 2.8.0

  ZSCORE key member
  summary: Get the score associated with the given member in a sorted set
  since: 1.2.0

  ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
  summary: Add multiple sorted sets and store the resulting sorted set in a new key
  since: 2.0.0

2、sorted_set常用操作

#ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
#向集合中加元素
127.0.0.1:6379> ZADD k1 6 apple 3 banana 5 orange
(integer) 3
#查看集合所有的元素
127.0.0.1:6379> ZRANGE k1 0 -1
1) "banana"
2) "orange"
3) "apple"
#带分值查看所有的元素
127.0.0.1:6379> ZRANGE k1 0 -1 withscores
1) "banana"
2) "3"
3) "orange"
4) "5"
5) "apple"
6) "6"
#倒序排序、通常用于排行榜、音乐排行榜、热榜等
127.0.0.1:6379> ZREVRANGE k1 0 -1 withscores
1) "apple"
2) "6"
3) "orange"
4) "5"
5) "banana"
6) "3"
#查看集合的长度
127.0.0.1:6379> ZCARD k1
(integer) 3

#统计某个分值区间的元素个数
127.0.0.1:6379> ZADD English 65 liming 88 sean 90 lisir 98 jack
127.0.0.1:6379> ZCOUNT English 60 90
(integer) 3
#增加
127.0.0.1:6379> ZINCRBY English 1 liming
"66"

#交并差集
#ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
#numkeys 多少个key进行交并查集运算 、必填
#WEIGHTS weight权重,按照权重计算分值、可选
#AGGREGATE SUM|MIN|MAX可以进行加法和最小最大运算、可选默认是加法
127.0.0.1:6379> ZADD Chinese 85 lisir 63 jonh 97 xiaohong
127.0.0.1:6379> ZADD English 65 liming 88 sean 90 lisir 98 jack
#并集采用默认的权重和默认的运算
127.0.0.1:6379> ZUNIONSTORE unkey 2 Chinese English 
(integer) 6
127.0.0.1:6379> ZRANGE unkey 0 -1 withscores
 1) "jonh"
 2) "63"
 3) "liming"
 4) "66"
 5) "sean"
 6) "88"
 7) "xiaohong"
 8) "97"
 9) "jack"
10) "98"
11) "lisir"
12) "175"
#按语文的百分百和英语的分值的百分之50进行加法计算
127.0.0.1:6379> ZUNIONSTORE unkey1 2 Chinese English weights 1 0.5
(integer) 6
127.0.0.1:6379> ZRANGE unkey1 0 -1 withscores
 1) "liming"
 2) "33"
 3) "sean"
 4) "44"
 5) "jack"
 6) "49"
 7) "jonh"
 8) "63"
 9) "xiaohong"
10) "97"
11) "lisir"
12) "130"


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值