Applied Redis

Post on 16-May-2015

778 views 3 download

Tags:

transcript

Applied Redis

Barcamp Saigon 2012

About me@hotrannam

KMS Technology

take away

fun

back to school

data structures

big O

do it right( data model & query performance )

redis???

data structureserver

data types

#1 strings#2 hashes#3 lists#4 sets#5 sorted sets

data examples

Keys

page:index.htmllogin_countusers_logged_in_todaylatest_post_idsuser:123:session

users_and_scores

Values

<html><head>[…] string7464{1, 2, 3, 4, 5} set[201, 204, 205] listtime => 10927353 hashusername => joejoe ~ 1348 sorted setfred ~ 938chris ~ 2832

command examples

SET my_key “my value”GET my_keyINCR next_post_idEXPIRE my_key 1234TTL my_keyDEL my_keyEXISTS my_key

HSET product:1 id 1HSET product:1 name “iPad”HSET product:1 available 10

field value

commands have its big O

( mostly )

and beyond

bring to life

#1 cache data

#2 who is online

# add to friends setSADD users:nam:friends duySADD users:nam:friends khoi# add to online setSADD online namSADD online nghiaSADD online khoi# get online friends – {khoi}SINTER users:nam:friends online

#3 leaderboard ( ranking )

# add to leaderboard (sorted set)ZADD leaderboard <score> <player># get top 5ZREVRANGEBYSCORE leaderboard +inf –inf WITHSCORES LIMIT 0 5# get rank of KhoiZREVRANK leaderboard Khoi# get 5 players around KhoiZREVRANGEBYSCORE leaderboard +inf –inf WITHSCORES LIMIT 2 5

max score

min score

countrank

#4 cross-app communication

# web appSUBSCRIBE chatPUBLISH chat “Hello! I’m web app.”

# mobile appSUBSCRIBE chatPUBLISH chat “Hi! I’m mobile app.”

channel

Thank you!