DBMS/Redis

[REDIS] ๐Ÿ“š ์ „์—ญ ํ‚ค ๋ช…๋ น์–ด(Common Keys) ์ข…๋ฅ˜ ๐Ÿ’ฏ ์ •๋ฆฌ

์ธํŒŒ_ 2022. 7. 4. 06:00

Redis-Common-Keys

Redis Common Keys

Common์— ์žˆ์€ ๋ช…๋ น์–ด๋“ค์€ 5๊ฐ€์ง€ Data type(์ž๋ฃŒ๊ตฌ์กฐ)์— ๊ด€๊ณ„์—†์ด ๋ชจ๋“  Key ์ ์šฉ๋˜๋Š” ๋ช…๋ น์„ ๋œปํ•œ๋‹ค.

 

Common Keys ๋ช…๋ น์–ด ๋ฆฌ์ŠคํŠธ

Commands Syntax Description
EXISTS key Key๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
UNLINK key Key๋ฅผ ์‚ญ์ œ
RM key (subquery) Key๋ฅผ ์‚ญ์ œ (Enterprise server)
KEYS key pattern Key๋“ค์„ pattern์œผ๋กœ ์กฐํšŒ
RENAME key new_key Key ์ด๋ฆ„์„ ๋ณ€๊ฒฝ
RENAMENX key new_key new_key๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋งŒ key ์ด๋ฆ„์„ ๋ณ€๊ฒฝ
EXPIRE key seconds ์ง€์ •๋œ ์‹œ๊ฐ„(์ดˆ) ํ›„ key ์ž๋™ ์‚ญ์ œ
RANDOMKEY   random์œผ๋กœ key๋ฅผ ์กฐํšŒ
SORT key [BY pattern] [LIMIT offset count]
[GET pattern [GET pattern ...]]
[ASC|DESC] [ALPHA] [STORE destination]
๋‹ค๋ฅธ key์™€ joinํ•˜์—ฌ data๋ฅผ sortํ•˜์—ฌ ์กฐํšŒ
TTL key ๋‚จ์€ expire time(seconds)์„ ์กฐํšŒ
TYPE key key์˜ data type์„ ์กฐํšŒ
EXPIREAT key timestamp ์ง€์ •๋œ ์‹œ๊ฐ„(Unix timestamp) ํ›„ key ์ž๋™ ์‚ญ์ œ
PERSIST key Expire time์„ ์‚ญ์ œ
OBJECT subcommand key key์— ๋Œ€ํ•œ ์ •๋ณด ์กฐํšŒ
PEXPIRE key milliseconds ์ง€์ •๋œ ์‹œ๊ฐ„(milliseconds) ํ›„ key ์ž๋™ ์‚ญ์ œ
PEXPIREAT key milliseconds-timestamp ์ง€์ •๋œ ์‹œ๊ฐ„(Unix milliseconds-timestamp) ํ›„ key ์ž๋™ ์‚ญ์ œ
PTTL key ๋‚จ์€ expire time์„ milliseconds๋กœ ์กฐํšŒ
MOVE key db Key๋ฅผ ๋‹ค๋ฅธ DB๋กœ ์˜ฎ๊ธด๋‹ค
DUMP key Data๋ฅผ dump
RESTORE key ttl serialized-value [REPLACE] Data๋ฅผ restore
MIGRATE host port key destination-db
timeout [COPY] [REPLACE]
Data๋ฅผ ๋‹ค๋ฅธ Redis Server๋กœ ์ด๋™/๋ณต์‚ฌ
SCAN cursor [MATCH pattern]
[COUNT count]
Key๋“ค์„ ์ผ์ • ๋‹จ์œ„ ๊ฐฏ์ˆ˜ ๋งŒํผ์”ฉ ์กฐํšŒ

 

๋ชจ๋“  ํ‚ค ๊ฐ€์ ธ์˜ค๊ธฐ (keys)

> keys * # ๋ชจ๋“  ํ‚ค ๊ฒ€์ƒ‰

> keys o* # o๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ‚ค ๊ฒ€์ƒ‰ [ํŒจํ„ด]

Redis-Common-Keys

๋ ˆ๋””์Šค ์ž์ฒด ์„ฑ๋Šฅ ๋–„๋ฌธ์— keys * ๊ฐ™์€ ๋ช…๋ น์–ด ๋Œ€์‹  scan 0์œผ๋กœ ๋Œ€์ฒด ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
"์žฌ๊ท€์ "์œผ๋กœ key๋ฅผ ์ˆœ์ฐจ ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

