连接
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) 15127.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) 9127.0.0.1:6379[8]> BITCOUNT k1 0 1
(integer) 3127.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.0HEXISTS key field
summary: Determine if a hash field exists
since: 2.0.0HGET key field
summary: Get the value of a hash field
since: 2.0.0HGETALL key
summary: Get all the fields and values in a hash
since: 2.0.0HINCRBY key field increment
summary: Increment the integer value of a hash field by the given number
since: 2.0.0HINCRBYFLOAT key field increment
summary: Increment the float value of a hash field by the given amount
since: 2.6.0HKEYS key
summary: Get all the fields in a hash
since: 2.0.0HLEN key
summary: Get the number of fields in a hash
since: 2.0.0HMGET key field [field ...]
summary: Get the values of all the given hash fields
since: 2.0.0HMSET key field value [field value ...]
summary: Set multiple hash fields to multiple values
since: 2.0.0HRANDFIELD key [count [WITHVALUES]]
summary: Get one or multiple random fields from a hash
since: 6.2.0HSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate hash fields and associated values
since: 2.8.0HSET key field value [field value ...]
summary: Set the string value of a hash field
since: 2.0.0HSETNX key field value
summary: Set the value of a hash field, only if the field does not exist
since: 2.0.0HSTRLEN key field
summary: Get the length of the value of a hash field
since: 3.2.0HVALS 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.0SCARD key
summary: Get the number of members in a set
since: 1.0.0--差集
SDIFF key [key ...]
summary: Subtract multiple sets
since: 1.0.0SDIFFSTORE 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.0SINTERSTORE 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.0SMEMBERS key
summary: Get all the members in a set
since: 1.0.0SMISMEMBER key member [member ...]
summary: Returns the membership associated with the given elements for a set
since: 6.2.0SMOVE source destination member
summary: Move a member from one set to another
since: 1.0.0SPOP 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.0SREM key member [member ...]
summary: Remove one or more members from a set
since: 1.0.0SSCAN 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.0SUNIONSTORE 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.0BZPOPMIN 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.0ZCARD key
summary: Get the number of members in a sorted set
since: 1.2.0ZCOUNT key min max
summary: Count the members in a sorted set with scores within the given values
since: 2.0.0ZDIFF numkeys key [key ...] [WITHSCORES]
summary: Subtract multiple sorted sets
since: 6.2.0ZDIFFSTORE 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.0ZINTER numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
summary: Intersect multiple sorted sets
since: 6.2.0ZINTERSTORE 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.0ZLEXCOUNT key min max
summary: Count the number of members in a sorted set between a given lexicographical range
since: 2.8.9ZMSCORE key member [member ...]
summary: Get the score associated with the given members in a sorted set
since: 6.2.0ZPOPMAX key [count]
summary: Remove and return members with the highest scores in a sorted set
since: 5.0.0ZPOPMIN key [count]
summary: Remove and return members with the lowest scores in a sorted set
since: 5.0.0ZRANDMEMBER 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.0ZRANGEBYLEX 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.5ZRANGESTORE 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.0ZREM key member [member ...]
summary: Remove one or more members from a sorted set
since: 1.2.0ZREMRANGEBYLEX key min max
summary: Remove all members in a sorted set between the given lexicographical range
since: 2.8.9ZREMRANGEBYRANK key start stop
summary: Remove all members in a sorted set within the given indexes
since: 2.0.0ZREMRANGEBYSCORE 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.0ZREVRANGEBYLEX 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.9ZREVRANGEBYSCORE 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.0ZREVRANK key member
summary: Determine the index of a member in a sorted set, with scores ordered from high to low
since: 2.0.0ZSCAN 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.0ZUNION numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
summary: Add multiple sorted sets
since: 6.2.0ZUNIONSTORE 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 跳跃表 随机造层 类平衡树
平均值相对最优
有序 正负索引