파란하늘의 지식창고
Published 2021. 6. 4. 08:22
redis commands Study/ETC
반응형

redis를 공부하려고 하면서 일단 어떤 것인지 알기 위해 간단한 command 부터 사용해보려고 한다.

접속

우선 로컬에 redis를 설치하고 접속한다.

telnet 127.0.0.1 6379

전체 commands

https://redis.io/commands

 

Command reference – Redis

 

redis.io

redis가 제공하는 기능들을 사용하기 위한 명령어 들이고 group이 ABC 순으로 정렬이 되어 있다.

  • Cluster
  • Connection
  • Geo
  • Hashes
  • HyperLogLog
  • Keys
  • Lists
  • Pub/Sub
  • Scripting
  • Server
  • Sets
  • Sorted Sets
  • Streams
  • Strings
  • Transactions

이 목록으로는 공부할 순서를 유추할 수 없다.

가장 간단한 예제부터 찾아보니 Strings, Keys가 먼저인 듯하다.
이것부터 정리해본다.

명령어는 대소문자를 가리지 않는듯 하다.

redis command 문서는 대문자로 표기되어 있어 그대로 표기를 따른다.

Strings

SET key value [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|KEEPTTL] [NX|XX] [GET]

key value를 저장하는 가장 기본적인 명령어

redis> SET mykey "Hello"
"OK"
redis> GET mykey
"Hello"
redis> SET anotherkey "will expire in a minute" EX 60
"OK"
redis> 

value는 "" 로 묶어서 사용해도 되고 공백이 없는 경우 SET mykey Hello 와 같이 사용해도 된다.

Return value

성공 시 "OK"라는 문자열을 반환한다.

Options

위 예제의 경우 EX란 추가 option으로 anotherkey의 expire time을 지정하였다.

이렇게 추가로 사용할 수 있는 option을 다음과 같이 제공한다.

  • EX seconds -- 지정된 expire time (seconds)을 설정
  • PX milliseconds -- 지정된 expire time (milliseconds)을 설정
  • EXAT timestamp-seconds -- 지정된 expire time (unix time seconds)을 설정
  • PXAT timestamp-milliseconds -- 지정된 expire time (unix time milliseconds)을 설정
  • NX -- key가 존재하지 않은 경우에만 저장
  • XX -- key가 존재하는 경우에만 저장
  • KEEPTTL -- 저장시 기존 expire time을 유지
  • GET -- 저장된 이전 값 없으면 nil 반환

또한 option의 기능을 포함한 명령어가 다음과 같이 제공된다.

  • SETEX [key] [expire time] [value] -- 지정된 expire time (seconds)을 설정
  • PSETEX [key] [expire time] [value] -- 지정된 expire time (milliseconds)을 설정
  • SETNX [key] [value] -- key가 존재하지 않는 경우에만 저장

GET key

set 명령어를 통해 저장된 key의 value를 반환한다.

redis> GET nonexisting
(nil)
redis> SET mykey "Hello"
"OK"
redis> GET mykey
"Hello"
redis> 

MGET key [key ...]

복수의 key의 value를 반환한다.
string value가 없거나 존재하지 않는 key에 대해 nil을 반환하고 응답이 fail이 발생하지 않는다.

redis> SET key1 "Hello"
"OK"
redis> SET key2 "World"
"OK"
redis> MGET key1 key2 nonexisting
1) "Hello"
2) "World"
3) (nil)
redis> 

Return value

Array reply : 지정된 key들의 value list

GETDEL key

key의 value를 가져온 다음 key를 delete 한다.

redis> SET mykey "Hello"
"OK"
redis> GETDEL mykey
"Hello"
redis> GET mykey
(nil)
redis> 

Return value

Bulk string reply : key의 value. 만약 key가 존재하지 않으면 nil 반환 또는 key의 value type이 string이 아닌 경우 error 반환

APPEND key value

기존 key의 value string에 추가. 만약 존재하지 않는 key인 경우 empty string을 생성한 후 추가.

