Skip to content

Instantly share code, notes, and snippets.

@nmvuong92
Last active October 8, 2018 08:57
Show Gist options
  • Save nmvuong92/3becbd9d711f6e40e1e18f0bbdaf48a7 to your computer and use it in GitHub Desktop.
Save nmvuong92/3becbd9d711f6e40e1e18f0bbdaf48a7 to your computer and use it in GitHub Desktop.

REDIS

  • Là ứng dụng lưu trữ cache key-value trên RAM

By default there are 16 databases (indexed from 0 to 15) and you can navigate between them using select command. Number of databases can be changed in redis config file with databases setting. để chọn database

select index

By default, it selects the database 0. To select a specified one, use redis-cli -n 2 (selects db 2)

Supports

  • Strings, Lists, Sets, Sorted sets, Hashes, Bitmap, Hyperlogs, Geospatial Indexes

Advantages Of Redis

  • Very Flexible
  • No schema & column names
  • Very fast: Can perform around 110,000 SETS per second, about 81,000 GETS per second
  • Rich datatype support
  • Caching & Disk persistence (Lưu bộ nhớ đệm và trên đĩa)

Redis & Security

  • Designed to be accessed by trusted clients
  • Do not allow external access/ Internet exposure
  • Simple authentication can be setup
  • Can be restricted to certain interfaces
  • Data enscryption not supported

REDIS SERVER

redis 127.0.0.1:6379

REDIS CLIENT

UTILITIES

KEYS *

Hiển thị tất cả các keys

KEYS * >
1) "foo"
2) "bar"

FLUSHALL

Xóa hết dữ liệu cache trên ram

FLUSHALL > OK
KEYS * > (empty list of set)

STRING

ping > PONG
ECHO 'Hello world' > 'Hello World'
SET foo 100 > OK  
GET foo > "100"   /*string*/
INCR foo > "101"  /*Tăng*/ /*string*/
DECR foo > "100"  /*Giãm*/ /*string*/
EXISTS foo > (integer) 1
EXISTS foo1 > (integer) 0
DEL bar > (integer) 1 /*xóa key - value bar*/
EXISTS bar > (integer) 0
GET bar (nil)

Tương tác server

  • Đặt tên cho server
SET server:name someserver

kết quả:

GET server:name > "some server"
  • set port
SET server:port 8000

kết quả

GET server:port > "8000"

"EXPIRE, TTL" dùng để SET 1 giá trị key-value trong khoảng thời gian

Tạo key-value có tên greeting

SET greeting "Hello world!" > OK

Đặt thời gian tồn tại trong 50s

EXPIRE greeting 50 > (integer) 1

Kiểm tra còn lại bao nhiêu

TTL greeting > (integer) 40

...Chờ 50s sau TTL trả về âm -1 thì giá trị trở về (nil - không tồn tại)

GET greeting > (nil) 

SETEX

Tương tự hàm EXPIRE mà gộp lại ngắn hơn set key value và thời gian

SETEX greeting 30 "Hello world"

PERSIST

Hủy bỏ thời gian tồn tại của key-value về -1 ngay lập tức và đưa key-value thành dạng cache bình thường ví dụ: Tạo key-value có thời gian sống 130s

SETEX greeting 130 "Hello world"

sau đó check thấy còn sống 124s

TTL greeting > (integer) 124

và bây giờ gọi lệnh PERSIST thì TTL trở về -1 và đưa key greeting về dạng cache bình thường mãi mãi

PERSIST greeting > (integer) -1

check lại

GET greeting > "Hello world"

MSET multi set

set nhiều key-value

MSET key1 "hello" key2 "world" > OK

check

GET key1 > "hello"
GET key2 > "world"

APPEND nối thêm string vào value của key

SET key1 "hello"
APPEND key1 " world" > (integer) 11
GET key1 > "hello world"

RENAME đổi tên key

SET key1 "hello world" > OK
RENAME key1 greeting > OK
GET key1 > (nil)
GET greeting > "hello world"

LIST

LPUSH

khởi tạo key nếu chưa có và push giá trị vào danh sách key cú pháp

LPUSH key value [value...]
trả về > số lượng item của list

ví dụ:

LPUSH people "brad" > (integer) 1
LPUSH people "jen"  > (integer) 2
LPUSH people "tom"  > (integer) 3

LRANGE

lấy danh sách ví dụ:

LRANGE people 0 -1
>
1) "tom"
2) "jen"
3) "brad"

XEM FULL COMMANDS TẠI

https://redis.io/commands
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment