Redis数据类型


redis版本6.2.0

1. 字符串

  • 赋值
    set num 4 #增加一个key=num,value=4的记录
    显示结果:ok
  • 取值
    get num #获取key=num的value
    显示结果:4
  • 删除(delete)
    del num#删除key=num的记录
    显示结果:(integer)1
  • 数值增减(increase/decrease)
    incrby num 5#把num的值加5
    decrby num 1#把num的值减1
  • 扩展命令
    append num 7#在num后附加7(字符串累加,不作计算)

127.0.0.1:6379> set name hello #set
OK
127.0.0.1:6379> APPEND name world#向name后面追加字符串
(integer) 10#返回字符串总长度
127.0.0.1:6379> get name
"helloworld"
127.0.0.1:6379> STRLEN name #name长度
(integer) 10

127.0.0.1:6379> SUBSTR name 1 3#从索引为1的字符开始往后三个字符
"ell"

######自增自减
127.0.0.1:6379> set views 0
OK
127.0.0.1:6379> INCR views #自增1
(integer) 1
127.0.0.1:6379> get views
"1"
127.0.0.1:6379> INCRBY views 10 #自增10
(integer) 11
127.0.0.1:6379> get views
"11"
127.0.0.1:6379> DECR views #自减1
(integer) 10
127.0.0.1:6379> DECRBY views 2 #自减2
(integer) 8

#####截取字符串
127.0.0.1:6379> get name
"helloworld"
127.0.0.1:6379> GETRANGE name 2 4 #获取区间字符串从索引2取4个字符
"llo"
127.0.0.1:6379> GETRANGE name 2 -1  #从索引2到最后字符串
"lloworld"

###替换
127.0.0.1:6379> set key2 abcdefg
OK
127.0.0.1:6379> SETRANGE key2 1 yy #把索引为1的位置换成yy
(integer) 7
127.0.0.1:6379> get key2
"ayydefg"

####SETEX  (set with expire)设置并且设置过期时间
####SETNX  (set if not exists)如果不存在就设置,否则失败
127.0.0.1:6379> SETEX key3 30 hhh
OK
127.0.0.1:6379> ttl key3
(integer) 24
127.0.0.1:6379> SETNX key4 'redis'
(integer) 1
127.0.0.1:6379> SETNX key4 'redis'
(integer) 0
127.0.0.1:6379> SETNX key4 'redis2'
(integer) 0
127.0.0.1:6379> get key4
"redis"
127.0.0.1:6379> ttl key3
(integer) -2
127.0.0.1:6379> get key3
(nil)
####批量设置
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 
OK
127.0.0.1:6379> mget k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> MSETNX k3 v3 k4 v4 #批量设置,原子性操作,有一个失败全失败
(integer) 0
127.0.0.1:6379> MSETNX k4 v4 k5 v5
(integer) 1
#设置一个对象
127.0.0.1:6379> mset user:1:name 'Lee' user:1:age 20
OK
127.0.0.1:6379> set age1 20
OK
127.0.0.1:6379> type age1
string
127.0.0.1:6379> mget user:1:name user:1:age
1) "Lee"
2) "20"

####组合指令
#getset先get后set,返回上一次的值
127.0.0.1:6379> getset db redis
(nil)
127.0.0.1:6379> get db
"redis"
127.0.0.1:6379> getset db mongodb
"redis"
127.0.0.1:6379> get db
"mongodb"

2. 哈希

#新增user.name='lucy',age=18的元素
#As per Redis 4.0.0, HMSET is considered deprecated. Please prefer [HSET]in new code.
#hmset废弃
127.0.0.1:6379> hset user name lucy age 18
(integer) 2
#获取user.name
127.0.0.1:6379> hget user name
"lucy"
#获取user的所有属性
127.0.0.1:6379> hgetall user
1) "name"
2) "lucy"
3) "age"
4) "18"

127.0.0.1:6379> hmget user name
1) "lucy"
127.0.0.1:6379> hmset user birth 2020-01-02 height 168
OK
#此处可以看出hget只能查一个属性
127.0.0.1:6379> hget user name age
(error) ERR wrong number of arguments for 'hget' command
#hmget可以查多个
127.0.0.1:6379> hmget user name age
1) "lucy"
2) "18"
#删除user.height
127.0.0.1:6379> HDEL user height
(integer) 1
#获取user中属性的个数
127.0.0.1:6379> HLEN user
(integer) 3
127.0.0.1:6379> hgetall user
1) "name"
2) "lucy"
3) "age"
4) "18"
5) "birth"
6) "2020-01-02"
#user.birth是否存在
127.0.0.1:6379> HEXISTS user birth
(integer) 1
127.0.0.1:6379> HEXISTS user address
(integer) 0

