Redis Key command
Các command cơ bản để quản lý key trong Redis
- Redis Del command - Xóa Key trong Redis
- Redis Dump command - Lệnh Dump trong Redis
- Redis Exists command - Kiểm tra Key đã tồn tại trong Redis
- Redis Expire command - Lệnh thiết lập thời gian hết hạn của Key trong Redis
- Redis Expireat command - Lệnh thiết lập thời gian hết hạn của Key trong Redis
- Redis Pexpire command - Lệnh thiết lập thời gian hết hạn của Key trong Redis
- Redis Pexpireat command - Lệnh thiết lập thời gian hết hạn của Key trong Redis
- Redis Keys command - Lệnh tìm kiếm gần đúng theo Key trong Redis
- Redis Move command - Lệnh move Key sang database khác trong Redis
- Redis Persist command - Lệnh xóa thiết lập thời gian hết hạn của Key trong Redis
- Redis Pttl command - Lệnh kiểm tra thời gian hết hạn của Key trong Redis
- Redis TTL command - Lệnh kiểm tra thời gian hết hạn còn lại của Key trong Redis
- Redis RANDOMKEY command - Lệnh lấy một Key ngẫu nhiên trong Redis
- Redis Rename command - Lệnh rename Key trong Redis
- Redis Renamenx command - Lệnh rename Key trong Redis
- Redis Type command - Lệnh kiểm tra loại dữ liệu giá trị của Key trong Redis
Redis Del command - Xóa Key trong Redis
Redis DEL command được sử dụng để xóa một bản ghi theo key đã tồn tại.
Return Value:
Số bản ghi (key) đã được xóa.
Cú pháp:
redis 127.0.0.1:6379> DEL KEY_NAME
Ví dụ:
# redis 127.0.0.1:6379> SET tutorialspoint redis
OK
# redis 127.0.0.1:6379> DEL tutorialspoint
(integer) 1
Redis Dump command - Lệnh Dump trong Redis
Redis DUMP command được sử dụng để trả về serialized version của giá trị được lưu trữ bởi key.
Return value:
Serialized value (String)
Cú pháp:
redis 127.0.0.1:6379> DUMP KEY_NAME
Ví dụ:
# redis 127.0.0.1:6379> SET tutorialspoint redis
OK
# redis 127.0.0.1:6379> DUMP tutorialspoint
"\x00\x05redis\x06\x00S\xbd\xc1q\x17z\x81\xb2"
Redis Exists command - Kiểm tra Key đã tồn tại trong Redis
Redis EXISTS command được sử dụng để kiểm tra xem một key
đã tồn tại hay chưa
Return Value
Integer value:
- 1, nếu key đã tồn tại.
- 0, nếu key không tồn tại.
Syntax:
redis 127.0.0.1:6379> EXISTS KEY_NAME
Ví dụ:
redis 127.0.0.1:6379> EXISTS tutorialspoint-new-key
(integer) 0
redis 127.0.0.1:6379> EXISTS tutorialspoint-new-key
(integer) 1
Redis Expire command - Lệnh thiết lập thời gian hết hạn của Key trong Redis
Redis Expire command được sử dụng để thiết lập thời gian hết hạn của một key. Sau thời gian này, key sẽ không còn tồn tại trên Redis.
Return Value
Integer value:
- 1, nếu thời gian timeout được thiết lập cho key.
- 0, nếu key không tồn tại hoặc thời gian timeout không được thiết lập cho key.
Syntax:
redis 127.0.0.1:6379> Expire KEY_NAME TIME_IN_SECONDS
Ví dụ:
redis 127.0.0.1:6379> SET laptrinhvn redis
OK
redis 127.0.0.1:6379> EXPIRE laptrinhvn 60
(integer) 1
Trong ví dụ trên, key laptrinhvn
được thiết lập thời gian expire là 60s (1 phút). Sau 1 phút, key laptrinhvn
sẽ bị expire tự động
Redis Expireat command - Lệnh thiết lập thời gian hết hạn của Key trong Redis
Redis Expireat command được sử dụng để thiết lập thời gian hết hạn của một key được tính theo Unix timestamp. Sau thời gian này, key sẽ không còn tồn tại trên Redis.
Return Value
Integer value:
- 1, nếu thời gian timeout được thiết lập cho key.
- 0, nếu key không tồn tại hoặc thời gian timeout không được thiết lập cho key.
Syntax:
redis 127.0.0.1:6379> Expireat KEY_NAME TIME_IN_UNIX_TIMESTAMP
Ví dụ:
redis 127.0.0.1:6379> SET laptrinhvn redis
OK
redis 127.0.0.1:6379> EXPIREAT laptrinhvn 1570939008
(integer) 1
EXISTS laptrinhvn
(integer) 0
Trong ví dụ trên, key laptrinhvn
được thiết lập thời gian expire là 1570939008 (2019-10-13T03:56:48+00:00). Sau thời gian này, key laptrinhvn
sẽ bị expire tự động
Redis Pexpire command - Lệnh thiết lập thời gian hết hạn của Key trong Redis
Redis Pexpire command được sử dụng để thiết lập thời gian hết hạn của một key (tính theo miliseconds). Sau thời gian này, key sẽ không còn tồn tại trên Redis.
Return Value
Integer value:
- 1, nếu thời gian timeout được thiết lập cho key.
- 0, nếu key không tồn tại hoặc thời gian timeout không được thiết lập cho key.
Syntax:
redis 127.0.0.1:6379> PEXPIRE KEY_NAME TIME_IN_MILLISECONDS
Ví dụ:
redis 127.0.0.1:6379> SET laptrinhvn redis
OK
redis 127.0.0.1:6379> PEXPIRE laptrinhvn 5000
(integer) 1
Trong ví dụ trên, key laptrinhvn
được thiết lập thời gian expire là 5000 ms (5s). Sau 5s, key laptrinhvn
sẽ bị expire tự động
Redis Pexpireat command - Lệnh thiết lập thời gian hết hạn của Key trong Redis
Redis Pexpireat command được sử dụng để thiết lập thời gian hết hạn của một key được tính theo Unix timestamp miliseconds. Sau thời gian này, key sẽ không còn tồn tại trên Redis.
Return Value
Integer value:
- 1, nếu thời gian timeout được thiết lập cho key.
- 0, nếu key không tồn tại hoặc thời gian timeout không được thiết lập cho key.
Syntax:
redis 127.0.0.1:6379> PEXPIREAT KEY_NAME TIME_IN_MILLISECONDS_IN_UNIX_TIMESTAMP
Ví dụ:
redis 127.0.0.1:6379> SET laptrinhvn redis
OK
redis 127.0.0.1:6379> PEXPIREAT laptrinhvn 1570939008000
(integer) 1
EXISTS laptrinhvn
(integer) 0
Trong ví dụ trên, key laptrinhvn
được thiết lập thời gian expire là 1570939008 (2019-10-13T03:56:48+00:00). Sau thời gian này, key laptrinhvn
sẽ bị expire tự động
Redis Keys command - Lệnh tìm kiếm gần đúng theo Key trong Redis
Redis Keys command được sử dụng để tìm kiếm key theo pattern.
Return Value:
Danh sách (Array) các key là trùng với pattern
Syntax:
redis 127.0.0.1:6379> KEYS PATTERN
Ví dụ:
redis 127.0.0.1:6379> SET tutorial1 redis
OK
redis 127.0.0.1:6379> SET tutorial2 mysql
OK
redis 127.0.0.1:6379> SET tutorial3 mongodb
OK
redis 127.0.0.1:6379> KEYS tutorial*
1) "tutorial3"
2) "tutorial1"
3) "tutorial2"
Để get tất cả các key trong Redis, sử dung *
redis 127.0.0.1:6379> KEYS *
1) "tutorial3"
2) "tutorial1"
3) "tutorial2"
Redis Move command - Lệnh move Key sang database khác trong Redis
Redis MOVE command được sử dụng để move một key từ database hiện tại sang một database khác.
Return Value:
Integer value:
- 1, nếu key được move thành công.
- 0, nếu key không được move.
Syntax:
redis 127.0.0.1:6379> MOVE KEY_NAME DESTINATION_DATABASE
Ví dụ:
Đầu tiên, tạo một key trong Redis và set value cho nó:
redis 127.0.0.1:6379> SET laptrinhvn redis
OK
Trong Redis, mặc định database 0th là database được sử dụng, chúng ta sẽ move key laptrinhvn tới database khác (1st):
redis 127.0.0.1:6379> MOVE laptrinhvn 1
1) (integer) 1
Redis Persist command - Lệnh xóa thiết lập thời gian hết hạn của Key trong Redis
Redis Persist command được sử dụng để xóa một thiết lập expire của key.
Return value:
Integer value:
- 1, nếu timeout được xóa khỏi thiết lập của key.
- 0, nếu key không tồn tại hoặc không có thiết lập timeout cho key.
Syntax:
redis 127.0.0.1:6379> PERSIST KEY_NAME
Ví dụ:
redis 127.0.0.1:6379> SET tutorial1 redis
OK
redis 127.0.0.1:6379> EXPIRE tutorial1 60
1) (integer) 1
redis 127.0.0.1:6379> TTL tutorial1
1) (integer) 60
redis 127.0.0.1:6379> PERSIST tutorial1
1) (integer) 1
redis 127.0.0.1:6379> TTL tutorial1
1) (integer) -1
Redis Pttl command - Lệnh kiểm tra thời gian hết hạn của Key trong Redis
Redis PTTL command được sử dụng để get thời gian expiry còn lại (time to live) của key (milliseconds).
Return value:
Integer value:
- TTL (milliseconds).
- -1, nếu key không có thiết lập expire.
- -2, nếu key không tồn tại.
Syntax:
redis 127.0.0.1:6379> PTTL KEY_NAME
Vi dụ:
redis 127.0.0.1:6379> SET laptrinhvn redis
OK
redis 127.0.0.1:6379> EXPIRE laptrinhvn 1
1) (integer) 1
redis 127.0.0.1:6379> PTTL laptrinhvn
1) (integer) 999
Redis TTL command - Lệnh kiểm tra thời gian hết hạn còn lại của Key trong Redis
Redis TTL command được sử dụng để get thời gian expiry còn lại (time to live) của key (seconds).
Return value:
Integer value:
- TTL (seconds).
- -1, nếu key không có thiết lập expire.
- -2, nếu key không tồn tại.
Syntax:
redis 127.0.0.1:6379> TTL KEY_NAME
Vi dụ:
redis 127.0.0.1:6379> SET laptrinhvn redis
OK
redis 127.0.0.1:6379> EXPIRE laptrinhvn 60
1) (integer) 1
redis 127.0.0.1:6379> TTL laptrinhvn
1) (integer) 59
Redis RANDOMKEY command - Lệnh lấy một Key ngẫu nhiên trong Redis
Redis RANDOMKEY command được sử dụng để get một key ngẫu nhiên từ Redis database.
Return value:
- String - Một key ngẫu nhiên
- nill - Nếu database không có bất kỳ key nào
Syntax:
redis 127.0.0.1:6379> RANDOMKEY
Ví dụ:
//First, create some keys in Redis and set some values in it.
redis 127.0.0.1:6379> SET tutorial1 redis
OK
redis 127.0.0.1:6379> SET tutorial2 mysql
OK
redis 127.0.0.1:6379> SET tutorial3 mongodb
OK
//Now, get a random key from Redis.
redis 127.0.0.1:6379> RANDOMKEY
1) tutorial3
Redis Rename command - Lệnh rename Key trong Redis
Redis RENAME command được sử dụng để rename một tên key.
Return value:
- OK
- error, nếu tên key cũ và key mới là giống nhau hoặc khi key không tồn tại. Nếu tên key mới là tồn tại, nó sẽ ghi đè vào key này.
Syntax:
redis 127.0.0.1:6379> RENAME OLD_KEY_NAME NEW_KEY_NAME
Ví dụ:
# First, create some keys in Redis and set some values in it.
redis 127.0.0.1:6379> SET tutorial1 redis
OK
# Now, rename the key ‘tutorial1’ to ‘new-tutorial’.
redis 127.0.0.1:6379> RENAME tutorial1 new-tutorial
OK
Redis Renamenx command - Lệnh rename Key trong Redis
Redis RENAMENX command được sử dụng để rename tên của key nếu tên này chưa được sử dụng.
Return value:
Integer reply:
- 1, nếu key được rename thành tên mới.
- 0, nếu tên key cần rename đã tồn tại.
Syntax:
redis 127.0.0.1:6379> RENAMENX OLD_KEY_NAME NEW_KEY_NAME
Ví dụ:
#First, create some keys in Redis and set some values in it.
redis 127.0.0.1:6379> SET tutorial1 redis
OK
redis 127.0.0.1:6379> SET tutorial2 mongodb
OK
#Now, rename the key ‘tutorial1’ to ‘new-tutorial’.
redis 127.0.0.1:6379> RENAMENX tutorial1 new-tutorial
(integer) 1
redis 127.0.0.1:6379> RENAMENX tutorial2 new-tutorial
(integer) 0
Redis Type command - Lệnh kiểm tra loại dữ liệu giá trị của Key trong Redis
Redis TYPE command được sử dụng để get kiểu data của giá trị được lưu trữ trong key.
Return value:
String, Data type giá trị or none.
Syntax:
redis 127.0.0.1:6379> TYPE KEY_NAME
Ví dụ:
#First, create some keys in Redis and set some values in it.
redis 127.0.0.1:6379> SET tutorial1 redis
OK
#Now, check the type of the key.
redis 127.0.0.1:6379> TYPE tutorial1
string