Redis五大基本数据类型

Posted alexephor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis五大基本数据类型相关的知识,希望对你有一定的参考价值。

Redis数据类型

  Redis支持五种数据类型:String(字符串),Hash(哈希),List(列表),Set(集合)及Zset(sorted set:有序集合)。

五种数据类型使用场景

技术图片

连接redis-server

1 $redis-cli
2 redis 127.0.0.1:6379>
3 redis 127.0.0.1:6379> PING
4 
5 $ redis-cli -h host -p port -a password

Redis的key 

  常规操作(更多操作https://www.runoob.com/redis/redis-keys.html

1 keys *         # 列出所有key
2 exists key  # 判断某个key是否存在
3 move key db # 当前库就没有了,被移除其它的库了
4 expire key  # 秒钟:为给定的key设置过期时间 当然还有毫秒等等
5 ttl key        # 查看还有多久过期 -1表示永不过期 -2表示已经过期
6 type key    # 查看key类型

  在终端演示

[root@localhost ~]# redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> set k2 v2
OK
127.0.0.1:6379> set k3 v3
OK
127.0.0.1:6379> exists k1
(integer) 1
127.0.0.1:6379> move k1 2
(integer) 1
127.0.0.1:6379> keys *
1) "k3"
2) "k2"
127.0.0.1:6379> ttl key
(integer) -2
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> get k1
"v1"
127.0.0.1:6379[2]> select 0
OK
127.0.0.1:6379> keys *
1) "k3"
2) "k2"
127.0.0.1:6379> ttl k1
(integer) -2
127.0.0.1:6379> ttl k2
(integer) -1
127.0.0.1:6379> expire k2 10
(integer) 1
127.0.0.1:6379> ttl k2
(integer) 3
127.0.0.1:6379> get k2
(nil)
127.0.0.1:6379> keys *
1) "k3"
127.0.0.1:6379> type k3
string

Redis的String  

   常规命令(更多操作https://www.runoob.com/redis/redis-strings.html

1 set/get/del/append/strlen
2 Incr/decr/incrby/decrby #一定要是数字才能进行加减
3 getrange/setrange
4 setex(set with expire)键秒值/setnx(set if not exist)
5 mset/mget/msetnx
6 getset(先get再set)

  终端操作

127.0.0.1:6379> set k3 woshiniba
OK
127.0.0.1:6379> get k3
"woshiniba"
127.0.0.1:6379> append k3 12345
(integer) 14
127.0.0.1:6379> get k3
"woshiniba12345"
127.0.0.1:6379> strlen k3
(integer) 14
127.0.0.1:6379> set k2 2
OK
127.0.0.1:6379> incr k2
(integer) 3
127.0.0.1:6379> incr k2
(integer) 4
127.0.0.1:6379> incr k2
(integer) 5
127.0.0.1:6379> decr k2
(integer) 4
127.0.0.1:6379> decr k2
(integer) 3
127.0.0.1:6379> get k2
"3"
127.0.0.1:6379> incrby k2 3
(integer) 6
127.0.0.1:6379> incrby k2 3
(integer) 9
127.0.0.1:6379> decrby k2 3
(integer) 6
127.0.0.1:6379> decrby k2 3
(integer) 3
127.0.0.1:6379> incr k3
(error) ERR value is not an integer or out of range
127.0.0.1:6379> getrange k3 0 -1
"woshiniba12345"
127.0.0.1:6379> getrange k3 0 -2
"woshiniba1234"
127.0.0.1:6379> getrange k3 0 8
"woshiniba"
127.0.0.1:6379> setrange k3 0 xxxx
(integer) 14
127.0.0.1:6379> get k3
"xxxxiniba12345"
127.0.0.1:6379> setex k4 10 v4
OK
127.0.0.1:6379> ttl k4
(integer) 4
127.0.0.1:6379> setnx k3 v11
(integer) 0
127.0.0.1:6379> get k3
"xxxxiniba12345"
127.0.0.1:6379> setnx k4 v4
(integer) 1
127.0.0.1:6379> get k4
"v4"
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> get k4
"v4"
127.0.0.1:6379> msetnx k3 v3 k4 v4 k5 v5
(integer) 0
127.0.0.1:6379> get k5
(nil)
127.0.0.1:6379> keys *
1) "k2"
2) "k4"
3) "k1"
4) "k3"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> keys *
1) "k1"
127.0.0.1:6379[2]> del k1
(integer) 1
127.0.0.1:6379[2]> select 0
OK

