Redis基础
redis的五大数据类型
Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sort set:有序集合).
list
k- v格式中的v的数据类型是list.
LPUSH key value [ value . . . ]
RPUSH key value [ value . . . ]
LPOP key
RPOP key
LRANGE key start stop
BLPOP key [ key . . . ] timeout
BRPOP key [ key . . . ] timeout
示例:
从左边插入数据: lpush comment :1 1 2 3
3 2 1
从右边插入数据: rpush comment :1 4 5 6
3 2 1 4 5 6
从左边取出所有数据: lrange comment :1 0 - 1
3 2 1 4 5 6
从左边弹出数据: lpop comment :1
3
从右边弹出数据: rpop comment :1
6
基于列表键, 实现新浪\Twitter用户消息列表
LPUSH key value [ value . . . ]
LRANGE key start stop
String
针对字符串操作:
SET key value
MSET key value [ key value . . . ]
SETNX key value ( key 存在, 保存不成功)
GET key
MGET key [ key . . . ]
DEL key
EXPIRE key seconds
PEXPIRE key milliseconds
针对数字操作:
INCRBY key increment
DECRBY key decrement
INCRBY key
DECRBY key
Redis分布式锁核心思路:
SETNX( "couponcode" , 1 ) = = 1
SETNX( "couponcode" , 1 ) = = 0
DEL( "couponcode" ) ;
PEXPIRE( "counponcode" , 10000 ) ;
hash
HashMap< String, HashMap< String, String>> h = new HashMap< String, HashMap< String, String>> ( ) ;
k- v格式中的v的数据类型是hash
针对字符串操作:
HSET key field value
HSETNX key field value
HMSET key field value [ field value . . . ]
HGET key field
HMGET key field[ field. . . ]
HDEL key field[ field. . . ]
针对数字操作:
HINCRBY key field increment
hmset mobile id 1 name huawei price 6688
hmget mobile name
hgetall mobile
hexists mobile name
set
k- v格式中的v的数据类型是set , 无序的, 且具有唯一性.
基本操作
SADD key member [ member. . . ]
SREM key member [ member. . . ]
SMEMBERs key
SCARD key
SISMEMBER key member
SRANDMEMBER key [ count]
SPOP key [ count]
集合运算操作
SINTER key [ key . . . ] SINTERSTORE destination key [ key . . . ]
SUNION key [ key . . . ] SUNIONSTORE destination key [ key . . . ]
SDIFF key [ key . . . ] SDIFFSTORE destination key [ key . . . ]
基于集合建, 实现直播刷礼物, 抽奖
SADD key {userID} 刷礼物, 转发微博加入到集合键中
SMEMBERS key 获取所有用户, 大轮盘转起来
SPOP key [ count] / SRANDMEMBER key [ count] 抽取count中间者
基于集合建, 实现点赞, 签到, like 等功能
点赞
取消点赞
检查用户是否点赞过
获取点赞用户列表
获取点赞用户数
基于集合键运算API, 实现关注模型( 可能认识的人)
我关注的人:sevenSub
青山关注的人:qingSub
mic关注的人:micSub
我和青山共同关注的人:SINTER sevenSub qingSub
我关注的人也关注她:SISMEMBER micSub qing, SISMEMBER jamesSub qing
我可能认识的人:SDIFFSTORE sevenMayKnow qingSub sevenSub
基于集合键运算API, 实现电商商品筛选
每个商品入库的时候即会简历他的静态标签列表如:品牌, 尺寸, 处理器, 内存. . .
SADD brand::lenoovo 拯救者y7000p- 001 ThinkPad- T480
SADD screenSize::15.6 拯救者y7000p- 001 机械革命Z2AIR
SADD processor::i7 拯救者y7000p- 001 机械革命X8TIPlus
SADD memory:8 G 拯救者y7000p- 001 机械革命X8TIPlus ThinkPad- T480
SINTER brand::lenoovo screenSize::15.6 processor::i7 memory:8 G
sadd friends:juran ming xiaoyuan xiye
sadd friend:ming xiaoyuan genghao cc
sinter friends:juran friend:ming
zset
k- v格式中v的数据类型是zset, 有序的
基本操作:
ZADD key score element [ [ score element] [ score element] . . . ]
ZREM key element [ element. . . ]
ZSCORE key element
ZINCRBY key increment element
ZCARD key
ZRANGE key start stop [ WITHSCORES]
ZRERANGE key start stop [ WITHSCORES]
集合运算操作:
ZUNIONSTORE destkey numkeys key [ key . . . ]
ZINTERSTORE destkey numkeys key [ key . . . ]
基于有序集合( zset) 键, 实现单日排行榜
ZADD hotNews{1111 } 1 马蓉怒斥王宝强
ZINCRBY hotNews{1111 } 1 马蓉怒斥王宝强
ZREVRANGE hotNews{1111 } 0 10 WITHSCORES
基于有序集合( zset) 键API, 实现周, 月, 年排行榜
ZUNIONSTORE hotNews{1129 - 1125 } howNews{1119 } howNews{1120 } . . . howNews{1115 }
ZREVRANGE hotNews{1119 - 1125 } 0 10 WITHSCORES
基于有序集合键, 实现自动补齐功能
1. 用户输入
一个词abc, 后台进行分词为a, ab, abc
第二遍abc, 后台进行分词为a, ab, abc
一个词abd, 后台进行分词为a, ab, abd
2. 存入redis
ZINCRBY a 1 abc
ZINCRBY ab 1 abc
ZINCRBY abc 1 abc
ZINCRBY a 1 abc
ZINCRBY ab 1 abc
ZINCRBY abc 1 abc
ZINCRBY a 1 abd
ZINCRBY ab 1 abd
ZINCRBY abd 1 abd
存入结果
a
ab
abc- {2 abc}
abd- {1 abc}
3. 输入a
ZREVRANGE a 0 - 1
zadd mobile:sales 100 huawei 99 ipx 98 vivo 97 oppo
zrange mobile:sales 0 - 1
zrange mobile:sales 0 - 1 withscores
zrevrange mobile:sales 0 - 1 withscores