2021-04-21 redis-五种数据结构

本文详细展示了Redis中String、List、Hash、Set和Sorted Set的数据操作,包括设置、获取、更新、删除、计数、遍历等基本操作,以及一些高级特性的使用,如位操作、哈希字段的数值计算、集合的交并差操作、有序集合的排序与分数计算等。同时,文中还提及了Redis在面试中常见的问题,如如何利用Bitmap记录用户登录信息和去重,以及Redis的二进制安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

连接

redis-cli -p 6379

--连接8号库

 redis-cli -p 6379 -n 8

redis有0-15共16个库

1、string(字符串、数值、bitmap)

27.0.0.1:6379[8]> set k1 hello --设置值
OK
127.0.0.1:6379[8]> get k1
"hello"
127.0.0.1:6379[8]> set k1 he nx  --不存在才能新增
(nil)
127.0.0.1:6379[8]> set k1 he xx -- 更新
127.0.0.1:6379[8]> mset k2 1 h3 2
OK
127.0.0.1:6379[8]> mget k2 h3
1) "1"
2) "2"
127.0.0.1:6379[8]> APPEND k1 ' worlod '
(integer) 10
127.0.0.1:6379[8]> get k1
"he worlod "
127.0.0.1:6379[8]> GETRANGE k1 -8 -1
" worlod "
127.0.0.1:6379[8]> SETRANGE k1 4 shijinqunal
(integer) 15
127.0.0.1:6379[8]> get k1
"he wshijinqunal"
127.0.0.1:6379[8]> 

127.0.0.1:6379[8]> STRLEN k1
(integer) 15

127.0.0.1:6379[8]> OBJECT encoding k1
"int"
127.0.0.1:6379[8]> INCR k1
(integer) 101
127.0.0.1:6379[8]> INCRBY k1 22
(integer) 123
127.0.0.1:6379[8]> DECRBY k1 88
(integer) 35
127.0.0.1:6379[8]> INCRBYFLOAT k1 0.2
"35.2"
127.0.0.1:6379[8]> 

127.0.0.1:6379[8]> set k3 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
OK
127.0.0.1:6379[8]> OBJECT encoding k3
"embstr"
127.0.0.1:6379[8]> APPEND k3 jjjjjjjjjjjjjjjjjjjjjjjj
(integer) 56
127.0.0.1:6379[8]> OBJECT encoding k3
"raw"
127.0.0.1:6379[8]> 
127.0.0.1:6379[8]> SETBIT k1 1 1
(integer) 0
127.0.0.1:6379[8]> get k1
"@"
127.0.0.1:6379[8]> set k1 7 1
(error) ERR syntax error
127.0.0.1:6379[8]> SETBIT k1 7 1 
(integer) 0
127.0.0.1:6379[8]> get k1
"A"
127.0.0.1:6379[8]> SETBIT k1 9 1
(integer) 0
127.0.0.1:6379[8]> get k1
"A@"
127.0.0.1:6379[8]> 

bitmap

127.0.0.1:6379[8]> SETBIT k1 7 1 
(integer) 0
127.0.0.1:6379[8]> get k1
"A"

127.0.0.1:6379[8]> BITPOS k1 1 0  1
(integer) 1
127.0.0.1:6379[8]> BITPOS k1 1 1 1
(integer) 9

127.0.0.1:6379[8]> BITCOUNT k1 0 1 
(integer) 3

127.0.0.1:6379[8]> SETBIT k1 1 1
(integer) 0
127.0.0.1:6379[8]> get k1
"@"
127.0.0.1:6379[8]> SETBIT k2 2 1
(integer) 0
127.0.0.1:6379[8]> get k2
" "
127.0.0.1:6379[8]> BITOP or andkey k1 k2
(integer) 1
127.0.0.1:6379[8]> get andkey
"`"
127.0.0.1:6379[8]> 

redis bitmap面试题:

用户登陆信息

用户登陆数量 去重

redis二进制安全