ํ‚ค๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ (exists)

> exists <key>

Redis-Common-Keys

 

ํ‚ค ์ด๋ฆ„ ๋ณ€๊ฒฝ (rename)

> rename <key>

 

ํ‚ค ์‚ญ์ œ (unlink)

> SET key1 "Hello"
"OK"

> SET key2 "World"
"OK"

> UNLINK key1 key2
(integer) 2
๋ ˆ๋””์Šค db์— ํ‚ค ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์„๋•Œ del ์ปค๋ฉ˜ํŠธ ์น˜๋ฉด ์ˆ˜ํ–‰ ์˜ค๋ž˜๊ฑธ๋ฆฐ๋‹ค. (ํ‚ค ์ง€์šฐ๋Š” ๋™์•ˆ ์•„๋ฌด๊ฒƒ๋„ ๋ชปํ•จ)
๋”ฐ๋ผ์„œ unlink์˜ ๊ฒฝ์šฐ์—๋Š” back ground ์ž‘์—…์œผ๋กœ ํ‚ค๋ฅผ ์—†์• ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ช…๋ น์–ด๋กœ ๋Œ€์ฒด ํ•˜๊ธธ ์ถ”์ฒœ๋œ๋‹ค.

 

๋ชจ๋“  ํ‚ค ์‚ญ์ œ (flushall)

> flushall

 

ํ‚ค ์ž๋™ ์‚ญ์ œ (expire / ttl)

์„ค์ •ํ•œ ์‹œ๊ฐ„๋‚ด๋กœ key๋ฅผ ์ž๋™ ์‚ญ์ œํ•˜๋Š” ๋ช…๋ น์–ด ์ด๋‹ค.

๋‹ค๋งŒ, ๊ธฐ๊ฐ„ ์‚ญ์ œ๋ฅผ ๋“ฑ๋กํ•˜๊ณ  ๋ฐ”๋กœ set, getset์˜ ๋ช…๋ น์–ด๋ฅผ expire ๋ช…๋ น์–ด ์ดํ›„์— key์— ๋‹ค์‹œ ์ ์šฉํ•˜๋ฉดexpire ๋ช…๋ น์€ ๋ฌดํšจ์ฒ˜๋ฆฌ๊ฐ€ ๋œ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ incr, lpush, sadd, zadd, hset๊ณผ ๊ฐ™์€ ๋ช…๋ น์–ด๋Š” ์œ ์ง€๋œ๋‹ค.

# expire <key> <member> <second> : key์— ttl ์„ค์ • (ttl : Time To Live)
# ttl <key> : ๋‚จ์€ ttl์„ ์ดˆ๋‹จ์œ„๋กœ ํ™•์ธ

127.0.0.1:6379> set a b
OK

127.0.0.1:6379> expire a 20 # key a๋ฅผ 20์ดˆ ํ›„์— ์‚ญ์ œ
(integer) 1

127.0.0.1:6379> ttl a # key ์‚ญ์ œ ๋‚จ์€ ์‹œ๊ฐ„์ด ์–ผ๋งˆ ๋‚จ์•˜๋Š”์ง€
(integer) 16

127.0.0.1:6379> ttl a
(integer) -2

127.0.0.1:6379> get a
(nil)

 

ํ™”๋ฉด ํด๋ฆฌ์–ด (clear)

> clear

 

๋ฐ์ดํ„ฐ ์˜๊ตฌ์ €์žฅ (save / bgsave)

  • ํ˜„์žฌ ์ž…๋ ฅํ•œ key/value ๊ฐ’์„ ํŒŒ์ผ๋กœ ์ €์žฅ ๊ฐ€๋Šฅ
  • config์—์„œ ์„ค์ •ํ•˜์—ฌ ์ž๋™ ์ €์žฅ๋„ ๊ฐ€๋Šฅ
  • exit ํ›„ redis-server์˜ working directory ์•ˆ์— rdbํŒŒ์ผ์ด ์ƒ์„ฑ๋จ
> save # ํฌ๊ทธ๋ผ์šด๋“œ๋กœ rdb ํŒŒ์ผ๋กœ ์ €์žฅ

> bgsave # ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์ €์žฅ