- 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)
- Strings, Lists, Sets, Sorted sets, Hashes, Bitmap, Hyperlogs, Geospatial Indexes
- 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)
- 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 127.0.0.1:6379
Hiển thị tất cả các keys
KEYS * >
1) "foo"
2) "bar"
Xóa hết dữ liệu cache trên ram
FLUSHALL > OK
KEYS * > (empty list of set)
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 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"
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)
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"
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"
set nhiều key-value
MSET key1 "hello" key2 "world" > OK
check
GET key1 > "hello"
GET key2 > "world"
SET key1 "hello"
APPEND key1 " world" > (integer) 11
GET key1 > "hello world"
SET key1 "hello world" > OK
RENAME key1 greeting > OK
GET key1 > (nil)
GET greeting > "hello world"
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
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