redis> EXISTS mykey
(integer) 0
redis> APPEND mykey "Hello"
(integer) 5
redis> APPEND mykey " World"
(integer) 11
redis> GET mykey
"Hello World"
redis> 

Return value

Integer reply : append 된 후의 string length 반환

STRLEN key

key의 저장된 string value의 length 반환

redis> SET mykey "Hello world"
"OK"
redis> STRLEN mykey
(integer) 11
redis> STRLEN nonexisting
(integer) 0
redis> 

Return value

Integer reply : key의 string value의 length 또는 key가 존재하지 않으면 0 반환

MSET key value [key value ...]

복수의 key value를 저장.

redis> MSET key1 "Hello" key2 "World"
"OK"
redis> GET key1
"Hello"
redis> GET key2
"World"
redis> 

Return Value

String reply : 성공 시 "OK"라는 문자열을 반환.

MSETNX key value [key value ...]

복수의 key value를 저장하며 대상 key중 하나라도 이미 기존에 저장되어 있는 경우 어떤 작업도 수행하지 않는다.

redis> MSETNX key1 "Hello" key2 "there"
(integer) 1
redis> MSETNX key2 "new" key3 "world"
(integer) 0
redis> MGET key1 key2 key3
1) "Hello"
2) "there"
3) (nil)
redis> 

Return value

Integer reply : 저장 시 1을 반환하고 저장하지 않은 경우 0을 반환.

INCR key

key에 저장된 숫자 값을 1씩 증가.
키가 없는 경우 작업을 수행하기 전에 0으로 설정된다.

redis> SET mykey "10"
"OK"
redis> INCR mykey
(integer) 11
redis> GET mykey
"11"
redis> 

Return value

Integer reply : 증가된 key의 value

INCRBY key increment

지정된 숫자만큼 증가시킨다.
키가 없는 경우 작업을 수행하기 전에 0으로 설정된다.

redis> SET mykey "10"
"OK"
redis> INCRBY mykey 5
(integer) 15
redis> 

Return value

Integer reply : 증가된 key의 value

INCRBYFLOAT key increment

지정된 숫자만큼 증가시킨다. (정수가 아닌 실수 사용)

redis> SET mykey 10.50
"OK"
redis> INCRBYFLOAT mykey 0.1
"10.6"
redis> INCRBYFLOAT mykey -5
"5.6"
redis> SET mykey 5.0e3
"OK"
redis> INCRBYFLOAT mykey 2.0e2
"5200"
redis> 

Return value

Bulk string reply : 증가된 key의 value

DECR key

key에 저장된 숫자 값을 1씩 감소시킨다.
키가 없는 경우 작업을 수행하기 전에 0으로 설정된다.

redis> SET mykey "10"
"OK"
redis> DECR mykey
(integer) 9
redis> SET mykey "234293482390480948029348230948"
"OK"
redis> DECR mykey
ERR ERR value is not an integer or out of range
redis> 

Return value

Integer reply : 감소된 key의 value

DECRBY key increment

지정된 숫자만큼 감소시킨다.
키가 없는 경우 작업을 수행하기 전에 0으로 설정된다.

redis> SET mykey "10"
"OK"
redis> DECRBY mykey 3
(integer) 7
redis> 

Return value

Integer reply : 감소된 key의 value

Hashs

HSET key field value [field value ...]

key에 저장된 hash의 field를 value로 설정.
key가 없으면 hash를 포함하는 새 key가 생성.
field가 이미 hash에 있으면 덮어쓴다.
Redis 4.0.0부터 HSET은 가변적이며 여러 field/value pair를 허용함.

redis> HSET myhash field1 "Hello"
(integer) 1
redis> HGET myhash field1
"Hello"
redis> 

Return value

Integer reply: 추가한 field의 수

HKEYS key

key에 저장된 hash의 모든 field 이름을 반환.

redis> HSET myhash field1 "Hello"
(integer) 1
redis> HSET myhash field2 "World"
(integer) 1
redis> HKEYS myhash
1) "field1"
2) "field2"
redis> 

Return value

