Các thao tác cơ bản với chuỗi trong Redis

Thiết lập giá trị cho một khóa

Lệnh: set

Cú pháp: set key value

127.0.0.1:6379> set username john_doe
OK

Lấy giá trị của một khóa

Lệnh: get

Cú pháp: get key

127.0.0.1:6379> get username
"john_doe"

Lấy chuỗi con trong khoảng chỉ định

Lệnh: getrange

Cú pháp: getrange key start end

Giải thích: start là vị trí bắt đầu, end là vị trí kết thúc, chỉ số bắt đầu từ 0

127.0.0.1:6379> GETRANGE username 2 4
"hn_"
127.0.0.1:6379> getrange username 0 2
"joh"
127.0.0.1:6379> getrange username 4 7
"doe"
127.0.0.1:6379> getrange username 1 5
"ohn_d"

Thiết lập giá trị mới và trả về giá trị cũ

Lệnh: getset

Cú pháp: getset key value

127.0.0.1:6379> getset username jane_doe
"john_doe"
127.0.0.1:6379> get username
"jane_doe"

Lấy bit tại vị trí chỉ định

Lệnh: getbit

Cú pháp: getbit key offset

Giả định username hiện có giá trị "jane_doe", ký tự đầu "j" có mã ASCII nhị phân là 0110 1010:

127.0.0.1:6379> getbit username 0
(integer) 0
127.0.0.1:6379> getbit username 1
(integer) 1
127.0.0.1:6379> getbit username 2
(integer) 1
127.0.0.1:6379> getbit username 3
(integer) 0
127.0.0.1:6379> getbit username 4
(integer) 1
127.0.0.1:6379> getbit username 5
(integer) 0
127.0.0.1:6379> getbit username 6
(integer) 1
127.0.0.1:6379> getbit username 7
(integer) 0

Lấy giá trị của nhiều khóa

Lệnh: mget

Cú pháp: mget key1 [key2] ...

127.0.0.1:6379> set product_id 1001
OK
127.0.0.1:6379> set category electronics
OK
127.0.0.1:6379> set price 299.99
OK
127.0.0.1:6379> mget product_id category price
1) "1001"
2) "electronics"
3) "299.99"

Thiết lập bit tại vị trí chỉ định

Lệnh: setbit

Cú pháp: setbit key offset value

Giả định username hiện có giá trị "jane_doe", ký tự đầu "j" có mã ASCII nhị phân là 0110 1010, nếu thiết lập bit thứ 5 thành 1, thì giá trị sẽ là 0110 1110, tương ứng với ký tự "n"

127.0.0.1:6379> get username
"jane_doe"
127.0.0.1:6379> setbit username 5 1
(integer) 0
127.0.0.1:6379> get username
"nane_doe"

Thiết lập giá trị với thời gian hết hạn (giây)

Lệnh: setex

Cú pháp: setex key seconds value

127.0.0.1:6379> setex session_token 3600 abc123xyz
OK
127.0.0.1:6379> get session_token
"abc123xyz"
127.0.0.1:6379> get session_token
"abc123xyz"
//Sau 1 giờ
127.0.0.1:6379> get session_token
(nil)

Thiết lập giá trị chỉ khi khóa chưa tồn tại

Lệnh: setnx

Cú pháp: setnx key value

127.0.0.1:6379> get username
"nane_doe"
127.0.0.1:6379> setnx username new_user
(integer) 0
127.0.0.1:6379> get username
"nane_doe"
127.0.0.1:6379> setnx user_email user@example.com
(integer) 1
127.0.0.1:6379> get user_email
"user@example.com"

Thay thế giá trị từ vị trí offset

Lệnh: setrange

Cú pháp: setrange key offset value

127.0.0.1:6379> set username john_doe
OK
127.0.0.1:6379> get username
"john_doe"
127.0.0.1:6379> setrange username 4 smith
(integer) 9
127.0.0.1:6379> get username
"johnsmith"
127.0.0.1:6379> setrange username 4 williams
(integer) 11
127.0.0.1:6379> get username
"johnwilliams"

Lấy độ dài của chuỗi

Lệnh: strlen

Cú pháp: strlen key

127.0.0.1:6379> get username
"johnwilliams"
127.0.0.1:6379> strlen username
(integer) 11

Thiết lập nhiều cặp key-value cùng lúc

