redis

Redis 처음 사용하기

가는가래 2019. 10. 13. 17:46

[Homepage]

 

https://redis.io/

 

 

[Redis]

 

  1. open source (BSD licensed)
  2. in-memory data structure store (데이터베이스, cache, message broker 처럼 사용), NoSQL 데이터베이스, key-value 저장소라도
  3. strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams 데이터 구조 지원
  4. replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster

 

 

[Redis 설치]

-

 

[Redis 실행 / 정지]

 

${REDIS_DIR}/redis-server (설정 파일이 없을 경우)

 

${REDIS_DIR}/redis-server /path/to/redis.conf   (설정파일이 있을 경우)

 

 

[Redis 서버 설정]

-

 

 

[실습]

 

http://try.redis.io/

 

기본적인 SET, GET 명령어

 

[root@server01 src]# ./redis-cli

127.0.0.1:6379> SET server:name "fido"

OK

127.0.0.1:6379> GET server:name

"fido"

127.0.0.1:6379>

 

 

DEL, INCR 명령어

 

127.0.0.1:6379> SET connections 10

OK

127.0.0.1:6379> INCR connections

(integer) 11

127.0.0.1:6379> INCR connections

(integer) 12

127.0.0.1:6379> DEL connections

(integer) 1

127.0.0.1:6379> INCR connections

(integer) 1

127.0.0.1:6379>

 

INCR 명령어는 atomic operations 위해 제공 . 밖에 여러 타입의 데이터에 atomic oprations 제공을 위한 명령어가 있음.

 

 

EXPIRE, TTL, 명령어

 

127.0.0.1:6379> SET resource:lock "Redis Demo"

OK

127.0.0.1:6379> EXPIRE resource:lock 120

(integer) 1

127.0.0.1:6379> TTL resource:lock

(integer) 110

127.0.0.1:6379> TTL resource:lock

(integer) 95

127.0.0.1:6379> TTL resource:lock

(integer) 44

127.0.0.1:6379> TTL resource:lock

(integer) -2

127.0.0.1:6379> TTL resource:lock

(integer) -2

127.0.0.1:6379>

 

EXPIRE 명령어로 시간 설정이 가능하며, 시간 설정 시간이 지날 수록 TTL 값은 감소한다. -2 삭제 키를 의미 한다. ('-1' 영구 저장을 의미)

조심해야 부분이 있다. -1 영구 저장을 의미한다고 명시적으로 EXPIRE -1 주면 바로 삭제 된다.

이럴 때는 같은 키값으로 다시 SET해주는 방법으로 처리하자.

 

127.0.0.1:6379> SET resource:lock "Redis Demo"

OK

127.0.0.1:6379> EXPIRE resource:lock 120

(integer) 1

127.0.0.1:6379> TTL resource:lock

(integer) 105

127.0.0.1:6379> EXPIRE resource:lock -1

(integer) 1

127.0.0.1:6379> TTL resource:lock

(integer) -2

 

 

List 타입 명령어 (RPUSH, LPUSH, LLEN, LRANGE, LPOP, RPOP)

 

127.0.0.1:6379> RPUSH friends "Alice"

(integer) 1

127.0.0.1:6379> RPUSH friends "Bob"

(integer) 2

127.0.0.1:6379> LPUSH friends "Sam"

(integer) 3

127.0.0.1:6379> LRANGE friends 0 -1

1) "Sam"

2) "Alice"

3) "Bob"

127.0.0.1:6379> LRANGE friends 0 1

1) "Sam"

2) "Alice"

127.0.0.1:6379> LRANGE friends 1 2

1) "Alice"

2) "Bob"

127.0.0.1:6379>

 

리스트의 인덱스가 0부터 시작 한다는 것을 있음. 0번째 항목이 리스트의 첫번째 데이터다.

 

127.0.0.1:6379> LLEN friends

(integer) 3

127.0.0.1:6379> LPOP friends

"Sam"

127.0.0.1:6379> LRANGE friends 0 -1

1) "Alice"

2) "Bob"