Array reply: hash에 있는 field list, 또는 key가 존재하지 않는 경우 empty list

HGETALL key

key에 저장된 hash의 모든 field와 value를 반환

redis> HSET myhash field1 "Hello"
(integer) 1
redis> HSET myhash field2 "World"
(integer) 1
redis> HGETALL myhash
1) "field1"
2) "Hello"
3) "field2"
4) "World"
redis> 

Return value

Array reply: hash에 저자오딘 field list와 value, 또는 key가 존재하지 않는 경우 empty list

HDEL key field [field ...]

key에 저장된 hash의 지정된 field를 삭제.
이 hash내에 존재하지 않는 지정된 field는 무시됨.
key가 없으면 empty hash로 처리되고 이 명령은 0을 반환함.

redis> HSET myhash field1 "foo"
(integer) 1
redis> HDEL myhash field1
(integer) 1
redis> HDEL myhash field2
(integer) 0
redis> 

Return value

Integer reply: hash에서 삭제된 field의 수 반환, 지정되었지만 존재하지 않는 field는 포함되지 않음

HINCRBY key field increment

key에 저장된 hash의 지정된 field를 increment만큼 증가
만약 key가 존재하지 않는다면 새 key를 추가하고 field가 없으면 작업이 수행되기 전에 value가 0으로 설정됨
HINCRBY에서 지원하는 값의 범위는 64bit signed integer로 제한됨

redis> HSET myhash field 5
(integer) 1
redis> HINCRBY myhash field 1
(integer) 6
redis> HINCRBY myhash field -1
(integer) 5
redis> HINCRBY myhash field -10
(integer) -5
redis> 

Return value

Integer reply: increment operation 이후의 field의 value

Keys

KEYS pattern

pattern과 일치하는 key를 반환
라이브 환경에서의 사용은 주의해야하며 개발 용도로만 사용해야함.

redis> MSET firstname Jack lastname Stuntman age 35
"OK"
redis> KEYS *name*
1) "firstname"
2) "lastname"
redis> KEYS a??
1) "age"
redis> KEYS *
1) "firstname"
2) "age"
3) "lastname"
redis> 

Return value

Array reply : pattern과 일치하는 key list

DEL key [key ...]

지정된 key를 제거한다.
키가 없으면 무시된다.

redis> SET key1 "Hello"
"OK"
redis> SET key2 "World"
"OK"
redis> DEL key1 key2 key3
(integer) 2
redis> 

Return value

Integer reply : 삭제된 key의 수

EXISTS key [key ...]

key가 존재하는 경우 반환.
Redis 3.0.3 부터 복수 key를 사용할 수 있으며 이 경우 key가 있는 총 수를 반환한다.

redis> SET key1 "Hello"
"OK"
redis> EXISTS key1
(integer) 1
redis> EXISTS nosuchkey
(integer) 0
redis> SET key2 "World"
"OK"
redis> EXISTS key1 key2 nosuchkey
(integer) 2
redis> 

Return value

Integer reply : 존재하면 1, 없으면 9 반환
복수 key 조회 시엔 존재하는 키의 수 반환

TTL key

timeout 시 까지 남은 time 반환 (seconds)

redis> SET mykey "Hello"
"OK"
redis> EXPIRE mykey 10
(integer) 1
redis> TTL mykey
(integer) 10
redis> 

Return value

Integer reply : 남은 second 또는 오류를 알리기 위한 음수 값
-2 : key가 존재하지 않음
-1 : key가 있지만 expire 대상이 아님

PTTL key

timeout 시 까지 남은 time 반환 (milliseconds)

redis> SET mykey "Hello"
"OK"
redis> EXPIRE mykey 1
(integer) 1
redis> PTTL mykey
(integer) 999
redis> 

Return value

Integer reply : 남은 millisecond 또는 오류를 알리기 위한 음수 값
-2 : key가 존재하지 않음
-1 : key가 있지만 expire 대상이 아님

 

반응형
profile

파란하늘의 지식창고

@Bluesky_

내용이 유익했다면 광고 배너를 클릭 해주세요