Lệnh: mset

Cú pháp: mset key value [key value] ....

127.0.0.1:6379> mset user_id 1001 user_name alice user_role admin
OK
127.0.0.1:6379> mget user_id user_name user_role
1) "1001"
2) "alice"
3) "admin"

Thiết lập nhiều cặp key-value chỉ khi tất cả các khóa chưa tồn tại

Lệnh: msetnx

Cú pháp: msetnx key value [key value] ....

127.0.0.1:6379> mset product_id 2002 product_name phone product_price 599
OK
127.0.0.1:6379> mget product_id product_name product_price
1) "2002"
2) "phone"
3) "599"
127.0.0.1:6379> msetnx product_id 3003 product_name laptop product_price 999 product_weight 2.5
(integer) 0
127.0.0.1:6379> mget product_id product_name product_price product_weight
1) "2002"
2) "phone"
3) "599"
4) (nil)
127.0.0.1:6379> msetnx order_id 5001 order_date today order_status pending
(integer) 1
127.0.0.1:6379> mget product_id product_name product_price product_weight order_id order_date order_status
1) "2002"
2) "phone"
3) "599"
4) (nil)
5) "5001"
6) "today"
7) "pending"

Thiết lập giá trị với thời gian hết hạn (mili giây)

Lệnh: psetex

Cú pháp: psetex key ms value

127.0.0.1:6379> psetex verification_code 50000 123456
OK
127.0.0.1:6379> get verification_code
"123456"
127.0.0.1:6379> get verification_code
"123456"
//Sau 50 giây
127.0.0.1:6379> get verification_code
(nil)

Tăng giá trị số của khóa lên 1

Lệnh: incr

Cú pháp: incr key

127.0.0.1:6379> set visitor_count 100
OK
127.0.0.1:6379> get visitor_count
"100"
127.0.0.1:6379> incr visitor_count
(integer) 101
127.0.0.1:6379> get visitor_count
"101"
127.0.0.1:6379> incr visitor_count
(integer) 102
127.0.0.1:6379> get visitor_count
"102"

Tăng giá trị của khóa theo một lượng chỉ định

Lệnh: incrby

Cú pháp: incrby key increment

127.0.0.1:6379> get visitor_count
"102"
127.0.0.1:6379> incrby visitor_count 50
(integer) 152
127.0.0.1:6379> get visitor_count
"152"

Tăng giá trị của khóa theo một lượng số thực

Lệnh: incrbyfloat

Cú pháp: incrbyfloat key increment

127.0.0.1:6379> set balance 1000.50
OK
127.0.0.1:6379> incrbyfloat balance 250.75
"1251.25"
127.0.0.1:6379> get balance
"1251.25"

Giảm giá trị số của khóa xuống 1

Lệnh: decr

Cú pháp: decr key

127.0.0.1:6379> get visitor_count
"152"
127.0.0.1:6379> decr visitor_count
(integer) 151
127.0.0.1:6379> get visitor_count
"151"
127.0.0.1:6379> decr visitor_count
(integer) 150
127.0.0.1:6379> get visitor_count
"150"

Giảm giá trị của khóa theo một lượng chỉ định

Lệnh: decrby

Cú pháp: decrby key decrement

127.0.0.1:6379> get visitor_count
"150"
127.0.0.1:6379> decrby visitor_count 30
(integer) 120
127.0.0.1:6379> get visitor_count
"120"

Thêm giá trị vào cuối chuỗi

Lệnh: append

Cú pháp: append key value

127.0.0.1:6379> set greeting hello
OK
127.0.0.1:6379> get greeting
"hello"
127.0.0.1:6379> append greeting world
(integer) 10
127.0.0.1:6379> get greeting
"helloworld"
127.0.0.1:6379> set version 1.0
OK
127.0.0.1:6379> get version
"1.0"
127.0.0.1:6379> append version .1
(integer) 4
127.0.0.1:6379> get version
"1.01"
127.0.0.1:6379> incrbyfloat version 0.5
"1.51"
127.0.0.1:6379> get version
"1.51"
127.0.0.1:6379> append version beta
(integer) 8
127.0.0.1:6379> get version
"1.51beta"

Thẻ: Redis chuỗi thao tác cơ bản lệnh Redis dữ liệu Redis

Đăng vào ngày 29 tháng 6 lúc 22:44