Skip to content

Redis Data Type : String


Set and retrieve a string value

  • Set a string value with key “name” by SET
  • Retrieve the value of key “name” by GET> SET name "Ivy"
OK> GET name

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> SET message "Redis "   # the key is "message"
OK> APPEND  message  "In Action"
(integer) 15> 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> MSET country "Germany" name "Ivy"  profession "Cloud Architect" age 25
OK> 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> SET salary  80000
OK> INCRBY salary  1000
(integer) 81000> INCRBYFLOAT salary  1.5
"81001.5"> GET salary

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> SETEX tempo_value 10 "it expires soon"
OK> GET tempo_value
"it expires soon"> TTL tempo_value
(integer) 3> GET tempo_value

Delete Keys

  • List all keys
  • Delete one key
  • Delete multiple keys at once> KEYS *   # list all  keys
1) "country"
2) "name"
3) "message"
4) "salary"
5) "age"
6) "profession"> DEL name   # delete one key
(integer) 1> DEL age country salary    # delete multiple keys
(integer) 3> KEYS *     # check the deletion
1) "message"
2) "profession"