The use of five data types in Redis

String type

Introduction

The string type is the most basic data storage type in Redis. It is binary safe in Redis, which means that the type can accept
data in any format, such as JPEG image data or Json object description information. In Redis, the maximum data length that a string type Value can hold
is 512M.

operating

  • set/get/append/strlen
$ redis­cli
127.0.0.1:6379> select 0 #切换到第1个数据库,默认共有16个数据库,
索引从0开始
                   
OK
127.0.0.1:6379> keys * #显示所有的键key                      
(empty list or set)
127.0.0.1:6379> set name tom #设置键                
OK
127.0.0.1:6379> get name #获取键对应的值                    
"tom"
127.0.0.1:6379> exists mykey            #判断该键是否存在,存在返回1,不存在返回
0
    
(integer) 0
127.0.0.1:6379> append mykey "hello"       #如果该键不存在,则创建,返回当前value
的长度
(integer) 5
127.0.0.1:6379> append mykey " world"      #如果该键已经存在,则追加,返回追加后
value的长度
(integer) 11
127.0.0.1:6379> get mykey                   #获取mykey的值
"hello world"
127.0.0.1:6379> strlen mykey               #获取mykey的长度 
(integer) 11
#EX和PX表示失效时间,单位为秒和毫秒,两者不能同时使用;NX表示数据库中不存在时才能设置,XX表
示存在时才能设置
127.0.0.1:6379> set mykey "this is test"  EX 5 NX 
OK
127.0.0.1:6379> get mykey
"this is test"

Note: Commands are not case sensitive, but key and value are case sensitive

  • incr/decr/incrby/decrby
127.0.0.1:6379> flushdb    #清空数据库                  
OK
127.0.0.1:6379> set mykey 20
OK
127.0.0.1:6379> incr mykey           #递增1        
(integer) 21
127.0.0.1:6379> decr mykey            #递减1       
(integer) 20
127.0.0.1:6379> del mykey           #删除该键         
(integer) 1
127.0.0.1:6379> decr mykey
(integer) ­1
127.0.0.1:6379> del mykey
(integer) 1
127.0.0.1:6379> INCR mykey
(integer) 1
127.0.0.1:6379> set mykey 'hello'    #将该键的Value设置为不能转换为整型的普
通字符串
       
OK
127.0.0.1:6379> incr mykey           #在该键上再次执行递增操作时,Redis将报
告错误信息
       
(error) ERR value is not an integer or out of range
127.0.0.1:6379> set mykey 10
OK
127.0.0.1:6379> incrby mykey 5         #递增5,即步长      
(integer) 15
127.0.0.1:6379> decrby mykey 10        #递减10      
(integer) 5
  • getset / setex / setnx
#   getset 获取的同时并设置新的值  
127.0.0.1:6379> incr mycount        #将计数器的值原子性的递增1         
(integer) 1
127.0.0.1:6379> getset mycount 666 #在获取计数器原有值的同时,并将其设置为
新值
         
"1"
127.0.0.1:6379> get mycount
"666"
# setex   设置过期时间   
127.0.0.1:6379> setex mykey 10 "hello"     #设置指定Key的过期时间为10秒,等同于set
mykey hello ex 10
 
OK
127.0.0.1:6379> ttl mykey     #查看指定Key的过期时间(秒数)               
(integer) 8
# setnx 当key不存在时才能设置         
127.0.0.1:6379> del mykey
(integer) 0
127.0.0.1:6379> setnx mykey "aaa"          #key不存在,可以设置,等同于set mykey
aaa nx
(integer) 1
127.0.0.1:6379> setnx mykey "bbb"         #key存在,不能设置   
(integer) 0
127.0.0.1:6379> get mykey
"aaa"
  • setrange/getrange set/get the character at the specified index position
