Redis共有五大基本类型和三大特殊类型。
但是Redis本质上是NoSQL的键值对类型数据库,所以不管什么类型,本质上仍然基于键值对来存储的,只是各种数据类型的一些函数方法不同。
# key * 查看所有键值
127.0.0.1:6379> keys *
1) "age"
2) "name"
3) "addr"
# exists key 判断key是否存在
127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> exists name age
(integer) 2
# move key db 移动key至指定db
127.0.0.1:6379> move addr 1
(integer) 1
127.0.0.1:6379> keys *
1) "age"
2) "name"
# expire key second 设置过期时间
127.0.0.1:6379> expire name 20
(integer) 1
# ttl key 查看剩余时间
127.0.0.1:6379> ttl name
(integer) 17
127.0.0.1:6379> ttl name
(integer) -2
# set key value 设置值
# get key 获取值
127.0.0.1:6379> set name zhangsan
OK
127.0.0.1:6379> get name
"zhangsan"
127.0.0.1:6379> keys *
1) "age"
2) "name"
# mset key value [key value ...] 批量设置
# mget key [key...] 批量获取
127.0.0.1:6379> mset sex S addr yunnan
OK
127.0.0.1:6379> mget name age hello
1) "zhangsanniub"
2) "12"
3) "hello world!"
# setex key seconds value (set and expire) 设置值并设置时间
# setnx key value (set if no exists) 设置值当不存在时
127.0.0.1:6379> setex timeout 12 guoqi
OK
127.0.0.1:6379> ttl timeout #多久过期
(integer) 3
127.0.0.1:6379> ttl timeout
(integer) -2
127.0.0.1:6379> setnx name lisi #存在,设置失败
(integer) 0
# msetnx 批量设置值时判断是否存在
127.0.0.1:6379> msetnx k1 v1 k2 v2
(integer) 1
127.0.0.1:6379> msetnx name lisi habbit tiaowu #原子性操作,一个存在全部失败
(integer) 0
# getset key value #先过去后设置,不存在时直接set,存在时修改
127.0.0.1:6379> getset name lisi
"zhangsanniub"
127.0.0.1:6379> get name
"lisi"
# incr key # 自增一
# decr key # 自减一
# incrby key increment # 自增指定数
# decrby key decrement # 自减指定数
127.0.0.1:6379> incr number
(integer) 2
127.0.0.1:6379> decr number
(integer) 1
127.0.0.1:6379> decrby number 2
(integer) -1
127.0.0.1:6379> incrby number 5
(integer) 4
# diy对象
127.0.0.1:6379> mset user:1:name lisi user:1:age 12
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "lisi"
2) "12"
# strlen key # 判断字符长度
127.0.0.1:6379> strlen name
(integer) 4
# exists key # 判断字符是否存在
127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> exists name age
(integer) 2
127.0.0.1:6379> exists null
(integer) 0
# append key value # 在字符后面追加值
127.0.0.1:6379> append name 123
(integer) 7
127.0.0.1:6379> get name
"lisi123"
127.0.0.1:6379> append name1 wang # 若不存在,直接创建
(integer) 4
127.0.0.1:6379> get name1
"wang"
# setrange key offset value # 在指定下标出开始替换
127.0.0.1:6379> get string
"abcde"
127.0.0.1:6379> setrange string 0 123
(integer) 5
127.0.0.1:6379> get string
"123de"
# getrange key start end
127.0.0.1:6379> getrange string 0 2
"123"
应用场景
Redis列表是简单的字符串列表,按照插入顺序排序,我们可以添加一个元素到列表的头部(左边)或者尾部(右边)。
它的底层实际是个链表 !
# lpush key value [value...] 从左边(头部)插入元素
# rpush key value [value...] 从右边(尾部)插入元素
# lrange key start end 获取指定下标值
127.0.0.1:6379> lpush list 1 2 3 4 # 从头部依次插入
(integer) 4
127.0.0.1:6379> rpush list a b c d # 从尾部依次插入
(integer) 8
127.0.0.1:6379> lrange list 0 -1 # 获取全部
1) "4"
2) "3"
3) "2"
4) "1"
5) "a"
6) "b"
7) "c"
8) "d"
# lpop key # 从左边(头部)弹出值
# rpop key # 从右边(尾部)弹出值
127.0.0.1:6379> lrange list 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
5) "a"
6) "b"
7) "c"
8) "d"
127.0.0.1:6379> lpop list
"4"
127.0.0.1:6379> rpop list
"d"
127.0.0.1:6379> lrange list 0 -1
1) "3"
2) "2"
3) "1"
4) "a"
5) "b"
6) "c"
# lset key index value #更新指定下标的值
127.0.0.1:6379> lrange list 0 -1
1) "a"
2) "b"
127.0.0.1:6379> lset list 0 s
OK
127.0.0.1:6379> lrange list 0 -1
1) "s"
2) "b"
# lindex key index # 获取指定下标的值
127.0.0.1:6379> lrange list 0 -1
1) "3"
2) "2"
3) "1"
4) "a"
5) "b"
6) "c"
127.0.0.1:6379> lindex list 0
"3"
127.0.0.1:6379> lindex list -1
"c"
127.0.0.1:6379> lindex list 1
"2"
# linsert key BEFORE|AFTER pivot value # 在指定值前面或者后面添加某值
127.0.0.1:6379> lrange list 0 -1
1) "s"
2) "b"
127.0.0.1:6379> linsert list after b isyou
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "s"
2) "b"
3) "isyou"
# llen key # 返回列表长度
127.0.0.1:6379> lrange list 0 -1
1) "3"
2) "2"
3) "1"
4) "a"
5) "b"
6) "c"
127.0.0.1:6379> llen list
(integer) 6
# lrem key count value # 删除指定个数的指定值
127.0.0.1:6379> lrange list 0 -1 # 共有3个2
1) "2"
2) "2"
3) "2"
4) "1"
5) "a"
6) "b"
7) "c"
127.0.0.1:6379> lrem list 1 2 # 删除1个2,后面两个不影响
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "2"
2) "2"
3) "1"
4) "a"
5) "b"
6) "c"
127.0.0.1:6379> lrem list 2 2 # 删除2个2
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1) "1"
2) "a"
3) "b"
4) "c"
# ltrim key start end # 指定范围裁剪
127.0.0.1:6379> lrange list 0 -1
1) "1"
2) "a"
3) "b"
4) "c"
127.0.0.1:6379> ltrim list 1 3 # 裁剪1到3
OK
127.0.0.1:6379> lrange list 0 -1
1) "a"
2) "b"
3) "c"
# rpoplpush source destination # 移除指定数组最后一位到目标数组第一位
127.0.0.1:6379> lrange list 0 -1
1) "a"
2) "b"
3) "c"
127.0.0.1:6379> rpoplpush list list1 #
"c"
127.0.0.1:6379> lrange list 0 -1
1) "a"
2) "b"
127.0.0.1:6379> lrange list1 0 -1
1) "c"
链表的操作无论是头和尾效率都极高,对中间部分操作效率低。
# sadd key member [member...] # 随机位置插入一个或者多个值
127.0.0.1:6379> sadd set a b c d
(integer) 4
# smembers key # 获取集合内的全部元素
127.0.0.1:6379> smembers set
1) "d"
2) "c"
3) "b"
4) "a"
# scard key # 获取集合内元素个数
127.0.0.1:6379> scard set
(integer) 4
# srem key member[member...] # 移除集合内的指定元素
127.0.0.1:6379> srem set c d
(integer) 2
127.0.0.1:6379> scard set
(integer) 2
127.0.0.1:6379> smembers set
1) "b"
2) "a"
# srandmember key count # 随机返回集合内的指定个数元素
127.0.0.1:6379> srandmember set
"a"
127.0.0.1:6379> srandmember set 2
1) "b"
2) "a"
# spop key count # 随机弹出集合内指定个数的元素
127.0.0.1:6379> spop set 1
1) "a"
127.0.0.1:6379> smembers set
1) "b"
# smove source destination member # 移动指定元素到指定集合
127.0.0.1:6379> smembers set
1) "0"
2) "b"
3) "1"
4) "a"
5) "2"
127.0.0.1:6379> smove set set1 0
(integer) 1
127.0.0.1:6379> smove set set1 1
(integer) 1
127.0.0.1:6379> smove set set1 2
(integer) 1
127.0.0.1:6379> smembers set1
1) "0"
2) "1"
3) "2"
127.0.0.1:6379> smembers set
1) "b"
2) "a"
# sdiff key [key...] # 查看差集
# sinter key [key...] # 查看交集
# sunion key [key...] # 查看并集
127.0.0.1:6379> smembers set
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> smembers set1
1) "d"
2) "c"
3) "b"
127.0.0.1:6379> sdiff set set1 # 差集
1) "a"
127.0.0.1:6379> sinter set set1 # 交集
1) "c"
2) "b"
127.0.0.1:6379> sunion set set1 # 并集
1) "b"
2) "c"
3) "a"
4) "d"
应用:同城好友?
# hset key filed value # 设置值
# hget key filed value # 获取值
127.0.0.1:6379> hset user name zhangsan
(integer) 1
127.0.0.1:6379> hset user age 12
(integer) 1
127.0.0.1:6379> hget user age
"12"
127.0.0.1:6379> hget user name
"zhangsan"
# hmset key filed value [key value...] # 批量设置值
# hmget key filed value [key value...] # 批量获取值
127.0.0.1:6379> hmset user sex m addr jiangxi
OK
127.0.0.1:6379> hmget user sex addr
1) "m"
2) "jiangxi"
# hgetall key # 获取全部信息
# hkeys key # 获取全部域(filed)
# hvals key # 获取全部值(value)
# hdel # 删除指定值
127.0.0.1:6379> hgetall user
1) "name"
2) "zhangsan"
3) "age"
4) "12"
5) "sex"
6) "m"
7) "addr"
8) "jiangxi"
127.0.0.1:6379> hdel user addr
(integer) 1
127.0.0.1:6379> hgetall user
1) "name"
2) "zhangsan"
3) "age"
4) "12"
5) "sex"
6) "m"
127.0.0.1:6379> hkeys user
1) "name"
2) "age"
3) "sex"
127.0.0.1:6379> hkeys user
1) "name"
2) "age"
3) "sex"
127.0.0.1:6379> hvals user
1) "zhangsan"
2) "12"
3) "m"
# hsetnx key flied value # 当键值不存在时设置
127.0.0.1:6379> hsetnx user name lisi
(integer) 0
# hincrby key filed increment # 增长指定值
127.0.0.1:6379> hincrby user age 5
(integer) 17
# zset key scores member # 添加
# zrange key start stop [withscores] # 指定范围内(下标)查看(升序)
# zrevrange key start stop [withscores] # 指定范围内(下标)查看(降序)
127.0.0.1:6379> zadd salary 1000 zhangsan
(integer) 1
127.0.0.1:6379> zadd salary 2000 lisi
(integer) 1
127.0.0.1:6379> zadd salary 1500 wangwu
(integer) 1
127.0.0.1:6379> zrange salary 0 -1
1) "zhangsan"
2) "wangwu"
3) "lisi"
# zrem key member [member...] # 移除指定元素
127.0.0.1:6379> zrem salary wangwu lisi
(integer) 2
127.0.0.1:6379> zrange salary 0 -1
1) "zhangsan"
# zcard salary # 统计集合内元素个数
127.0.0.1:6379> zrange salary 0 -1
1) "zhangsan"
127.0.0.1:6379> zcard salary
(integer) 1
# zcunt key min max # 统计某一区间的元素
127.0.0.1:6379> zrange salary 0 -1 withscores
1) "zhangsan"
2) "1000"
3) "wangwu"
4) "1230"
5) "lisi"
6) "2000"
127.0.0.1:6379> zcount salary 1000 3000
(integer) 3
# zrank key member # 返回某一元素在集合内的排名,(升序)
# zrank key member # 返回某一元素在集合内的排名,(降序)
127.0.0.1:6379> zrange salary 0 -1 withscores
1) "zhangsan"
2) "1000"
3) "wangwu"
4) "1230"
5) "lisi"
6) "2000"
127.0.0.1:6379> zrank salary lisi
(integer) 2
# zrangebyscore key min max [withscores] # 指定区间排序(升序)
127.0.0.1:6379> zrangebyscore salary -inf +inf withscores
1) "zhangsan"
2) "1000"
3) "wangwu"
4) "1230"
5) "lisi"
6) "2000"
除了五大基本类型外,redis还提供了三大特殊类型,方便大家处理更加复杂的数据存储业务。
# geoadd key longitude latitude member [longitude latitude member] # 添加
127.0.0.1:6379> geoadd china 120.21 30.20 hangzhou 121.48 31.40 shanghai
(integer) 2
127.0.0.1:6379> geoadd china 116.23 40.22 beijing 113.88 22.55 shenzheng
(integer) 2
# geopos key member # 获取
127.0.0.1:6379> geopos china hangzhou
1) "120.21000176668167114"
2) "30.19999988833350102"
# geodist key member1 member2 [unit] # 获取两地距离
127.0.0.1:6379> geodist china hangzhou shanghai km
"180.3758"
# georadius key longitude latitude radius m|km|ft|mi [withcoord][withdist] [withhash][asc|desc][count number] # 以给定的经纬度为中心,找出某一半径内的元素
- withcoord # 返回位置名称和经纬度
- withdist # 返回距离
- withhash # 返回位置hash值
- asc # 升序
- desc # 降序
- count # 限定寻找个数
127.0.0.1:6379> georadius china 120 30 1000 km
1) "hangzhou"
2) "shanghai"
127.0.0.1:6379> georadius china 120 30 1000 km withcoord withdist withhash asc count 2
1) 1) "hangzhou"
2) "30.0535"
3) (integer) 4054122592018769
4) 1) "120.21000176668167114"
2) "30.19999988833350102"
2) 1) "shanghai"
2) "210.4288"
3) (integer) 4054807767553238
4) 1) "121.48000091314315796"
2) "31.40000025319353938"
# georadiusbymember key member radius m|km|ft|mi [withcoord][withdist] [withhash][asc|desc][count number] # 找出位于指定范围内的元素,中心点是由给定的位置元素决定
127.0.0.1:6379> georadiusbymember china hangzhou 1000 km withdist
1) 1) "hangzhou"
2) "0.0000"
2) 1) "shanghai"
2) "180.3758"
# geohash key member [member...] # Redis使用geohash将二维经纬度转换为一维字符串,字符串越长表示位置更精确,两个字符串越相似 表示距离越近。
127.0.0.1:6379> geohash china hangzhou shanghai
1) "wtm7z3wrb00"
2) "wtw6sk5n300"
-----------------------zset-----------------------------
# geo的底层是zset,所以使用zset的一些方法可以对其进行删除排序等操作
127.0.0.1:6379> zrange china 0 -1
1) "shenzheng"
2) "hangzhou"
3) "shanghai"
4) "beijing"
127.0.0.1:6379> zrem china hangzhou
(integer) 1
127.0.0.1:6379> zrange china 0 -1
1) "shenzheng"
2) "shanghai"
3) "beijing"
基数集栗子
比如数据集{1,2,5,2,4}的基数集为{1,2,4,5},它是类似set的一种结构,但存储效率极高。
使用方法比较单调,只提供基础的计数功能。
# pfadd key element [element...] # 添加元素
# pfcount key # 统计元素个数
# pfmerge key destkey sourcekey [sourcekey...] # 合并key
127.0.0.1:6379> pfadd pf a b c d e f g g g a b c
(integer) 1
127.0.0.1:6379> pfcount pf
(integer) 7
127.0.0.1:6379> pfadd pf2 h i j j j
(integer) 1
127.0.0.1:6379> pfmerge pf pf2
OK
127.0.0.1:6379> pfcount pf
(integer) 10
127.0.0.1:6379> pfcount pf1
(integer) 0
# setbit key offset value # 设置值
# getbit key offset # 获取值
# bitcount key [start end] # 统计一定区间的数量(无意义,值一定为0,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> getbit sign 0
(integer) 1
127.0.0.1:6379> bitcount sign
(integer) 5