类型 | 实现原理 |
---|---|
string | String |
hash | HashMap |
list | LinkedList |
set | HashSet |
sorted_set | TreeSet |
存储的数据:单个数据,最简单的数据存储类型,也是最常用的数据存储类型
存储数据的格式:一个存储空间保存一个数据
存储内容:通常使用字符串,如果字符串以整数的形式展示,可以作为数字操作使用
代码实例
string 类型数据的基本操作
set username zhangsan
127.0.0.1:6379> get username
"zhangsan"
127.0.0.1:6379> del username
(integer) 1
mset name xiaoming sex nan address hn
mget name sex address
1) "xiaoming"
2) "nan"
3) "hn"
127.0.0.1:6379> strlen name
(integer) 8
27.0.0.1:6379> append name dd
(integer) 10
127.0.0.1:6379> get name
"xiaomingdd"
string 类型数据的扩展操作
业务场景1
大型企业级应用中,分表操作是基本操作,使用多张表存储同类型数据,但是对应的主键 id 必须保证统一性 ,不能重复。Oracle 数据库具有
sequence 设定,可以解决该问题,但是 MySQL数据库并不具有类似的机 制,那么如何解决
解决方案
incr key
incrby key increment
incrbyfloat key increment
实例
127.0.0.1:6379> incr seq_id1
(integer) 1
127.0.0.1:6379> incr seq_id1
(integer) 2
127.0.0.1:6379> incrby seq_id1 2
(integer) 4
127.0.0.1:6379> incr seq_id1
(integer) 5
127.0.0.1:6379> incrbyfloat seq_id1 0.5
"5.5"
decr key
decrby key increment
实例
127.0.0.1:6379> decr seq_id2
(integer) -1
127.0.0.1:6379> decrby seq_id2 2
(integer) -3
string 作为数值操作
Tips 1:
业务场景2
“最强女生”启动海选投票,只能通过微信投票,每个微信号每 4 小时只能投1票。
电商商家开启热门商品推荐,热门商品不能一直处于热门期,每种商品热门期维持3天,3天后自动取消热门。
新闻网站会出现热点新闻,热点新闻最大的特征是时效性,如何自动控制热点新闻的时效性
解决方案
秒和毫秒
setex key seconds value
psetex key milliseconds value
实例
127.0.0.1:6379> setex name 5 xiaohong
OK
127.0.0.1:6379> get name
"xiaohong"
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> psetex sex 5000 nan
OK
127.0.0.1:6379> get sex
"nan"
127.0.0.1:6379> get sex
(nil)
Tips 2:
业务场景3
主页高频访问信息显示控制,例如新浪微博大V主页显示粉丝数与微博数量
解决方案
业务场景解决方案点这个
存储的困惑
对象类数据的存储如果具有较频繁的更新需求操作会显得笨重
hash存储结构优化
hash 类型数据的基本操作
hset key field value
127.0.0.1:6379> hset student id 1
(integer) 1
127.0.0.1:6379> hset student name zhangsan
(integer) 1
hget key field
hgetall key
127.0.0.1:6379> hget student id
"1"
127.0.0.1:6379> hgetall student
1) "id"
2) "1"
3) "name"
4) "zhangsan"
hdel key field1 [field2]
127.0.0.1:6379> hdel student id name
(integer) 2
hmset key field1 value1 field2 value2 …
127.0.0.1:6379> hmset student id 1 name lisi address beijing
OK
hmget key field1 field2 …
127.0.0.1:6379> hmget student id name address
1) "1"
2) "lisi"
3) "beijing"
hlen key
127.0.0.1:6379> hlen student
(integer) 3
hexists key field
127.0.0.1:6379> hexists student id
(integer) 1
127.0.0.1:6379> hexists student sex
(integer) 0
hash 类型数据扩展操作
hkeys key
hvals key
127.0.0.1:6379> hkeys student
1) "id"
2) "name"
3) "address"
127.0.0.1:6379> hvals student
1) "1"
2) "lisi"
3) "beijing"
hincrby key field increment
hincrbyfloat key field increment
127.0.0.1:6379> hincrby student id 2
(integer) 3
127.0.0.1:6379> hget student id
"3"
127.0.0.1:6379> hincrbyfloat student id 0.5
"3.5"
127.0.0.1:6379> hget student id
"3.5"
hash 类型数据操作的注意事项
hash类型应用场景
hash类型应用场景点这里
list 类型数据基本操作
lpush key value1 [value2] ……
rpush key value1 [value2] ……
127.0.0.1:6379> lpush mylist1 apple banana orange bear
(integer) 4
127.0.0.1:6379> rpush mylist2 aa bb cc dd ee
(integer) 5
lrange key start stop
lindex key index
llen key
127.0.0.1:6379> lrange mylist1 0 10
1) "bear"
2) "orange"
3) "banana"
4) "apple"
127.0.0.1:6379> lrange mylist2 0 10
1) "aa"
2) "bb"
3) "cc"
4) "dd"
5) "ee"
127.0.0.1:6379> lindex mylist1 1
"orange"
127.0.0.1:6379> lindex mylist2 1
"bb"
127.0.0.1:6379> llen mylist1
(integer) 4
127.0.0.1:6379> llen mylist2
(integer) 5
lpop key
rpop key
127.0.0.1:6379> lpop mylist1
"bear"
127.0.0.1:6379> rpop mylist1
"apple"
127.0.0.1:6379> lpop mylist2
"aa"
127.0.0.1:6379> rpop mylist2
"ee"
list 类型数据扩展操作
blpop key1 [key2] timeout
brpop key1 [key2] timeout
brpoplpush source destination timeout
127.0.0.1:6379> blpop mylist1 2
1) "mylist1"
2) "orange"
127.0.0.1:6379> lrange mylist1
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379> lrange mylist1 0 10
1) "banana"
127.0.0.1:6379> brpop mylist2 2
1) "mylist2"
2) "dd"
127.0.0.1:6379> brpoplpush mylist1 0 2
"banana"
127.0.0.1:6379> lrange mylist1 0 10
(empty list or set)
lrem key count value
127.0.0.1:6379> lpush mylist3 aa bb aa cc dd ee dd
(integer) 7
127.0.0.1:6379> lrem mylist3 1 aa
(integer) 1
127.0.0.1:6379> lrange mylist3 0 10
1) "dd"
2) "ee"
3) "dd"
4) "cc"
5) "bb"
6) "aa"
127.0.0.1:6379> lrem mylist3 2 dd
(integer) 2
127.0.0.1:6379> lrange mylist3 0 10
1) "ee"
2) "cc"
3) "bb"
4) "aa"
Tips 6:
list 类型数据操作注意事项
list应用场景
业务场景点击这里
set类型
set 类型数据的基本操作
sadd key member1 [member2]
127.0.0.1:6379> sadd myset1 aa bb cc dd ee dd ff
(integer) 6
smembers key
127.0.0.1:6379> smembers myset1
1) "dd"
2) "bb"
3) "cc"
4) "aa"
5) "ee"
6) "ff"
srem key member1 [member2]
srem myset1 dd
(integer) 1
127.0.0.1:6379> smembers myset1
1) "bb"
2) "cc"
3) "aa"
4) "ee"
5) "ff"
scard key
127.0.0.1:6379> scard myset1
(integer) 5
sismember key member
127.0.0.1:6379> sismember myset1 aa
(integer) 1
127.0.0.1:6379> sismember myset1 dd
(integer) 0
set 类型数据的扩展操作
解决方案
srandmember key [count]
127.0.0.1:6379> srandmember myset1 2
1) "bb"
2) "cc"
127.0.0.1:6379> srandmember myset1
"ee"
spop key [count]
127.0.0.1:6379> spop myset1 1
1) "ff"
127.0.0.1:6379> spop myset1 2
1) "aa"
2) "cc"
sinter key1 [key2]
sunion key1 [key2]
sdiff key1 [key2]
127.0.0.1:6379> sadd myset2 aa bb cc dd
(integer) 4
127.0.0.1:6379> sadd myset3 dd ee ff gg
(integer) 4
127.0.0.1:6379> sinter myset2 myset3
1) "dd"
127.0.0.1:6379> sunion myset2 myset3
1) "cc"
2) "bb"
3) "aa"
4) "dd"
5) "gg"
6) "ee"
7) "ff"
127.0.0.1:6379> sdiff myset2 myset3
1) "bb"
2) "cc"
3) "aa"
sinterstore destination key1 [key2]
sunionstore destination key1 [key2]
sdiffstore destination key1 [key2]
127.0.0.1:6379> sinterstore myset4 myset2 myset3
(integer) 1
127.0.0.1:6379> smembers myset4
1) "dd"
127.0.0.1:6379> sunionstore myset5 myset2 myset3
(integer) 7
127.0.0.1:6379> smembers myset5
1) "cc"
2) "bb"
3) "aa"
4) "dd"
5) "gg"
6) "ee"
7) "ff"
127.0.0.1:6379> sdiffstore myset6 myset2 myset3
(integer) 3
127.0.0.1:6379> smembers myset6
1) "bb"
2) "cc"
3) "aa"
smove source destination member
set应用场景
set应用场景点击这里
sorted_set 类型数据的基本操作
zadd key score1 member1 [score2 member2]
127.0.0.1:6379> zadd sal 1 3600
(integer) 1
127.0.0.1:6379> zadd sal 2 4000
(integer) 1
127.0.0.1:6379> zadd sal 3 10000
(integer) 1
127.0.0.1:6379> zadd sal 4 8000
(integer) 1
zrange key start stop [WITHSCORES]
zrevrange key start stop [WITHSCORES]
127.0.0.1:6379> zrange sal 0 10 withscores
1) "3600"
2) "1"
3) "4000"
4) "2"
5) "10000"
6) "3"
7) "8000"
8) "4"
127.0.0.1:6379> zrevrange sal 0 10 withscores
1) "8000"
2) "4"
3) "10000"
4) "3"
5) "4000"
6) "2"
7) "3600"
8) "1"
zrem key member [member ...]
127.0.0.1:6379> zrem sal 4000
(integer) 1
127.0.0.1:6379> zrem sal 16000
(integer) 0
127.0.0.1:6379> zrange sal 0 10 withscores
1) "3600"
2) "1"
3) "10000"
4) "3"
5) "8000"
6) "4"
zrangebyscore key min max [WITHSCORES] [LIMIT]
zrevrangebyscore key max min [WITHSCORES]
127.0.0.1:6379> zrangebyscore sal 2 4 withscores
1) "10000"
2) "3"
3) "8000"
4) "4"
127.0.0.1:6379> zrevrangebyscore sal 2 4 withscores
(empty list or set)
zremrangebyrank key start stop
zremrangebyscore key min max
127.0.0.1:6379> zremrangebyrank sal 0 3
(integer) 3
127.0.0.1:6379> zremrangebyscore sal 1000 10000
(integer) 0
注意:
zcard key
zcount key min max
127.0.0.1:6379> zadd score 1 80
(integer) 1
127.0.0.1:6379> zadd score 2 90
(integer) 1
127.0.0.1:6379> zadd score 3 60
(integer) 1
127.0.0.1:6379> zadd score 4 70
(integer) 1
127.0.0.1:6379> zadd score 6 85
(integer) 1
127.0.0.1:6379> zcard score
(integer) 5
127.0.0.1:6379> zcount score 2 5
(integer) 3
zinterstore destination numkeys key [key ...]
zunionstore destination numkeys key [key ...]
sorted_set 类型数据的扩展操作
获取数据对应的索引(排名)
zrank key member
zrevrank key member
127.0.0.1:6379> zrank score 60
(integer) 2
127.0.0.1:6379> zrevrank score 60
(integer) 2
score值获取与修改
zscore key member
zincrby key increment member
127.0.0.1:6379> zscore score 60
"3"
127.0.0.1:6379> zincrby score 2 60
"5"
sorted_set 类型数据操作的注意事项
sorted_set 类型应用场景
sorted_set 类型应用场景