redis edu 2
TRANSCRIPT
Redis collections
Key/Value
List
Set
Sorted Set
Hash
Key/Value
$> set <KEY> <Value>
$> get <KEY>
Key/Value
$> mset <KEY> <Value> <KEY> …
$> mget <KEY> <KEY> …
Ex) Key/Value insert into userinfo(name, email) values (‘charsyam’,’[email protected]’)
Ex) Key/Value set charsyam:name charsyam set charsyam:email [email protected] mget charsyam:name charsyam:email 1) “charsyam” 2) “[email protected]”
K/V Internal
Hash, O(1)
Separate Chainning With Linked List
K/V Internal
Hash Expand #1
List
$> rpush listname a --- (a) $> rpush listname b --- (a, b) $> lpush listname c --- (c, a, b) $> rpop listname(or lpop listname)
List
Job Queue가 필요할 때…
Redis 기반의 유명한 Job Queue - SideKiq, Resque - SideKiq 추천
LPOP, BLPOP
Sidekiq 과 Resque의 큰 차이
LPOP은 polling, BLPOP은 push
LPOP은 list에 데이터가 없으면 바로 리턴 BLPOP은 데이터가 들어올 때 까지 대기함.
Set
$> sadd setname id1 $> sadd setname id2 $> smember setname 1) “id2” 2) “id1”
Set
특정 유저를 follow 하는 유저들의 목록등을 저장할 때…
Sorted Set
$> zadd zsetname 1 “one” $> zadd zsetname 2 “two” $> zadd zsetname 3 “three”
Sorted Set
$> zrange zsetname 0 -1 1) “one” 2) “two” 3) “three”
Sorted Set
말 그대로 정렬된 Set User Ranking 등을 구현할 때 유리
Sorted Set
Set 은 앞의 Hash 형태로 저장되지만 Sorted Set은 SkipList로 구현됨.
SkipList
지하철 급행이라고 생각하면 쉬움
Hash insert into userinfo (name, email) values(‘charsyam’, ‘[email protected]’);
Hash $> hmset charsyam name charsyam email [email protected]
Hash $> hgetall charsyam 1) “name” 2) “charsyam” 3) “email” 4) “[email protected]”
Hash Hash는 기본 Key/Value 안에 다시 Hash 구조체가 있는 형태
자주 나오는 질문 K/V로 저장하는게 좋을까요? 아니면 Hash에 저장하는게 좋을까요?
자주 나오는 질문 그 때, 그 때 다릅니다만… 한 Hash 안에 데이터가 몇 만개 이상이 되는건 좋지 않습니다.
Thank you.