Redis| Data structure in Redis

Introduction

Redis is a kind of NoSQL(Not only sql), which means redis is not a relation based database. Redis works on RAM so we always setup a redis server (not in the EE project) as a first level or second level database. In this post, I will include the five data structure that can be saved in Redis.

Five Data structrues in Redis

Imgur

  1. STRING Imgur
    • String in Redis is a k-v saving format, there are three basic actions to redis: GET, SET, DEL.
    • Imgur
  2. LIST: LIST is a bi-directional linkedlist, which is a container of elements. In the list, we can save duplicate strings. Imgur
    • LPUSH/RPUSH: Allow us to push string into the list from left and right end.
    • LPOP/RPOP: Allow us to pop string from left and right end.
    • LINDEX: Get the string on index in the list.
    • LRANGE: Return all of the elements in the given range. For example, lrange 0, -1 # 0 means the start of the list and -1 means the end of the current list. Imgur
  3. SET: Set data structure guaranteed that no dulicate string is contained. And very similary as set in Java, the strings saved in SET is unordered. Imgur
    • SADD: Add string into the set.
    • SREM: Remove string from the set.
    • SISMEMBER: Check if one string is saved in the set, same as set.containsKey() in java.
    • SMEMBERS: Show all members in current set. Since we need to traversal all strings in the set but we need to be very careful when using this command. Imgur
  4. HASH: Hash in redis represent the mapping between different Strings, Hash can be considered as a lite version of redis, and the values saved in hash can be either numbers or STRING. Same as hashMap, HASH in redis is unordered. Imgur
    • HSET: put k-v values into the HASH.
    • HGET: Get the value saved in HASH.
    • HGETALL: Get all k-v pairs saved in current HASH.
    • HDEL: Remove the element if given key exists in the HASH. Imgur
  5. ZSET: ZSET is very like HASH but it is sorted. Each element in the ZSET is a k-v pair and its key is a string while its value is a score(float). As a result, we can get value by key and get the key by value, which means both key and value in ZSET is unique. Imgur
    • ZADD: Add element into zset
    • ZRANGE: Get the elements that locate in the given range.
    • ZRANGEBYSCORE: Get the elements that locate in the given score range.
    • ZREM: Remove the element if it exists in the ZSET. Imgur

Reference

  1. Redis in Action