Redis的List  

  常规操作(更多操作https://www.runoob.com/redis/redis-lists.html

1 lpush/rpush/lrange
2 lpop/rpop
3 lindex #按照索引下标获得元素
4 llen
5 lrem key #删n个value
6 ltrim key #开始index结束index 截取指定范围的值后再赋值给key
7 rpoplpush #原列表 目的列表
8 lset key index value
9 linset key before/after 值1 值2 

   终端操作

127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> LPUSH list01 1 2 3 4 5
(integer) 5
127.0.0.1:6379> LRANGE list01 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> RPUSH list02 1 2 3 4 5
(integer) 5
127.0.0.1:6379> LRANGE list02 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> LPOP list01
"5"
127.0.0.1:6379> LPOP list02
"1"
127.0.0.1:6379> RPOP list01
"1"
127.0.0.1:6379> RPOP list02
"5"
127.0.0.1:6379> lrange list01 0 -1
1) "4"
2) "3"
3) "2"
127.0.0.1:6379> LINDEX list01 0
"4"
127.0.0.1:6379> LINDEX list02 0
"2"
127.0.0.1:6379> LLEN list01
(integer) 3
127.0.0.1:6379> RPUSH list03 1 1 1 2 2 2 3 3 3 4 5 6
(integer) 12
127.0.0.1:6379> LREM list03 2 2
(integer) 2
127.0.0.1:6379> lrange list03 0 -1
 1) "1"
 2) "1"
 3) "1"
 4) "2"
 5) "3"
 6) "3"
 7) "3"
 8) "4"
 9) "5"
10) "6"
127.0.0.1:6379> lrange list01 0 -1
1) "4"
2) "3"
3) "2"
127.0.0.1:6379> lset list01 1 x
OK
127.0.0.1:6379> LRANGE list01 0 -1
1) "4"
2) "x"
3) "2"
127.0.0.1:6379> LINSERT list01 before x Python
(integer) 4
127.0.0.1:6379> LRANGE list01 0 -1
1) "4"
2) "Python"
3) "x"
4) "2"
127.0.0.1:6379> LINSERT list01 after x C++
(integer) 5
127.0.0.1:6379> LRANGE list01 0 -1
1) "4"
2) "Python"
3) "x"
4) "C++"
5) "2"

小结:

  left right都可以插入添加

  如果键不存在,创建新的链表

  若果键存在,新增内容

  若果值全移除,对应的键就消失了

  链表的操作无论是头和尾效率极高,但是假如是对中间元素进行操作,效率就很惨淡了    

Redis的Set  

  常规操作(更多操作https://www.runoob.com/redis/redis-sets.html)   

1 sadd/smembers/sismember
2 scard #获取集合里边的元素个数
3 srem key value #删除集合中元素
4 srandmember key #某个整数(随机出几个数)
5 spop key #随机出栈
6 smove key1 key2 在key1里某个值将key1里的某个值赋给key2
7 集合相关 差:sdiff
8         交:sinter
9         并:sunion 

   终端操作

127.0.0.1:6379> sadd set01 1 1 2 2 3 3
(integer) 3
127.0.0.1:6379> SMEMBERS set01
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> SISMEMBER set01 1
(integer) 1
127.0.0.1:6379> SCARD set01
(integer) 3
127.0.0.1:6379> SREM set01 1
(integer) 1
127.0.0.1:6379> SMEMBERS set01
1) "2"
2) "3"
127.0.0.1:6379> SADD set01 1 2 3 4 5 6 7 8 9
(integer) 7
127.0.0.1:6379> SRANDMEMBER set01 3
1) "9"
2) "2"
3) "1"
127.0.0.1:6379> SRANDMEMBER set01 3
1) "8"
2) "3"
3) "5"
127.0.0.1:6379> SRANDMEMBER set01 3
1) "8"
2) "7"
3) "3"
127.0.0.1:6379> SMEMBERS set01
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
127.0.0.1:6379> SPOP set01
"3"
127.0.0.1:6379> SMEMBERS set01
1) "1"
2) "2"
3) "4"
4) "5"
5) "6"
6) "7"
7) "8"
8) "9"
127.0.0.1:6379> sadd set01 x y z
(integer) 3
127.0.0.1:6379> sadd set02 x y z
(integer) 3
127.0.0.1:6379> SMOVE set01 set02 5
(integer) 1
127.0.0.1:6379> SMEMBERS set02
1) "y"
2) "5"
3) "x"
4) "z"
127.0.0.1:6379> del set01
(integer) 1
127.0.0.1:6379> del set02
(integer) 1
127.0.0.1:6379> keys * 
1) "list01"
2) "list03"
3) "list02"
127.0.0.1:6379> sadd set01 1 2 3 4 5 
(integer) 5
127.0.0.1:6379> sadd set02 1 2 3 a b
(integer) 5
127.0.0.1:6379> sdiff set01 set02
1) "4"
2) "5"
127.0.0.1:6379> SINTER set01 set02
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> SUNION set01 set02
1) "5"
2) "3"
3) "2"
4) "b"
5) "a"
6) "4"
7) "1"

