Redis 的基础使用 不同value类型的常用命令

目录

http://redis.cn/documentation.html 

一、基本的get set;

二、value 为String 相关

三、value 为List  类型相关命令

四、value为 Hash  类型相关命令

四、value为 Set  类型相关命令

五、value 为 sorted_set 类型去重且排序

 


http://redis.cn/documentation.html 

一、基本的get set;

SET key value [EX seconds|PX milliseconds|KEEPTTL] [NX|XX] ;

NX :只有不存在的时候才会创建该key,并且写入当前值,如果存在次key,则value 无法更新进去;应用场景,分布式锁等;

XX:只有key 存在的时候才会将value值更新进去,不存在的话,不会新建,不会更新,doNuthing;

MSET key value [key value ...]; mset k1 a k2 b;  get k1->a; get k2->b;

MGET key [key ...] mget k1 -> a; mget k1 k2-> a b;

TYPE key 描述key 的value 值的类型;set k1 99;type k1 ->string;

FLUSHALL [ASYNC]
  summary: Remove all keys from all databases
  since: 1.0.0
  group: server

KEYS pattern
  summary: Find all keys matching the given pattern
  since: 1.0.0
  group: generic

OBJECT subcommand [arguments [arguments ...]] 

  summary: Inspect the internals of Redis objects
  since: 2.2.3
  group: generic

 subcommand子命令,通过 object help 去查看支持哪些子命令

127.0.0.1:6379> object help
1) OBJECT <subcommand> arg arg ... arg. Subcommands are:
2) ENCODING <key> -- Return the kind of internal representation used in order to store the value associated with a key.
3) FREQ <key> -- Return the access frequency index of the key. The returned integer is proportional to the logarithm of the recent access frequency of the key.
4) IDLETIME <key> -- Return the idle time of the key, that is the approximated number of seconds elapsed since the last access to the key.
5) REFCOUNT <key> -- Return the number of references of the value associated with the specified key.

 关于encoding 的例子,如下;encoding 代表的是计算类型,数值是可以进行数值操作的,

127.0.0.1:6379> set k1 99
OK
127.0.0.1:6379> get k1
"99"
127.0.0.1:6379> type k1
string
127.0.0.1:6379> object ENCODING k1
"int"
127.0.0.1:6379> INCR k1
(integer) 100
127.0.0.1:6379> INCRBY k1 21
(integer) 121

encoding 会随着操作类型进行变化

当redis-cli 启动没有--raw ,则,只识别ascall 码,二进制的,get的中文会以二进制形式出现,如果带了会标准化出来,得到汉字;具体一个汉字有几个字节,则是由当前的编码来决定的,utf-8 3个字节,gbk2个等,所以k2和k3 是不一样的二进制,因为set 的时候是不一样的编码格式进行保存的;由此,redis 是二进制安全的,因为是字节流进行操作的,而不是字符流;


 

二、value 为String 相关

  • VALUE 为String  的时候,有以下几种运算,以及运用场景

  •  Help @String  查询value为String 的相关命令