127.0.0.1:6379> RPOP friends

"Bob"

127.0.0.1:6379> LLEN friends

(integer) 1

127.0.0.1:6379> LRANGE friends 0 -1

1) "Alice"

127.0.0.1:6379>

 

Stack 데이터 타입과 같이 작동 하는 명령어도 있음. (삭제 )

 

 

Set 타입 명령어 (SADO, SREM, SISMEMBER, SMEMBERS, SUNION)

 

127.0.0.1:6379> SADD superpowers "flight"

(integer) 1

127.0.0.1:6379> SADD superpowers "x-ray vision"

(integer) 1

127.0.0.1:6379> SADD superpowers "reflexes"

(integer) 1

127.0.0.1:6379> SREM superpowers "reflexes"

(integer) 1

127.0.0.1:6379> SMEMBERS superpowers

1) "flight"

2) "x-ray vision"

127.0.0.1:6379> SISMEMBER superpowers "flight"

(integer) 1

127.0.0.1:6379> SISMEMBER superpowers "reflexes"

(integer) 0

127.0.0.1:6379> SADD birdpowers "pecking"

(integer) 1

127.0.0.1:6379> SADD birdpowers "flight"

(integer) 1

127.0.0.1:6379> SUNION superpowers birdpowers

1) "flight"

2) "pecking"

3) "x-ray vision"

 

SMEMBERS 명령어가 set 전체 데이터를 보여주는 명령어 이다.

 

 

Sorted Sets 타입 명령어 (ZADD, ZRANGE)

 

127.0.0.1:6379> ZADD hackers 1940 "Alan Kay"

(integer) 1

127.0.0.1:6379> ZADD hackers 1906 "Grace Hopper"

(integer) 1

127.0.0.1:6379> ZADD hackers 1953 "Richard Stallman"

(integer) 1

127.0.0.1:6379> ZADD hackers 1965 "Yukihiro Matsumoto"

(integer) 1

127.0.0.1:6379> ZADD hackers 1916 "Claude Shannon"

(integer) 1

127.0.0.1:6379> ZADD hackers 1969 "Linus Torvalds"

(integer) 1

127.0.0.1:6379> ZADD hackers 1957 "Sophie Wilson"

(integer) 1

127.0.0.1:6379> ZADD hackers 1912 "Alan Turing"

(integer) 1

127.0.0.1:6379> ZRANGE hackers 2 4

1) "Claude Shannon"

2) "Alan Kay"

3) "Richard Stallman"

127.0.0.1:6379> ZMEMBERS hackers

 

 

Hashes 타입 명령어  (HSET, HGET, HGETALL, HMSET)

 

127.0.0.1:6379> HSET user:1000 name "John Smith"

(integer) 1

127.0.0.1:6379> HSET user:1000 email "john.smith@example.com"

(integer) 1

127.0.0.1:6379> HSET user:1000 password "s3cret"

(integer) 1

127.0.0.1:6379> HGETALL user:1000

1) "name"

2) "John Smith"

3) "email"

4) "john.smith@example.com"

5) "password"

6) "s3cret"

127.0.0.1:6379> HMSET user:1001 name "Mary Jones" password "hidden" email "mjones@example.com"

OK

127.0.0.1:6379> HGET user:1001 name

"Mary Jones"

 

hash내의 값도 strings 값과 같이 increment 명령어를 지원한다.

 

127.0.0.1:6379> HSET user:1000 visits 10

(integer) 1

127.0.0.1:6379> HINCRBY user:1000 visits 1

(integer) 11

127.0.0.1:6379> HINCRBY user:1000 visits 10

(integer) 21

127.0.0.1:6379> HDEL user:1000 visits

(integer) 1

127.0.0.1:6379> HINCRBY user:1000 visits 1

(integer) 1

 

 

[Redis 전체 명령어]

 

https://redis.io/commands#

 

 

[Redis 참조 url]

 

https://redis.io/documentation

https://redis.io/commands

https://redis.io/topics/twitter-clone

https://redis.io/topics/data-types-intro