0%

Redis Notes

Redis Notes

  • C
  • Corss-Platform
  • NoSQL

Install

  • Official
  • GitHub
  • Raspberry Pi : sudo apt install redis-server
  • macOS : brew install redis

Usage

$ redis-cli

Data Types

  • Strings, Lists, Sets, Sorted Sets, Hashes
  • case-sensitive

clear : clear console
ping : test redis work
FLUSHALL : 清空
ECHO '123'

Strings

SET foo 100
GET foo
SET bar 'hi'
GET bar

INCR foo : +1
DECR foo : -1
EXISTS foo : 是否存在
EXISTS foooo

SET server:name hihi
GET server:name
SET server:port 8000
GET server:port

EXPIRE bar 50 : 50秒後過期
TTL bar : 看剩幾秒

SETEX bar 30 "hi" : 過期前設值
TTL bar : 看剩幾秒
PERSIST bar : 永久存在

MSET : 設定很多值
APPEND : 串接加上
RENAME : 改名

Lists (L)

LPUSH people "abby1"
LPUSH people "abby2"
RPUSH people "abby3"
RPUSH people "abby4"

LRANGE people 0 -1 : print all
LRANGE people 0 1 : index 0, 1

LPOP people
RPOP peolple

LINSERT people BEFORE "abby2" "abbyX"

Sets (S)

SADD cars "Ford"
SADD cars "Honda"
SADD cars "BWM"

SISMEMBER cars "Ford"
SMEMBERS cars

SMOVE cars mycars "Ford"

SREM : remove

Sorted Sets (Z)

ZADD users 1988 'abby'
ZADD users 1288 'ybba'

ZRANGE users 0 -1 : print all

ZRANK user 'abby' : 回傳排名

Hashes (H)

HSET user:abby name "Abby"
HSET user:abby email "abby@com"
HGET user:abby
HGET user:abby email
HGETALL user:abby

Data Persistence

reference

Snapshotting
save 60 1000

Applications

reference

  • 短網址系統
  • 統計系統
  • 秒殺搶購系統

Node.js client

ioredis

Python client

redis