Get startedGet started for free

Storing and retrieving key-value data

1. Storing and retrieving key-value data

Now that we've explored the basics of key-value databases and Redis, it's time to learn more about storing and retrieving this data.

2. Storing key-value data

After connecting to the Redis cluster or server, we can use the dot-set method to store a key-value pair. First, a key is passed to the dot-set method, followed by the value stored with that key. In this first example, the value "JDoe" is stored at the key "username". In addition to string values, values of type int and float can also be passed to the dot-set method. Here, the value 27 is stored at the "age" key. However, this value is then converted to a string before being stored with Redis. With Redis, we can also overwrite existing key-value pairs. Here, we overwrite the value stored at the "username" key with the value "BSmith". This is convenient, especially when working with rapidly changing data.

3. Retrieving key-value data

Retrieving data from Redis is quite similar to retrieving data from a Python dictionary. To retrieve data, we'll use the dot-get method provided by Redis. This method takes a key, and if the key exists, it returns the corresponding value. Here, we use the set "JDoe" to be stored at the key "username". Then, we use the dot-get method to retrieve this value by passing "username" to it. The result is the value "JDoe". In our second example, we illustrate data being overwritten with Redis. Here, the value "BSmith" is stored to the key "username", overwriting the original value of "JDoe". Then, this value is retrieved and printed using the dot-get method. This example illustrates both the power of the dot-get method, as well as the ability to overwrite data with Redis. If a key-value pair does not exist and that key is passed to the dot-get method, None will be returned. In this last example, we try to access the key favorite-color. However, since this key does not exist, the value None is returned and output. This is similar to the behavior of the dot-get method when working with Pythonic dictionaries.

4. Storing complex key-value data

As well as being able to store simple data types, such as strings, ints, and floats, Redis can store more complex data types, like Python dictionaries. To do this, we'll use the dot-hset method, which takes a key (of type string), and a value, which takes the form of a Python dictionary. On the upper left, we store a dictionary that contains three key-value pairs to the "shopping_cart" key using the dot-hset method. To retrieve this value, we'll use the dot-hgetall method. This method takes a key and returns the mapping that was stored at that key. While different from the traditional dot-set and dot-get methods, the dot-hset and dot-hgetall methods allow for more complex data types to be stored using Redis.

5. Let's practice!

Fantastic work! We've explored a few techniques to store and retrieve data from Redis using the redis library in Python. Time to reinforce these tools with a few exercises!