127.0.0.1:6379> set mykey "hello world"
OK
127.0.0.1:6379> get mykey
"hello world"
127.0.0.1:6379> setrange mykey 6 dd        #从索引为6的位置开始替换(索引从0开始) 
(integer) 11
127.0.0.1:6379> get mykey
"hello ddrld"
127.0.0.1:6379> setrange mykey 20 dd        #超过的长度使用0代替
(integer) 22
127.0.0.1:6379> get mykey
"hello ddrld\x00\x00\x00\x00\x00\x00\x00\x00\x00dd"
127.0.0.1:6379> getrange mykey 3 12        #获取索引为[3,12]之间的内容 
"lo ddrld\x00\x00"
  • setbit/getbit set/get the BIT value of the specified bit, application scenario: time attendance punch card
127.0.0.1:6379> del mykey
(integer) 1
127.0.0.1:6379> setbit mykey 7 1        #设置从0开始计算的第七位BIT值为1,返回
原有BIT值0
    
(integer) 0
127.0.0.1:6379> get mykey                 #获取设置的结果,二进制的0000 0001的十
六进制值为0x01
  
"\x01"
127.0.0.1:6379> setbit mykey 6 1        #设置从0开始计算的第六位BIT值为1,返回
原有BIT值0
    
(integer) 0
127.0.0.1:6379> get mykey                 #获取设置的结果,二进制的0000 0011的十
六进制值为0x03
  
"\x03"
127.0.0.1:6379> getbit mykey 6           #返回了指定Offset的BIT值    
(integer) 1
127.0.0.1:6379> getbit mykey 10         #如果offset已经超出了value的长度,则返
回0
    
(integer) 0
  • mset/mget/msetnx
127.0.0.1:6379> mset key1 "hello" key2 "world" #批量设置了key1和key2两个键。 
OK
127.0.0.1:6379> mget key1 key2              #批量获取了key1和key2两个键的值。     
1) "hello"
2) "world"
#批量设置了key3和key4两个键,因为之前他们并不存在,所以该命令执行成功并返回1
127.0.0.1:6379> msetnx key3 "itany" key4 "liu"
(integer) 1
127.0.0.1:6379> mget key3 key4                  
1) "itany"
2) "liu"
#批量设置了key3和key5两个键,但是key3已经存在,所以该命令执行失败并返回0
127.0.0.1:6379> msetnx key3 "hello" key5 "world"
(integer) 0
#批量获取key3和key5,由于key5没有设置成功,所以返回nil
127.0.0.1:6379> mget key3 key5                  
1) "itany"
2) (nil)

Set type

Overview

在Redis中,我们可以将Set类型看作为没有排序的字符集合,也可以在该类型的数据值上执行添加、删除或判
断某一元素是否存在等操作。Set可包含的最大元素数量是4294967295。
   和List类型不同的是,Set集合中不允许出现重复的元素,这一点和Java中的set容器是完全相同的。换句话
说,如果多次添加相同元素,Set中将仅保留该元素的一份拷贝。和List类型相比,Set类型在功能上还存在着一个
非常重要的特性,即在服务器端完成多个Sets之间的聚合计算操作,如unions并、intersections交和differences
差。由于这些操作均在服务端完成,因此效率极高,而且也节省了大量的网络IO开销。

operating

  • sadd / smembers / sismember / scard
#由于该键myset之前并不存在,因此参数中的三个成员都被正常插入
127.0.0.1:6379> sadd myset a b c
(integer) 3
#查看集合中的元素,从结果可以,输出的顺序和插入顺序无关(无序的)
127.0.0.1:6379> smembers myset
1) "a"
2) "c"
3) "b"
#由于参数中的a在myset中已经存在,因此本次操作仅仅插入了d和e两个新成员(不允许重复)
127.0.0.1:6379> sadd myset a d e
(integer) 2
#查看插入的结果
127.0.0.1:6379> smembers myset
1) "a"
2) "c"
3) "d"
4) "b"
5) "e"
#判断a是否已经存在,返回值为1表示存在
127.0.0.1:6379> sismember myset a
(integer) 1
#判断f是否已经存在,返回值为0表示不存在
127.0.0.1:6379> sismember myset f
(integer) 0
#获取集合中元素的数量
127.0.0.1:6379> scard myset
(integer) 5
  • srandmember/spop/srem/smove
