Skip to main content
Engineering LibreTexts

14.4: Redis data types

  • Page ID
    12816
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Redis is basically a map from keys, which are strings, to values, which can be one of several data types. The most basic Redis data type is a string. I will write Redis types in italics to distinguish them from Java types.

    To add a string to the database, use jedis.set, which is similar to Map.put; the parameters are the new key and the corresponding value. To look up a key and get its value, use jedis.get:

    jedis.set("mykey", "myvalue");
    String value = jedis.get("mykey");
    

    In this example, the key is "mykey" and the value is "myvalue".

    Redis provides a set structure, which is similar to a Java Set<String>. To add elements to a Redis set, you choose a key to identify the set and then use jedis.sadd:

    jedis.sadd("myset", "element1", "element2", "element3");
    boolean flag = jedis.sismember("myset", "element2");
    

    You don’t have to create the set as a separate step. If it doesn’t exist, Redis creates it. In this case, it creates a set named myset that contains three elements.

    The method jedis.sismember checks whether an element is in a set. Adding elements and checking membership are constant time operations.

    Redis also provides a list structure, which is similar to a Java List<String>. The method jedis.rpush adds elements to the end (right side) of a list:

    jedis.rpush("mylist", "element1", "element2", "element3");
    String element = jedis.lindex("mylist", 1);
    

    Again, you don’t have to create the structure before you start adding elements. This example creates a list named “mylist” that contains three elements.

    The method jedis.lindex takes an integer index and returns the indicated element of a list. Adding and accessing elements are constant time operations.

    Finally, Redis provides a hash structure, which is similar to a Java Map<String, String>. The method jedis.hset adds a new entry to the hash:

    jedis.hset("myhash", "word1", Integer.toString(2));
    String value = jedis.hget("myhash", "word1");
    

    This example creates a hash named myhash that contains one entry, which maps from the key word1 to the value "2".

    The keys and values are strings, so if we want to store an Integer, we have to convert it to a String before we call hset. And when we look up the value using hget, the result is a String, so we might have to convert it back to Integer.

    Working with Redis hashes can be confusing, because we use a key to identify which hash we want, and then another key to identify a value in the hash. In the context of Redis, the second key is called a “field”, which might help keep things straight. So a “key” like myhash identifies a particular hash, and then a “field” like word1 identifies a value in the hash.

    For many applications, the values in a Redis hash are integers, so Redis provides a few special methods, like hincrby, that treat the values as numbers:

    jedis.hincrBy("myhash", "word2", 1);
    

    This method accesses myhash, gets the current value associated with word2 (or 0 if it doesn’t already exist), increments it by 1, and writes the result back to the hash.

    Setting, getting, and incrementing entries in a hash are constant time operations.

    You can read more about Redis data types at thinkdast.com/redistypes.


    This page titled 14.4: Redis data types is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .

    • Was this article helpful?