Redis 데이터 타입

데이터 타입

basic

Redis Key

키에 관한 몇 가지 규칙

Redis Strings

> set mykey somevalue
OK
> get mykey
"somevalue"
> set mykey "new value1"
OK

# 이미 존재하므로 실패
> set mykey "new value2" NX
(nil)

# 만료 시간 10초
> set mykey "new value3" EX 10
OK
> get mykey
"new value3"

# 10초 후에 접근해보면 nil
> get mykey
(nil)

# NX와 EX를 동시에 사용하는 예
> set mykey2 "new value1" NX EX 10
OK
> set counter 100
OK
> incr counter
(integer) 101
> get counter
"101"
> mset a 10 b 20 c 30
OK
> mget a b c
1) "10"
2) "20"
3) "30"

키 공간을 변경하고 조회하기

> set mykey hello
OK
> exists mykey
(integer)1
> del mykey
(integer)1
> exists mykey
(integer)0
> set mykey x
OK
> type mykey
string
> del mykey
(integer) 1
> type mykey
none

exipre: 키의 만료시간

> set key some-value
OK

# 5초후에 만료
> expire key 5
(integer) 1
> get key (immediately)
"some-value"
> get key (after some time)
(nil)
my-redis:6379> set key hello
OK
my-redis:6379> expire key 5
(integer) 1
my-redis:6379> ttl key
(integer) 3
my-redis:6379> ttl key
(integer) 2
my-redis:6379> ttl key
(integer) 1
my-redis:6379> ttl key
(integer) -2

Redis List

List의 첫번째 단계

> rpush mylist A
(integer) 1
> rpush mylist B
(integer) 2
> lpush mylist first
(integer) 3
> lrange mylist 0 -1
1) "first"
2) "A"
3) "B"
> rpush mylist 1 2 3 4 5 "foo bar"
(integer) 9
> lrange mylist 0 -1
1) "first"
2) "A"
3) "B"
4) "1"
5) "2"
6) "3"
7) "4"
8) "5"
9) "foo bar"
> rpush mylist a b c
(integer) 3
> rpop mylist
"c"
> rpop mylist
"b"
> rpop mylist
"a"
> rpop mylist
(nil)

Common use cases for lists

Capped lists(고정된 크기의 리스트)

> rpush mylist 1 2 3 4 5
(integer) 5
> ltrim mylist 0 2
OK
> lrange mylist 0 -1
1) "1"
2) "2"
3) "3"

Blocking operation on lists

> brpop tasks 5
1) "tasks"
2) "do_something"

키의 자동 생성과 제거

> del mylist
(integer) 1
> lpush mylist 1 2 3
(integer) 3
> set foo bar
OK
> lpush foo 1 2 3
(error) WRONGTYPE Operation against a key holding the wrong kind of value
> type foo
string
> lpush mylist 1 2 3
(integer) 3
> exists mylist
(integer) 1
> lpop mylist
"3"
> lpop mylist
"2"
> lpop mylist
"1"

# 요소를 모두 제거했더니, 해당 키가 존재하지 않음
> exists mylist
(integer) 0
> del mylist
(integer) 0
> llen mylist
(integer) 0
> lpop mylist
(nil)

Redis Hash

> hmset user:1000 username antirez birthyear 1977 verified 1
OK
> hget user:1000 username
"antirez"
> hget user:1000 birthyear
"1977"
> hgetall user:1000
1) "username"
2) "antirez"
3) "birthyear"
4) "1977"
5) "verified"
6) "1"
> hmget user:1000 username birthyear no-such-field
1) "antirez"
2) "1977"
3) (nil)
> hincrby user:1000 birthyear 10
(integer) 1987
> hincrby user:1000 birthyear 10
(integer) 1997

Redis Sets

> sadd myset 1 2 3
(integer) 3
> smembers myset
1. 3
2. 1
3. 2
> sismember myset 3
(integer) 1
> sismember myset 30
(integer) 0
> sadd news:1000:tags 1 2 5 77
(integer) 4
> smembers news:1000:tags
1. 5
2. 1
3. 77
4. 2
> sinter tag:1:news tag:2:news tag:10:news tag:27:news
... results here ...
> sadd k1 a b c
(integer) 3

> sadd k2 c d e
(integer) 3

# 혹시 k0키가 있다면 지우고
> del k0

> sunionstore k0 k1 k2
(integer) 3

> smembers k0
1) "c"
2) "b"
3) "e"
4) "a"
5) "d"

Redis Sorted Set

> zadd hackers 1940 "Alan Kay"
(integer) 1
> zadd hackers 1957 "Sophie Wilson"
(integer) 1
> zadd hackers 1953 "Richard Stallman"
(integer) 1
> zadd hackers 1949 "Anita Borg"
(integer) 1
> zrange hackers 0 -1
1) "Alan Kay"
2) "Anita Borg"
3) "Richard Stallman"
4) "Sophie Wilson"
> zrevrange hackers 0 -1
1) "Sophie Wilson"
2) "Richard Stallman"
3) "Anita Borg"
4) "Alan Kay"
> zrange hackers 0 -1 withscores
1) "Alan Kay"
2) "1940"
3) "Anita Borg"
4) "1949"
5) "Richard Stallman"
6) "1953"
7) "Sophie Wilson"
8) "1957"

범위에서 동작

> zrangebyscore hackers -inf 1950
1) "Alan Kay"
2) "Anita Borg"
# 삭제된 개수를 리턴한다.
> zremrangebyscore hackers 1940 1960
(integer) 4
> zrank hackers "Anita Borg"
(integer) 4

사전적인 스코어

> zadd hackers 0 "Alan Kay" 0 "Sophie Wilson" 0 "Richard Stallman" 0 "Anita Borg" 0 "Yukihiro Matsumoto" 0 "Hedy Lamarr" 0 "Claude Shannon" 0 "Linus Torvalds" 0 "Alan Turing"
> zrange hackers 0 -1
1) "Alan Kay"
2) "Alan Turing"
3) "Anita Borg"
4) "Claude Shannon"
5) "Hedy Lamarr"
6) "Linus Torvalds"
7) "Richard Stallman"
8) "Sophie Wilson"
9) "Yukihiro Matsumoto"

zrangebylex hackers [B [P

1) "Claude Shannon"
2) "Hedy Lamarr"
3) "Linus Torvalds"

스코어 업데이트: 리더보드

Bitmap

> setbit key 10 1
(integer) 1
> getbit key 10
(integer) 1
> getbit key 11
(integer) 0
> setbit key 0 1
(integer) 0
> setbit key 100 1
(integer) 0
> bitcount key
(integer) 2

HyperLogLogs