###自增
127.0.0.1:6379> HINCRBY user age 1
(integer) 19
127.0.0.1:6379> HINCRBY user age -1
(integer) 18
#如果不存在就添加,否则失败
127.0.0.1:6379> HSETNX user address nj
(integer) 1
127.0.0.1:6379> HSETNX user address nj
(integer) 0
  • 赋值
    hmset user username eve0 age 18#key=user,其中用户的username=eve0,age=18
  • 取值
    hget user username取出user.username的值
    显示结果:"eve0"
    hmget user username age获取user.username,user.age
    显示结果:
1) "eve0"
2) "18"

hgetall user获取user的所有属性
显示结果:

1) "username"
2) "eve0"
3) "age"
4) "18"
  • 删除
    hdel user username#删除user的username属性
    del user删除key=user的记录
  • 数值增减
    hincrby user age 2#将user.age的值加2
    hincrby user age -2#将user.age的值减2
  • 扩展命令
    hexists user username #检查user的username属性是否存在(1存在,0不存在)
    hlen user #user的属性个数
    hkeys user #user的所有key
    显示结果为:
1) "username"
2) "age"

hvals user#user的所有value
显示结果为:

1) "eve0"
2) "18"

3. List

## 两端添加
#从左侧向userlist新增俩元素eve0,apple
127.0.0.1:6379> lpush userlist eve0 apple
(integer) 2
#从右侧向userlist新增bana元素
127.0.0.1:6379> rpush userlist bana
(integer) 3
#查找从索引0到索引1
127.0.0.1:6379> lrange userlist 0 1
1) "apple"
2) "eve0"
#查找从第0个到最后一个(-1表示从后往前第几个位置)
127.0.0.1:6379> lrange userlist 0 -1
1) "apple"
2) "eve0"
3) "bana"
#弹出左侧第一个,userlist-size 减1
127.0.0.1:6379> lpop userlist
"apple"
##返回长度
127.0.0.1:6379> LLEN userlist
(integer) 2
127.0.0.1:6379> lpush userlist lalala hhhh
(integer) 4
127.0.0.1:6379> LLEN userlist
(integer) 4
#弹出右侧2个,userlist-size 减2
127.0.0.1:6379> rpop userlist 2
1) "bana"
2) "eve0"

#根据索引获取元素
127.0.0.1:6379> LINDEX userlist 1
"lalala"
127.0.0.1:6379> LINDEX userlist 2
(nil)
127.0.0.1:6379> lindex userlist 0
"hhhh"
##移除元素
#移除list中一个key=one的元素
127.0.0.1:6379> lrem list 1 one
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "three"
3) "two"
#移除list中2个key=three的元素
127.0.0.1:6379> lrem list 2 three
(integer) 2
127.0.0.1:6379> LRANGE list  0 -1
1) "two"
127.0.0.1:6379>
########
#trim 截断操作
127.0.0.1:6379> rpush mylist 'hello' 'hello1' 'hello2' 'hello3'
(integer) 4
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "hello1"
3) "hello2"
4) "hello3"


127.0.0.1:6379> ltrim mylist 1 3#索引1到索引3
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello1"
2) "hello2"
3) "hello3"
#######
#rpoplpush移除最右侧,并push到目标list中
127.0.0.1:6379> rpoplpush mylist mylist2#移动最右侧一个到mylist2中
"hello3"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello1"
2) "hello2"
127.0.0.1:6379> LRANGE mylist2 0 -1
1) "hello3"

######
#lset更新某个下标对应的值
127.0.0.1:6379> lpush mylist hello1 hello2 hello3 hello4
(integer) 4
#更新下标为0的元素为item
127.0.0.1:6379> lset mylist 0 item
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "item"
2) "hello3"
3) "hello2"
4) "hello1"

127.0.0.1:6379> lset mylist 1 item2
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "item"
2) "item2"
3) "hello2"
4) "hello1"
######
#linsert插入
127.0.0.1:6379> lpush mylist hello world
(integer) 2
127.0.0.1:6379> LRANGE mylist
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379> LRANGE mylist 0 -1
1) "world"
2) "hello"
#向hello前插入hahaha
127.0.0.1:6379> LINSERT mylist before hello hahaha
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "world"
2) "hahaha"
3) "hello"
#向hello后面插入lalala
127.0.0.1:6379> LINSERT mylist after hello lalala
(integer) 4
127.0.0.1:6379> LRANGE mylist 0 -1
1) "world"
2) "hahaha"
3) "hello"
4) "lalala"
127.0.0.1:6379>
  • 扩展命令
    lpushx userlist cup#向userlist中从左侧插入cup元素
    rpushx userlist qiaqia#向userlist中从右侧插入qiaqia元素
    lrem mylist 1 a #从前往后删除一个a
    lrem mylist -1 a#从后往前删除一个a
    lrem mylist 0 a#删除列表中所有的a

