Content
Set and retrieve a string value
- Set a string value with key “name” by
SET
- Retrieve the value of key “name” by
GET
127.0.0.1:6379> SET name "Ivy"
OK
127.0.0.1:6379> GET name
"Ivy"
Append a value to an existing string
- Set an initial string value by
SET
- Add additional text to the string by
APPEND
- Retrieve the updated string value by
GET
127.0.0.1:6379> SET message "Redis " # the key is "message"
OK
127.0.0.1:6379> APPEND message "In Action"
(integer) 15
127.0.0.1:6379> GET message
"Redis In Action"
Set multiple string values
- Set multiple string values and numeric values at once by
MSET
- Retrieve the values of multiple keys at once by
MGET
127.0.0.1:6379> MSET country "Germany" name "Ivy" profession "Cloud Architect" age 25
OK
127.0.0.1:6379> MGET country name profession age
1) "Germany"
2) "Ivy"
3) "Cloud Architect"
4) "25"
Set and update a numeric value
- Set an initial value for a key called salary by
SET
- Increment the salary by 1000 by
INCRBY
- Increment the salary with float by
INCRBYFLOAT
- Retrieve the updated value by
GET
127.0.0.1:6379> SET salary 80000
OK
127.0.0.1:6379> INCRBY salary 1000
(integer) 81000
127.0.0.1:6379> INCRBYFLOAT salary 1.5
"81001.5"
127.0.0.1:6379> GET salary
"81001.5"
Set a TTL (expiration time) to a string value
- Set a string value with a TTL (time to live) of 10 seconds
- Check the remaining time to live for the key
- After 10 seconds, the key will expire
127.0.0.1:6379> SETEX tempo_value 10 "it expires soon"
OK
127.0.0.1:6379> GET tempo_value
"it expires soon"
127.0.0.1:6379> TTL tempo_value
(integer) 3
127.0.0.1:6379> GET tempo_value
(nil)
Delete Keys
- List all keys
- Delete one key
- Delete multiple keys at once
127.0.0.1:6379> KEYS * # list all keys
1) "country"
2) "name"
3) "message"
4) "salary"
5) "age"
6) "profession"
127.0.0.1:6379> DEL name # delete one key
(integer) 1
127.0.0.1:6379> DEL age country salary # delete multiple keys
(integer) 3
127.0.0.1:6379> KEYS * # check the deletion
1) "message"
2) "profession"