127.0.0.1:6379> del myset
(integer) 1
#准备测试数据
127.0.0.1:6379> sadd myset a b c d
(integer) 4
#查看集合中的元素
127.0.0.1:6379> smembers myset
1) "c"
2) "d"
3) "a"
4) "b"
#随机返回一个成员,成员还在集合中
127.0.0.1:6379> srandmember myset
"c"
#取出一个成员,成员会从集合中删除
127.0.0.1:6379> spop myset
"b"
#查看移出后Set的成员信息
127.0.0.1:6379> smembers myset
1) "c"
2) "d"
3) "a"
#从Set中移出a、d和f三个成员,其中f并不存在,因此只有a和d两个成员被移出,返回为2
127.0.0.1:6379> srem myset a d f
(integer) 2
#查看移出后的输出结果
127.0.0.1:6379> smembers myset
1) "c"
127.0.0.1:6379> del myset
(integer) 1
#为后面的smove命令准备数据
127.0.0.1:6379> sadd myset a b
(integer) 2
127.0.0.1:6379> sadd myset2 c d
(integer) 2
#将a从myset移到myset2,从结果可以看出移动成功
127.0.0.1:6379> smove myset myset2 a
(integer) 1
#再次将a从myset移到myset2,由于此时a已经不是myset的成员了,因此移动失败并返回0。
127.0.0.1:6379> smove myset myset2 a
(integer) 0
#分别查看myset和myset2的成员,确认移动是否真的成功。
127.0.0.1:6379> smembers myset
1) "b"
127.0.0.1:6379> smembers myset2
1) "c"
2) "d"
3) "a"
  • sdiff/sdiffstore/sinter/sinterstore/sunion/sunionstore
127.0.0.1:6379> flushdb
OK
#准备测试数据
127.0.0.1:6379> sadd myset a b c d
(integer) 4
127.0.0.1:6379> sadd myset2 c
(integer) 1
127.0.0.1:6379> sadd myset3 a c e
(integer) 3
#获取多个集合之间的不同成员,要注意匹配的规则
#先将myset和myset2进行比较,a、b和d三个成员是两者之间的差异成员,然后再用这个结果继续和
myset3进行差异比较,b和d是myset3不存在的成员
127.0.0.1:6379> sdiff myset myset2 myset3
1) "d"
2) "b"
127.0.0.1:6379> sdiff myset3 myset2 myset
1) "e"
#将3个集合的差异成员存储到与diffkey关联的Set中,并返回插入的成员数量
127.0.0.1:6379> sdiffstore diffkey myset myset2 myset3
(integer) 2
#查看一下sdiffstore的操作结果
127.0.0.1:6379> smembers diffkey
1) "d"
2) "b"
#获取多个集合之间的交集,这三个Set的成员交集只有c
127.0.0.1:6379> sinter myset myset2 myset3
1) "c"
#将3个集合中的交集成员存储到与interkey关联的Set中,并返回交集成员的数量
127.0.0.1:6379> sinterstore interkey myset myset2 myset3
(integer) 1
#查看一下sinterstore的操作结果
127.0.0.1:6379> smembers interkey
1) "c"
#获取多个集合之间的并集
127.0.0.1:6379> sunion myset myset2 myset3
1) "b"
2) "c"
3) "d"
4) "e"
5) "a"
#将3个集合中成员的并集存储到unionkey关联的set中,并返回并集成员的数量
127.0.0.1:6379> sunionstore unionkey myset myset2 myset3
(integer) 5
#查看一下sunionstore的操作结果
127.0.0.1:6379> smembers unionkey
1) "b"
2) "c"
3) "d"
4) "e"
5) "a"

