-->
728x90

 

[Mac M1] Redis 설치 및 실행

 

Redis는 Key-Value형태의 값을 저장할 수 있는 NoSQL이면서 memcached와 마찬가지인 인메모리 데이터베이스이다. 

매우 빠른 속도로 I/O 작업을 할 수 있고 다양한 자료구조를 지원한다.

 

저번에 homebrew를 설치했기 때문에 너무나 쉽고 간단하게 작업할 수가 있다.

 

1.  brew install redis

➜  ~ brew install redis

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations

Running `brew update --auto-update`...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).

==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2022-07-19_1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/blobs/sha256:9e0df163364a5ae07f3ee2cf39083cd74bcb38
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:9e0df163364a5ae07f3ee2cf39083cd
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/1.1/manifests/1.1.1q
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/1.1/blobs/sha256:4e7b6cb2252c554a8e25ecae960f2fcd9a7fa742aa
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:4e7b6cb2252c554a8e25ecae960f2fc
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/redis/manifests/7.0.4
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/redis/blobs/sha256:5fd4a4cdd769fdebc82dc5ea1b06edb2464039b38c09355f
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:5fd4a4cdd769fdebc82dc5ea1b06edb
######################################################################## 100.0%
==> Installing dependencies for redis: ca-certificates and openssl@1.1
==> Installing redis dependency: ca-certificates
==> Pouring ca-certificates--2022-07-19_1.all.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while...
🍺  /opt/homebrew/Cellar/ca-certificates/2022-07-19_1: 3 files, 222.6KB
==> Installing redis dependency: openssl@1.1
==> Pouring openssl@1.1--1.1.1q.arm64_monterey.bottle.tar.gz
🍺  /opt/homebrew/Cellar/openssl@1.1/1.1.1q: 8,097 files, 18MB
==> Installing redis
==> Pouring redis--7.0.4.arm64_monterey.bottle.tar.gz
==> Caveats
To restart redis after an upgrade:
  brew services restart redis
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/redis/bin/redis-server /opt/homebrew/etc/redis.conf
==> Summary
🍺  /opt/homebrew/Cellar/redis/7.0.4: 14 files, 2.6MB
==> Running `brew cleanup redis`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> redis
To restart redis after an upgrade:
  brew services restart redis
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/redis/bin/redis-server /opt/homebrew/etc/redis.conf

 

2.  redis-server

➜  ~ redis-server
76063:C 04 Aug 2022 13:40:28.155 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
76063:C 04 Aug 2022 13:40:28.155 # Redis version=7.0.4, bits=64, commit=00000000, modified=0, pid=76063, just started
76063:C 04 Aug 2022 13:40:28.155 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
76063:M 04 Aug 2022 13:40:28.155 * Increased maximum number of open files to 10032 (it was originally set to 256).
76063:M 04 Aug 2022 13:40:28.155 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 7.0.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 76063
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

76063:M 04 Aug 2022 13:40:28.155 # WARNING: The TCP backlog setting of 511 cannot be enforced because kern.ipc.somaxconn is set to the lower value of 128.
76063:M 04 Aug 2022 13:40:28.155 # Server initialized
76063:M 04 Aug 2022 13:40:28.156 * Ready to accept connections

 

3.  brew services start redis

➜  ~ brew services start redis
==> Tapping homebrew/services
Cloning into '/opt/homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 2057, done.
remote: Counting objects: 100% (449/449), done.
remote: Compressing objects: 100% (102/102), done.
remote: Total 2057 (delta 380), reused 350 (delta 347), pack-reused 1608
Receiving objects: 100% (2057/2057), 557.41 KiB | 1.32 MiB/s, done.
Resolving deltas: 100% (951/951), done.
Tapped 1 command (45 files, 711.0KB).
==> Successfully started `redis` (label: homebrew.mxcl.redis)

 

4.  redis-cli

➜  ~ redis-cli
127.0.0.1:6379>

 

5.  set, get

redis는 set key value 문법으로 작성한다.

get key 명령어를 통해 저장된 value값을 가져온다.

127.0.0.1:6379> set key1 "hello Minggu"
OK
127.0.0.1:6379> get key1
"hello Minggu"
127.0.0.1:6379>

 

6.  append 

append key value는 기존에 저장된 value값 뒤에 문자열을 추가할 수 있다.

127.0.0.1:6379> set key2 "minggu"
OK
127.0.0.1:6379> get key2
"minggu"
127.0.0.1:6379> append key2 " is Developer"
(integer) 18
127.0.0.1:6379> get key2
"minggu is Developer"
127.0.0.1:6379>

 

7. Set

127.0.0.1:6379> smembers set:mingguSet
(empty array)
127.0.0.1:6379> sadd set:mingguSet minggu
(integer) 1
127.0.0.1:6379> sadd set:mingguSet like
(integer) 1
127.0.0.1:6379> sadd set:mingguSet pokemon
(integer) 1
127.0.0.1:6379> sadd set:mingguSet pokemon game
(integer) 1
127.0.0.1:6379> smembers set:mingguSet
1) "like"
2) "game"
3) "pokemon"
4) "minggu"
127.0.0.1:6379>

sadd 명령어를 통해 Set 자료구조를 이용할 수 있다.

중복을 허용하지 않는 Set 구조이기에 중간에 'pokemon'은 삭제되었다. 그리고 순서가 섞여 나온다는 것을 알 수 있다.

 

8. sortedSet

127.0.0.1:6379> zadd rank 1 A
(integer) 1
127.0.0.1:6379> zadd rank 1 B
(integer) 1
127.0.0.1:6379> zadd rank 1 C
(integer) 1
127.0.0.1:6379> zadd rank 1 D
(integer) 1
127.0.0.1:6379> zrange rank 0 -1
1) "A"
2) "B"
3) "C"
4) "D"
127.0.0.1:6379>

zadd 명령어를 통해 가중치 값을 지정할 수 있다.

zrange 명령어는 출력 범위를 지정한다. (-1이면 뒤에서부터 시작)

 

9. HashMap

127.0.0.1:6379> hset user name minggu
(integer) 1
127.0.0.1:6379> hset user age 15
(integer) 1
127.0.0.1:6379> hset user address "Seoul"
(integer) 1
127.0.0.1:6379> hgetall user
1) "name"
2) "minggu"
3) "age"
4) "15"
5) "address"
6) "Seoul"
127.0.0.1:6379>

hset 명령어를 통해 key - value값을 설정하고

hgetall {name}을 통해 값을 가져온다.

반응형

'Database > 📦Redis' 카테고리의 다른 글

[Redis] Cache 전략  (0) 2024.07.29
Redis의 분산락 사용에 대해서  (0) 2024.07.19

+ Recent posts