Weitere ähnliche Inhalte Ähnlich wie Another Introduce to Redis (20) Another Introduce to Redis4. 介绍
官方定义:
Redis is an open source, advanced key-value store. It is often referred to as
a data structure server since keys can
contain strings, hashes, lists, sets and sorted sets.
4
5. 介绍
官方定义:
Redis is an open source, advanced key-value store. It is often referred to as
a data structure server since keys can
contain strings, hashes, lists, sets and sorted sets.
主要卖点:
Key-value 内存级 高性能 复杂数据结构
5
7. Redis vs Memcached
相同点:
• 都基于内存,性能都足够高
不同点:
• Redis支持复杂类型
• Redis支持主从复制
• Redis支持数据持久化
• Redis支持虚拟内存
• Redis只使用单核
• Redis自身暂不支持sharding
7
9. 数据类型
value:
• String: 二进制安全, 最大1G
• List :String类型的双向链表
• Set :String类型的无序集合,可取交集、并集、差集
• SortSet :可排序的Set, 可指定score, 区间操作
• Hash:String类型的k-v映射
9
10. eg.list
LPUSH mylist a # now the list is “a”
LPUSH mylist b # now the list is “b”, “a”
RPUSH mylist c # now the list is “b”, “a”, “c”
10
11. eg.hash
动手尝试 http://try.redis-db.com/
命令手册 http://redis.io/commands
中文参考 http://redis.readthedocs.org/en/latest/
11
12. 事务
支持连接级别的简单事务 redis> multi
OK
• multi :启动事务
redis> incr a
• exec:提交事务 QUEUED
• discard:放弃事务 redis> incr b
QUEUED
redis> exec
1. (integer) 1
2. (integer) 1
12
19. 简单的微博
• 个人信息
• 关注列表
• 粉丝列表
• 推送信息
19
20. 个人信息
• 用户唯一标识(ID) SET global:newUserId 0
• 录入个人相关信息 …
INCR global:nextUserId
=> 1000
SET uid:1000:username asuka
SET uid:1000:password *****
SET uid:1000:url /asuka
SET uid:1000:xxx xxxxxxxxx
…
20
21. 关注/粉丝
• 添加关注 # 添加关注
SADD uid:1000:following 1001
• 添加粉丝
SADD uid:1000:following 1002
• 统计粉丝数
SADD uid:1000:following 1003
• 获取关注列表 …
• 共同关注 # 添加粉丝
SADD uid:1000:followers 0001
SADD uid:1000:followers 0002
SADD uid:1000:followers 0003
…
#获取关注列表
SMEMBERS uid:1000:following
=>1002,1003,1001
#统计多少粉丝
SCARD uid:1000:followers
=>3
#共同关注
SINTER uid:1000:following uid:1001:following
21
…
22. 推送消息
• 发送消息 # 发送消息
• 接收消息 SET postid:900001 我的第一条微博
RPUSH uid:1000:posts 900001
# 接收消息
RPUSH uid:1001:receive 900001
# 翻页查看
LRANGE uid:1001:receive 0 9
=>900001,893201,939922,…..
22
27. Part 4
参考资料
http://redis.io/
http://blog.nosqlfan.com
http://www.slideshare.net/search/slideshow?searchfrom=header&q=redis
27