4. Set

######## 添加元素
#向myset中添加三个元素a b c
127.0.0.1:6379> sadd myset a b c
(integer) 3
#获得集合中的元素
127.0.0.1:6379> SMEMBERS myset
1) "b"
2) "c"
3) "a"
#a是否是myset中的元素
127.0.0.1:6379> SISMEMBER myset a
(integer) 1
127.0.0.1:6379> SISMEMBER myset d
(integer) 0
#查看myset中所有元素的个数
127.0.0.1:6379> scard myset
(integer) 3
#移除myset中b元素
127.0.0.1:6379> SREM myset b
(integer) 1
127.0.0.1:6379> scard myset
(integer) 2
127.0.0.1:6379> SMEMBERS myset
1) "c"
2) "a"

#####
#获取随机元素
127.0.0.1:6379> sadd myset b d e f g h i j k l m n o p
(integer) 14
#获取myset中随机的三个元素
127.0.0.1:6379> SRANDMEMBER myset 3
1) "f"
2) "i"
3) "a"
127.0.0.1:6379> SRANDMEMBER myset 3
1) "b"
2) "m"
3) "d"
127.0.0.1:6379>

#####
#删除指定的key
127.0.0.1:6379> SMEMBERS myset1
1) "a"
127.0.0.1:6379> SREM myset m n
(integer) 2
#随机移除元素
127.0.0.1:6379> spop myset
"i"
127.0.0.1:6379> spop myset
"l"
#随机移除2个元素
127.0.0.1:6379> spop myset 2
1) "h"
2) "d"
##############
##移动myset中的a元素到myset1中
127.0.0.1:6379> SMOVE myset myset1 a
(integer) 1
127.0.0.1:6379> SMEMBERS myset
 1) "b"
 2) "k"
 3) "c"
 4) "j"
 5) "p"
 6) "g"
 7) "m"
 8) "n"
 9) "e"
10) "o"
11) "f"
127.0.0.1:6379> SMEMBERS myset1
1) "a"

########
#
127.0.0.1:6379> sadd myset1 a b c d
(integer) 4
127.0.0.1:6379> sadd myset2 b d e f ;
(integer) 5
//求差集
127.0.0.1:6379> SDIFF myset1 myset2
1) "a"
2) "c"
//求交集
127.0.0.1:6379> SINTER myset1 myset2
1) "b"
2) "d"
//求并集
127.0.0.1:6379> SUNION myset1 myset2
1) "b"
2) "c"
3) ";"
4) "e"
5) "f"
6) "d"
7) "a"

  • 其他操作
    (1) sdiffstore setc seta setb #seta与setb差集存于setc中
    (2) sinterstore setd seta setb #seta与setb交集存于setd中
    (3) sunionstore sete seta setb #seta与setb并集存于sete中

5. Sorted-set

#添加元素
127.0.0.1:6379> zadd salary 5000 xiaohong
(integer) 1
127.0.0.1:6379> zadd salary 2500 zhangsan
(integer) 1
127.0.0.1:6379> zadd salary 8900 lee
(integer) 1
#升序显示
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf
1) "zhangsan"
2) "xiaohong"
3) "lee"
#升序显示从下标0到下标1
127.0.0.1:6379> ZRANGE salary 0 1 withscores
1) "zhangsan"
2) "2500"
3) "xiaohong"
4) "5000"
#升序从下标0到下标1
127.0.0.1:6379> ZRANGE salary 0 2 withscores
1) "zhangsan"
2) "2500"
3) "xiaohong"
4) "5000"
5) "lee"
6) "8900"
#升序显示所有
127.0.0.1:6379> ZRANGE salary 0 -1 withscores
1) "zhangsan"
2) "2500"
3) "xiaohong"
4) "5000"
5) "lee"
6) "8900"
127.0.0.1:6379> ZREVRANGE salary 0 -1
1) "lee"
2) "xiaohong"
3) "zhangsan"
#倒序显示所有
127.0.0.1:6379> ZREVRANGE salary 0 -1 withscores
1) "lee"
2) "8900"
3) "xiaohong"
4) "5000"
5) "zhangsan"
6) "2500"
#工资小于5000的个数
127.0.0.1:6379> ZCOUNT salary -inf 5000
(integer) 2
#小红的工资
127.0.0.1:6379> zscore salary xiaohong
"5000"
#删除
127.0.0.1:6379> zrem salary lee
(integer) 1
#查询个数
127.0.0.1:6379> zcard salary
(integer) 2

  • keys * #查询所有的key
  • keys a*#查询所有以a开头的key
  • del a b#删除key=a,key=b的元素
  • exists seta#seta元素是否存在
  • rename num3 numhello#重命名num3为numhello
  • expire numhello 3000#设置过期时间(单位:秒)
  • ttl numhello#查看key=numhello距离过期时间还剩多少秒
  • type numhello#查找类型:string,hash,list,set,zset
  • select 2 #切换数据库
  • dbsize#查看数据库中的数据数
  • flushdb#清空当前数据库
  • flushall #清空所有数据库
  • move name 1 #移动name到1库