Redis的Hash  

   常规操作(更多操作https://www.runoob.com/redis/redis-hashes.html  

1 hset/hget/hmset/hmget/hgetall/hdel
2 hlen
3 hexists key 在key里边的某个值的key
4 hkeys/hvals
5 hincrby/hincrbyfloat
6 hsetnx

   终端操作

127.0.0.1:6379> hset user id 11
(integer) 1
127.0.0.1:6379> hget user id
"11"
127.0.0.1:6379> hset user name z3
(integer) 1
127.0.0.1:6379> hget user name
"z3"
127.0.0.1:6379> hmset customer id 11 name li4 age 26
OK
127.0.0.1:6379> hmget customer id name age
1) "11"
2) "li4"
3) "26"
127.0.0.1:6379> HGETALL customer
1) "id"
2) "11"
3) "name"
4) "li4"
5) "age"
6) "26"
127.0.0.1:6379> hdel user name 
(integer) 1
127.0.0.1:6379> hget user id
"11"
127.0.0.1:6379> hlen customer
(integer) 3
127.0.0.1:6379> hexists customer id
(integer) 1
127.0.0.1:6379> hkeys customer
1) "id"
2) "name"
3) "age"
127.0.0.1:6379> hvals customer
1) "11"
2) "li4"
3) "26"
127.0.0.1:6379> HINCRBY customer age 2
(integer) 28
127.0.0.1:6379> HINCRBY customer age 2
(integer) 30
127.0.0.1:6379> HINCRBY customer age 2
(integer) 32
127.0.0.1:6379> hset customer score 91.5
(integer) 1
127.0.0.1:6379> HINCRBYFLOAT customer score 4
"95.5"
127.0.0.1:6379> HSETNX customer age 26
(integer) 0
127.0.0.1:6379> HSETNX customer email abc@126.com
(integer) 1

Redis的Zset  

  常规操作(更多操作https://www.runoob.com/redis/redis-sorted-sets.html) 

1 在set基础上加一个scroe的值之前k1 v1 v2 v3,现在zset k1 score v1 score2 v2
2 zadd/zrange
3 zrangebyscore key 开始score 结束score
4 zrem key 某score下对应的vaule值 作用是删除元素
5 zcard/zcount key score区间/zrank key values值 作用是获得下标值/zscore key 对应值获得分
6 zrevrank key values值 作用是逆序获得下标值
7 zrevrange
8 zrevrangebyscore key 结束score 开始score
9 zrangebyscore key 开始score 结束score withscores ( 不包含 Limit作用是返回限制 limit开始下标步 多少步 

   终端操作

127.0.0.1:6379> zadd zset01 60 v1 70 v2 80 v3 90 v4 100 v5
(integer) 5
127.0.0.1:6379> zrange zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
5) "v5"
127.0.0.1:6379> zrange zset01 0 -1 withscores
 1) "v1"
 2) "60"
 3) "v2"
 4) "70"
 5) "v3"
 6) "80"
 7) "v4"
 8) "90"
 9) "v5"
10) "100"
127.0.0.1:6379> ZRANGEBYSCORE zset01 60 90 
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379> ZRANGEBYSCORE zset01 60 (90 
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> ZRANGEBYSCORE zset01 (60 (90 
1) "v2"
2) "v3"
127.0.0.1:6379> ZRANGEBYSCORE zset01 60 90 limit 2 2
1) "v3"
2) "v4"
127.0.0.1:6379> zrem zset01 v5
(integer) 1
127.0.0.1:6379> zrange zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379> ZRANGE zset01 0 -1 withscores
1) "v1"
2) "60"
3) "v2"
4) "70"
5) "v3"
6) "80"
7) "v4"
8) "90"
127.0.0.1:6379> zcard zset01
(integer) 4
127.0.0.1:6379> ZCOUNT zset01 60 80
(integer) 3
127.0.0.1:6379> ZRANK zset01 v4
(integer) 3
127.0.0.1:6379> ZSCORE zset01 v4
"90"
127.0.0.1:6379> ZREVRANK zset01 v4
(integer) 0
127.0.0.1:6379> ZREVRANGE zset01 0 -1
1) "v4"
2) "v3"
3) "v2"
4) "v1"
127.0.0.1:6379> ZREVRANGEBYSCORE zset01 90 60 withscores limit 2 2
1) "v2"
2) "70"
3) "v1"
4) "60"
127.0.0.1:6379> ZREVRANGEBYSCORE zset01 90 60 withscores limit 0 2
1) "v4"
2) "90"
3) "v3"
4) "80"
127.0.0.1:6379> 

  

 

以上是关于Redis五大基本数据类型的主要内容,如果未能解决你的问题,请参考以下文章

Redis学习--- 安装基本命令及常见五大数据类型详解

redis五大数据类型

redis 五大数据类型

redis学习之——五大基本数据类型

redis五大数据类型

Redis学习二(常用五大数据类型)