12. data examples
Keys Values
page:index.html <html><head>[…] string
login_count 7464
users_logged_in_today {1, 2, 3, 4, 5} set
latest_post_ids [201, 204, 205] list
user:123:session time => 10927353 hash
username => joe
users_and_scores joe ~ 1348 sorted set
fred ~ 938
chris ~ 2832
13. command examples
SET my_key “my value”
GET my_key
INCR next_post_id
EXPIRE my_key 1234
TTL my_key
DEL my_key field value
EXISTS my_key
HSET product:1 id 1
HSET product:1 name “iPad”
HSET product:1 available 10
18. #2 who is online
# add to friends set
SADD users:nam:friends duy
SADD users:nam:friends khoi
# add to online set
SADD online nam
SADD online nghia
SADD online khoi
# get online friends – {khoi}
SINTER users:nam:friends online
19. #3 leaderboard ( ranking )
# add to leaderboard (sorted set)
ZADD leaderboard <score> <player>
# get top 5
ZREVRANGEBYSCORE leaderboard +inf –inf
WITHSCORES LIMIT 0 5
# get rank of Khoi
ZREVRANK leaderboard Khoi
# get 5 players around Khoi
ZREVRANGEBYSCORE leaderboard +inf –inf
WITHSCORES LIMIT 2 5
20. #4 cross-app communication
# web app
SUBSCRIBE chat
PUBLISH chat “Hello! I’m web app.”
# mobile app
SUBSCRIBE chat
PUBLISH chat “Hi! I’m mobile app.”