6.Geo地图信息

##添加位置信息 经度 纬度
127.0.0.1:6379> geoadd china:city  116.397128 39.916527 beijing
(integer) 1
127.0.0.1:6379> geoadd china:city  118.8921 31.32751  nanjing
(integer) 1
127.0.0.1:6379> geoadd china:city 121.48941 31.40527 shanghai
(integer) 1
127.0.0.1:6379> geoadd china:city 120.63132 31.30227 suzhou
(integer) 1
#两点距离
127.0.0.1:6379> GEODIST china:city nanjing shanghai km
"246.8158"
#移除元素
127.0.0.1:6379> zrem china:city beijing
(integer) 1
127.0.0.1:6379> geoadd china:city  116.397128 39.916527 beijing
(integer) 1
#查询某个点为圆心,半径100km的城市
127.0.0.1:6379> GEORADIUS china:city 119.59794 31.72322 100 km withcoord withdist
1) 1) "nanjing"
   2) "80.0971"
   3) 1) "118.89209836721420288"
      2) "31.32750976275760735"

127.0.0.1:6379> GEORADIUS china:city 119.59794 31.72322 300 km withcoord withdist
1) 1) "nanjing"
   2) "80.0971"
   3) 1) "118.89209836721420288"
      2) "31.32750976275760735"
2) 1) "suzhou"
   2) "108.5987"
   3) 1) "120.63131779432296753"
      2) "31.3022690094529068"
3) 1) "shanghai"
   2) "182.7086"
   3) 1) "121.48941010236740112"
      2) "31.40526993848380499"

7.HyperLoglog基数统计

基数就是统计一个数据集中不重复的元素个数。

#添加数据集key1
127.0.0.1:6379> PFADD key1 a b c d f g h i
(integer) 1
#添加数据及key2
127.0.0.1:6379> pfadd key2 c d f k
(integer) 1
#合并key1和key2到key3
127.0.0.1:6379> PFMERGE key3 key1 key2
OK
#key3的总个数
127.0.0.1:6379> PFCOUNT key3
(integer) 9

8.Bitmap

使用场景:统计一个月的签到信息、用户活跃度

#设置第一位为1
127.0.0.1:6379> SETBIT sign 0 1
(integer) 0
127.0.0.1:6379> SETBIT sign 1 0
(integer) 0
127.0.0.1:6379> SETBIT sign 2 1
(integer) 0
127.0.0.1:6379> SETBIT sign 3 0
(integer) 0
127.0.0.1:6379> SETBIT sign 4 1
(integer) 0
127.0.0.1:6379> SETBIT sign 5 1
(integer) 0
127.0.0.1:6379> SETBIT sign 6 1
(integer) 0
127.0.0.1:6379> SETBIT sign 7 1
(integer) 0
127.0.0.1:6379> SETBIT sign 8 0
(integer) 0
127.0.0.1:6379> SETBIT sign 9 1
(integer) 0
127.0.0.1:6379> SETBIT sign 10 1
(integer) 0
127.0.0.1:6379> SETBIT sign 11 1
(integer) 0
#统计所有为1的个数
127.0.0.1:6379> BITCOUNT sign
(integer) 9

#以上添加的数据为
#0000 1110 1111 0101
#统计从第0个字节到第0个字节的1的个数
127.0.0.1:6379> BITCOUNT sign 0 0
(integer) 6
#统计从第0个字节到第一个字节的1的个数
127.0.0.1:6379> BITCOUNT sign 0 1
(integer) 9

注意:BITCOUNT统计单位为字节,SETBIT单位是一个bit

推荐阅读更多精彩内容