Scope of application

  1. You can use the Set data type of Redis to track some unique data, such as the unique IP address information for accessing a certain blog. For this
    scenario, we only need to store the visitor's IP in Redis every time we visit the blog, and the Set data type will automatically ensure the uniqueness
    of the IP address .
  2. Make full use of the convenient and efficient features of the Set type of server aggregation operation, which can be used to maintain the association relationship between data objects. Such as
    to buy an electronic device customer ID is stored in a specified Set in the purchase of another electronic products to customers in another ID is stored
    outside a Set, at this time if we want to get what also purchased With these two products, the intersections command of Set can
    give full play to its advantages of convenience and efficiency.

Hash (hash) type

Hash (hash) type

You can think of the Hash type in Redis as a map container with String Key and String Value. So this type is very suitable for storing
information about value objects . Such as Username, Password and Age, etc. If the Hash contains few fields, then this type of data will also only take up very little
disk space. Each Hash can store 4294967295 key-value pairs.

operating

  • hset/hget/hlen/hexists/hdel/hsetnx
#给键值为myhash的键设置字段为field1,值为itany
127.0.0.1:6379> hset myhash field1 "itany"
(integer) 1
#获取键值为myhash,字段为field1的值
127.0.0.1:6379> hget myhash field1
"itany"
#myhash键中不存在field2字段,因此返回nil
127.0.0.1:6379> hget myhash field2
(nil)
#给myhash关联的Hashes值添加一个新的字段field2,其值为liu
127.0.0.1:6379> hset myhash field2 "liu"
(integer) 1
#获取myhash键的字段数量
127.0.0.1:6379> hlen myhash
(integer) 2
#判断myhash键中是否存在字段名为field1的字段,由于存在,返回值为1
127.0.0.1:6379> hexists myhash field1
(integer) 1
#删除myhash键中字段名为field1的字段,删除成功返回1
127.0.0.1:6379> hdel myhash field1
(integer) 1
#再次删除myhash键中字段名为field1的字段,由于上一条命令已经将其删除,因为没有删除,返回0
127.0.0.1:6379> hdel myhash field1
(integer) 0
#通过hsetnx命令给myhash添加新字段field1,其值为itany,因为该字段已经被删除,所以该命令添
加成功并返回1
127.0.0.1:6379> hsetnx myhash field1 "itany"
(integer) 1
#由于myhash的field1字段已经通过上一条命令添加成功,因为本条命令不做任何操作后返回0
127.0.0.1:6379> hsetnx myhash field1 "itany"
(integer) 0
  • hincrby
127.0.0.1:6379> del myhash
(integer) 1
#准备测试数据
127.0.0.1:6379> hset myhash field 5
(integer) 1
#给myhash的field字段的值加1,返回加后的结果
127.0.0.1:6379> hincrby myhash field 1
(integer) 6
#给myhash的field字段的值加­1,返回加后的结果
127.0.0.1:6379> hincrby myhash field ­1
(integer) 5
#给myhash的field字段的值加­10,返回加后的结果
127.0.0.1:6379> hincrby myhash field ­10
(integer) ­5 
  • hmset / hmget / hgetall / hkeys / hvals
127.0.0.1:6379> del myhash
(integer) 1
#为该键myhash,一次性设置多个字段,分别是field1 = "hello", field2 = "world"
127.0.0.1:6379> hmset myhash field1 "hello" field2 "world"
OK
#获取myhash键的多个字段,其中field3并不存在,因为在返回结果中与该字段对应的值为nil
127.0.0.1:6379> hmget myhash field1 field2 field3
1) "hello"
2) "world"
3) (nil)
#返回myhash键的所有字段及其值,从结果中可以看出,他们是逐对列出的
127.0.0.1:6379> hgetall myhash
1) "field1"
2) "hello"
3) "field2"
4) "world"
#仅获取myhash键中所有字段的名字
127.0.0.1:6379> hkeys myhash
1) "field1"
2) "field2"
#仅获取myhash键中所有字段的值
127.0.0.1:6379> hvals myhash
1) "hello"
2) "world"

Guess you like

Origin blog.csdn.net/weixin_39218464/article/details/113073534