e-Book - Redis in Action

This book covers the use of Redis, an in-memory database/data structure server.
  • Foreword
  • Preface
  • Acknowledgments
  • About this Book
  • About the Cover Illustration
  • Part 1: Getting Started
  • Part 2: Core concepts
  • Part 3: Next steps
  • Appendix A
  • Appendix B
  • Buy the paperback

    1.2.1 Strings in Redis

    Figure 1.1An example of a STRING, world, stored under a key, hello

    In Redis, STRINGs are similar to strings that we see in other languages or other key-value stores. Generally, when I show diagrams that represent keys and values, the diagrams have the key name and the type of the value along the top of a box, with the value inside the box. I’ve labeled which part is which as an example in figure 1.1, which shows a STRING with key hello and value world.

    The operations available to STRINGs start with what’s available in other key-value stores. We can GET values, SET values, and DEL values. After you have installed and tested Redis as described in appendix A, within redis-cli you can try to SET, GET, and DEL values in Redis, as shown in listing 1.1, with the basic meanings of the functions described in table 1.3.

    Table 1.3 Commands used on STRING values


    What it does


    Fetches the data stored at the given key


    Sets the value stored at the given key


    Deletes the value stored at the given key (works for all types)

    Listing 1.1 An example showing the SET, GET, and DEL commands in Redis
    $ redis-cli

    Start the redis-cli client up.

    redis> set hello world

    Set the key hello to the value world.


    If a SET command succeeds, it returns OK, which turns into Trueon the Python side.

    redis> get hello

    Now get the value stored at the key hello.


    It’s still world, like we just set it.

    redis> del hello

    Let’s delete the key-value pair.

    (integer) 1

    If there was a value to delete, DEL returns the number of items that were deleted.

    redis> get hello

    There’s no more value, so trying to fetch the value returns nil, which turns into None on the Python side.

    Using Redis-CLI In this first chapter, I introduce Redis and some commands using the redis-cli interactive client that comes with Redis. This allows you to get started interacting with Redis quickly and easily.

    In addition to being able to GET, SET, and DEL STRING values, there are a handful of other commands for reading and writing parts of STRINGs, and commands that allow us to treat strings as numbers to increment/decrement them. We’ll talk about many of those commands in chapter 3. But we still have a lot of ground to cover, so let’s move on to take a peek at LISTs and what we can do with them.