对于字符串相关的操作,位数有正反向索引:正向索引0开始,逆向索引1开始;以字符串"hel"为例,start=1,end = 2 和start =1,end = -1的结果是一致的;

  APPEND key value
  summary: Append a value to a key
  since: 2.0.0

  BITCOUNT key [start end]
  summary: Count set bits in a string,bit 位为1 出现的次数;[start] [end] 字节索引
  since: 2.6.0

  BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
  summary: Perform arbitrary bitfield integer operations on strings
  since: 3.2.0

  BITOP operation destkey key [key ...]
  summary: Perform bitwise operations between strings,进行位的与或非操作 ,将 operation 结果存储到 destkey 中;
  since: 2.6.0

  BITPOS key bit [start] [end]
  summary: Find first bit set or clear in a string
  since: 2.8.7

  [start] [end] 字节索引

  k1 的value 二进制位是 下图 0100 0001

  DECR key
  summary: Decrement the integer value of a key by one
  since: 1.0.0

  DECRBY key decrement
  summary: Decrement the integer value of a key by the given number
  since: 1.0.0

  GET key
  summary: Get the value of a key
  since: 1.0.0

  GETBIT key offset
  summary: Returns the bit value at offset in the string value stored at key
  since: 2.2.0

  GETRANGE key start end
  summary: Get a substring of the string stored at a key
  since: 2.4.0

  GETSET key value
  summary: Set the string value of a key and return its old value
  since: 1.0.0

  INCR key
  summary: Increment the integer value of a key by one
  since: 1.0.0

  INCRBY key increment
  summary: Increment the integer value of a key by the given amount
  since: 1.0.0

  INCRBYFLOAT key increment
  summary: Increment the float value of a key by the given amount
  since: 2.6.0

  MGET key [key ...]
  summary: Get the values of all the given keys
  since: 1.0.0

  MSET key value [key value ...]
  summary: Set multiple keys to multiple values,原子性的
  since: 1.0.1

  MSETNX key value [key value ...]
  summary: Set multiple keys to multiple values, only if none of the keys exist
  since: 1.0.1

  PSETEX key milliseconds value
  summary: Set the value and expiration in milliseconds of a key
  since: 2.6.0

  SET key value [EX seconds|PX milliseconds|KEEPTTL] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0

  SETBIT key offset value
  summary: Sets or clears the bit at offset in the string value stored at key
  since: 2.2.0

  offset代表的是一个字节里,bit 位的索引,如下所示: 0100 0001 代表的是一个字节(1字节= 8 bit 位),上边圆圈值0为字节索引,下边的0-7 则代表bit(#`O′)索引;

  SETEX key seconds value
  summary: Set the value and expiration of a key
  since: 2.0.0

  SETNX key value
  summary: Set the value of a key, only if the key does not exist
  since: 1.0.0

  SETRANGE key offset value
  summary: Overwrite part of a string at key starting at the specified offset,对于offset 以后的字符串,将用value 去替换;
  since: 2.2.0

  STRALGO LCS algo-specific-argument [algo-specific-argument ...]
  summary: Run algorithms (currently LCS) against strings
  since: 6.0.0

  STRLEN key
  summary: Get the length of the value stored in a key
  since: 2.2.0

三、value 为List  类型相关命令

list 数据模型,以及可以实现的数据结构;

  • 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 an element 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 element
  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

  LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]
  summary: Return the index of matching elements on a 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

  BLPOP与BRPOP  表示阻塞式的弹出,当队列里有符合要求的元素,命令才会执行结束,否则会阻塞等待 timeout 时间,如果timeout = 0 会一直阻塞进去

  BRPOPLPUSH source destination timeout
  summary: Pop an element 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 element
  summary: Insert an element before or after another element in a list,在pivot 前或者后边插入一个element ;如果有多个pivot  则在第一个符合要求的地方插入;
  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

  LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]
  summary: Return the index of matching elements on a list
  since: 6.0.6

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

  LPUSHX key element [element ...]
  summary: Prepend an element 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;start stop也有正负索引,正向以0开始,反向以-1开始
  since: 1.0.0 

  LREM key count element
  summary: Remove elements from a list;从列表中移除count 个element 元素,当count 为正,则从正向开始查的前count个,count 为负的时候则从反向开始查的qian count绝对值 个
  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

  LTRIM key start stop
  summary: Trim a list to the specified range ; 将list 变为start 和stop  以内的 新list 
  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 element [element ...]
  summary: Append one or multiple elements to a list
  since: 1.0.0,与 LPUSH、RPOP、LPOP  组合,L、R 分别表示不同的方向,当同方向pop 和push 组合的话,得到的是一个栈的效果,不同方向的代表的是一个队列的效果;

  RPUSHX key element [element ...]
  summary: Append an element to a list, only if the list exists
  since: 2.2.0

四、value为 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 [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

四、value为 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  差集有方向性,具体根据key 的顺序进行差集的计算,演示看下图
  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,做交集,将交集结果放入一个新的key =destination 中
  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

  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

k1 有7个元素,-20 可以取20个,带有重复的元素集合

  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

五、value 为 sorted_set 类型去重且排序

数据模型:一个元素包括本身元素值,以及用于排序的分值,还有正反向的索引;当分值一样的时候,有字典序等序列

跳表分层

  跳表增加原理:类平衡树;  跳表有若干层,用空间换取增删改的时间;以新增 「11」 为例,先从最高的一层进行比较,比3大,nil之间,则降级到下层,比22小,则再次降级到最下层,进行比较,找到顺序的位置插入最下层,后边根据设置建层,补上边层的位置;

   help @sorted_set

  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] [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

  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

  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 ; 集合操作,两个集合的分支相加之后存入一个新的集合中,如果有权重,则按照每个集合分值乘以权重进行相加;如果有AGGREGATE  则选取满足要求的 最大最小或者相加后的值作为目标新集合的元素分值
  since: 2.0.0

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值