Simple Key Values

Redis can handle a few data types as the "value" in the "key-value" pair. In this section, we focus on simple value types which include strings and integers.

Old Yeller

SET

SET key value [EX seconds] [PX milliseconds] [NX|XX]

Options EX seconds -- Set the specified expire time, in seconds. PX milliseconds -- Set the specified expire time, in milliseconds. NX -- Only set the key if it does not already exists. XX -- Only set the key if it already exist.

SET Examples

Set a key/value pair:

redis> set dog "Old Yeller"
OK

Set a key/value pair to expire in 60 seconds:

redis> set dog "Old Yeller" EX 60
OK

Read more about the SET command in the Redis documentation.

GET

GET key

Get the value of key. If the key does not exist, the special value nil is returned. An error is returned if the value stored at key is not a string, because GET only handles string values.

GET Examples

Get the value of a key:

redis> get dog
"Old Yeller"

Get the value of a key and store it in a variable (from the shell prompt rather than the redis-cli prompt):

$ myDog=$(redis-cli get dog)
$ echo $myDog
hello

Read more about the GET command in the Redis documentation.

Additional Notes

In addition to strings, we can also use the get/set commands with numbers. For example:

redis> set dogMealsPerday 3
OK

results matching ""

    No results matching ""