사용 환경
Python(3.8) + Redis
python의 Redis 함수명과 Redis-cli에서 사용하는 명령어가 거의 유사하여 Python으로 진행하였다.
사용할 데이터 타입 6가지 (몇 개 더 있을건데 사용할 법한 애들만)
String, Set, Hash ,SortedSet, List, Bitmap
1. String
조회 : get
입력 : set
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import redis
r = redis.StrictRedis(host="localhost", port=6379, db=0)
key = "String key"
def printFormat(key):
result = r.get(key)
print("=============================")
print('type : {}'.format(type(result)))
print('result : {}'.format(result))
value = "String value"
r.set(key, value)
printFormat(key)
value = "String value 2"
r.set(key, value)
printFormat(key)
|
하나의 키에 하나의 String value가 입력된다.
예시 코드에서 같은 키에 Value를 두 번 입력하게 되면, 해당 키의 값이 교체된다.
1
2
3
4
5
6
|
=============================
type : <class 'bytes'>
result : b'String value'
=============================
type : <class 'bytes'>
result : b'String value 2'
|
위와 같이 출력해보면 교체된 값을 확인할 수 있고,
Redis 전체 조회를 했을 때의 최종 결과 값이 마지막 값으로 저장된 것을 알 수 있다.
2. Set
조회 : smembers
입력 : sadd
삭제 : srem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
import redis
r = redis.StrictRedis(host="localhost", port=6379, db=0)
key = "Set key"
def printFormat(key):
result = r.smembers(key)
print("=============================")
print('type : {}'.format(type(result)))
print('result : {}'.format(result))
# 입력
value = 1
r.sadd(key, value)
printFormat(key)
# 입력
value = 2
r.sadd(key, value)
printFormat(key)
# 중복 입력 : 저장되지 않음
value = 2
r.sadd(key, value)
printFormat(key)
# 입력
value = 3
r.sadd(key, value)
printFormat(key)
# 삭제 : set value 중 값이 3인 것을 삭제 -> 삭제됨
r.srem(key, 3)
printFormat(key)
# 삭제 : set value 중 없는 것을 삭제 시도 -> 별 이상 없음
r.srem(key, 4)
printFormat(key)
|
Set 자료구조의 특징에 맞게 하나의 Key에 Value가 중복 없이 저장된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
=============================
type : <class 'set'>
result : {b'2', b'1'}
=============================
type : <class 'set'>
result : {b'2', b'1'}
=============================
type : <class 'set'>
result : {b'2', b'1'}
=============================
type : <class 'set'>
result : {b'3', b'2', b'1'}
=============================
type : <class 'set'>
result : {b'2', b'1'}
=============================
type : <class 'set'>
result : {b'2', b'1'}
|
Value 2를 여러번 입력했지만 한번만 저장된 것을 확인할 수 있다.
없는 값을 삭제 시도해도 결과에 영향은 없다.
3. Hash
조회 : hset
입력 : hget
삭제 : hdel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import redis
r = redis.StrictRedis(host="localhost", port=6379, db=0)
key = "Hash key"
def printFormat(key, hashKey):
result = r.hget(key, hashKey)
print("=============================")
print('type : {}'.format(type(result)))
print('result : {}'.format(result))
r.hset(key, "a", "A")
printFormat(key, "a")
r.hset(key, "b", "B")
printFormat(key, "b")
# 값 B가 들어있던 키 b에 F를 입력한다. -> Hash의 규칙에 따라 값이 교체된다.
r.hset(key, "b", "F")
printFormat(key, "b")
r.hset(key, "c", "C")
printFormat(key, "c")
r.hset(key, "d", "D")
printFormat(key, "d")
|
하나의 Key 아래 [Key:Value] 구조로 데이터를 저장할 수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
=============================
type : <class 'bytes'>
result : b'A'
=============================
type : <class 'bytes'>
result : b'B'
=============================
type : <class 'bytes'>
result : b'F'
=============================
type : <class 'bytes'>
result : b'C'
=============================
type : <class 'bytes'>
result : b'D'
|
조회할 때, Redis Key와 Redis Key의 Value내에서 사용한 Key를 사용하여 Value 하나 만을 가져올 수 있다.
위와 같이 Key와 Value 쌍으로 저장되는 것을 확인 할 수 있다.
4. SortedSet
조회 : zrange
입력 : zadd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import redis
r = redis.StrictRedis(host="localhost", port=6379, db=0)
key = "Sorted Set key"
# sorted set 은 key와 score의 구조를 갖는다.
value = {}
value['f'] = 1
value['c'] = 4
value['e'] = 2
value['a'] = 6
value['d'] = 3
value['b'] = 5
r.zadd(key, value)
print(r.zrange(key, 0, 5))
listResult = []
listResult = r.zrange(key, 0, 5)
for idx in listResult:
print(idx)
|
SortedSet의 경우, 파이썬 버전마다 입력 방식이 다르다고 한다. *작성 버전은 3.8이다.
입력 시, 위 코드처럼 사용할 값이 Dict의 Key이고, 정렬 index가 Dict의 Value가 된다.
index로 사용되어야 하기 때문에, int값이 들어가야 한다.
1
2
3
4
5
6
7
|
[b'f', b'e', b'd', b'c', b'b', b'a']
b'f'
b'e'
b'd'
b'c'
b'b'
b'a'
|
입력한 key의 순서는 f,c,e,a,d,b 였지만, index의 오름차순 순서로 인해 조회했을 때의 결과가 f,e,d,c,b,a인 것을 확인할 수 있다.
Redis 저장 결과 또한 그런 것을 확인이 가능하다.
5. List
조회 : lrange, lindex
입력 : rpush
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import redis
r = redis.StrictRedis(host="localhost", port=6379, db=0)
key = "List key"
r.rpush(key, 1)
r.rpush(key, 2)
r.rpush(key, 3)
r.rpush(key, 4)
r.rpush(key, 5)
r.rpush(key, 6)
listResult = r.lrange(key, 0, -1)
print(listResult)
listResult2 = r.lindex(key, 1)
print(listResult2)
|
Redis의 List는 입력하는대로 중복을 허용하며 저장된다.
한 번 실행하면,
1
2
|
[b'1', b'2', b'3', b'4', b'5', b'6']
b'2'
|
한 번 더 실행하면,
1
2
|
[b'1', b'2', b'3', b'4', b'5', b'6', b'1', b'2', b'3', b'4', b'5', b'6']
b'2'
|
이와 같이 두 번 다 저장된 것을 확인할 수 있다.
6. Bitmap
조회 : getbit
입력 : setbit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# boolean 값을 저장할 때 사용된다 .
# 적은 용량으로 많은 데이터의 상태값을 유지시킬 수 있다.
# 512mb 메모리로 40억개의 boolean 상태값을 유지할 수 있다. (라고 한다.)
import redis
r = redis.StrictRedis(host="localhost", port=6379, db=0)
key = "Bitmap key"
r.setbit(key, 1, 1)
r.setbit(key, 2, 0)
r.setbit(key, 3, 0)
r.setbit(key, 4, 0)
r.setbit(key, 5, 1)
r.setbit(key, 6, 1)
print(r.getbit(key, 1))
print(r.getbit(key, 2))
print(r.getbit(key, 3))
print(r.getbit(key, 4))
print(r.getbit(key, 5))
print(r.getbit(key, 6))
r.setbit(key, 1, 0)
print("--------------")
print(r.getbit(key, 1))
print(r.getbit(key, 2))
print(r.getbit(key, 3))
print(r.getbit(key, 4))
print(r.getbit(key, 5))
print(r.getbit(key, 6))
|
Hash처럼 Redis Key 아래 [key, value]단위로 데이터를 저장하게 된다. 0,1만 저장되는 저용량 Hash라고 생각하면 될 듯 하다.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
1
0
0
0
1
1
--------------
0
0
0
0
1
1
|
Key 1 의 값이 교체된 것을 알 수 있다.
이건 이상하게 Redis 툴에서 값을 읽지는 못한다.
이유가 있나? Redis Key는 조회가 되는데 Value 조회가 안된다.
'공부 - 개념, 철학 > Redis' 카테고리의 다른 글
Redis mget과 pipeline 차이점 (0) | 2021.07.06 |
---|---|
[Redis] Redis 란 (0) | 2020.02.24 |