2、list

可以实现:

栈:同向命令

lpush lpop

队列:反向命令

lpush rpop

数组:lindex

阻塞単波队列

lindex key index
lset key index value
lrem key  移除几个 字符
LINSERT key BEFORE|AFTER pivot element
LREM key count element

添加元素 
LPUSH key element [element ...]
summary: Prepend one or multiple elements to a list
since: 1.0.0
RPUSH key element [element ...]
summary: Append one or multiple elements to a list
since: 1.0.0
删除元素 弹出元素
RPOP key [count]
summary: Remove and get the last elements in a list
since: 1.0.0
LPOP key [count]
summary: Remove and get the first elements in a list
since: 1.0.0
下标取数
LINDEX key index
summary: Get an element from a list by its index
since: 1.0.0
下标替换值
LSET key index element
summary: Set the value of an element in a list by its index
since: 1.0.0
移除字符
LREM key count element
summary: Remove elements from a list
since: 1.0.0
在某个元素前后插入元素
LINSERT key BEFORE|AFTER pivot element
summary: Insert an element before or after another element in a list
since: 2.2.0
获取元素
LRANGE key start stop
summary: Get a range of elements from a list
since: 1.0.0

统计长度
LLEN key
summary: Get the length of a list
since: 1.0.0

删除两端元素
LTRIM key start stop
summary: Trim a list to the specified range
since: 1.0.0
 

 

3、hash

可以对field进行数值计算

场景:点赞、收藏、详情页。

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

HRANDFIELD key [count [WITHVALUES]]
summary: Get one or multiple random fields from a hash
since: 6.2.0

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

HSET key field value [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

4、set

无序、去重

随机事件:SPOP 取出一个  SRANDMEMBER k1 100 取出多个

--添加

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

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

SMEMBERS key
summary: Get all the members in a set
since: 1.0.0

SMISMEMBER key member [member ...]
summary: Returns the membership associated with the given elements for a set
since: 6.2.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

--count 为负数时可以重复

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
 

5、sorted set

 

ZUNIONSTORE unkey 2 k1 k2 weights 1 0.5 交集 带权重 (相同则求和)

 


  BZPOPMAX key [key ...] timeout
  summary: Remove and return the member with the highest score from one or more sorted sets, or block until one is available
  since: 5.0.0

  BZPOPMIN key [key ...] timeout
  summary: Remove and return the member with the lowest score from one or more sorted sets, or block until one is available
  since: 5.0.0
  
--新增
  ZADD key [NX|XX] [GT|LT] [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

  ZDIFF numkeys key [key ...] [WITHSCORES]
  summary: Subtract multiple sorted sets
  since: 6.2.0

  ZDIFFSTORE destination numkeys key [key ...]
  summary: Subtract multiple sorted sets and store the resulting sorted set in a new key
  since: 6.2.0

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

  ZINTER numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
  summary: Intersect multiple sorted sets
  since: 6.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

  ZMSCORE key member [member ...]
  summary: Get the score associated with the given members in a sorted set
  since: 6.2.0

  ZPOPMAX key [count]
  summary: Remove and return members with the highest scores in a sorted set
  since: 5.0.0

  ZPOPMIN key [count]
  summary: Remove and return members with the lowest scores in a sorted set
  since: 5.0.0

  ZRANDMEMBER key [count [WITHSCORES]]
  summary: Get one or multiple random elements from a sorted set
  since: 6.2.0

  --查询
  ZRANGE key min max [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]
  summary: Return a range of members in a sorted set
  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

  ZRANGESTORE dst src min max [BYSCORE|BYLEX] [REV] [LIMIT offset count]
  summary: Store a range of members from sorted set into another key
  since: 6.2.0

  --取出排名
  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

  ZUNION numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
  summary: Add multiple sorted sets
  since: 6.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

排序是怎么实现的

skip list 跳跃表 随机造层 类平衡树

平均值相对最优

 

有序 正负索引

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心系代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值