0x01 Introduce

Redis is an open source(BSD licensed), in-memory data structure store,used as database,cache and message broker. It supports data structures such as strings,hashes,lists,sets,sorted sets with range queries,bitmaps,hyperloglogs and geospatial indexes with redius queries.

0x02 Quickstart

  1. keys/strings

    1
    2
    3
    4
    127.0.0.1:6379> set key _k
    OK
    127.0.0.1:6379> get key
    "_k"
  2. hash

    1
    2
    3
    4
    5
    6
    7
    8
    9
    127.0.0.1:6379> HMSET LGrok name "LG" description  "human" likes "hack"
    OK
    127.0.0.1:6379> HGETALL LGrok
    1) "name"
    2) "LG"
    3) "description"
    4) "human"
    5) "likes"
    6) "hack"
  3. lists

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    127.0.0.1:6379> LPUSH List 1
    (integer) 1
    127.0.0.1:6379> LPUSH List 2
    (integer) 2
    127.0.0.1:6379> LPUSH List 3
    (integer) 3
    127.0.0.1:6379> LPUSH List 4
    (integer) 4
    127.0.0.1:6379> LRANGE List 0 1
    1) "4"
    2) "3"
    127.0.0.1:6379> LRANGE List 0 3
    1) "4"
    2) "3"
    3) "2"
    4) "1"
  4. sets

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    127.0.0.1:6379> SADD Lset set1
    (integer) 1
    127.0.0.1:6379> SADD Lset set2
    (integer) 1
    127.0.0.1:6379> SADD Lset set3
    (integer) 1
    127.0.0.1:6379> SADD Lset set4
    (integer) 1
    127.0.0.1:6379> SMEMBERS Lset
    1) "set4"
    2) "set2"
    3) "set1"
    4) "set3"
  5. sorted sets

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    127.0.0.1:6379> ZADD sset 1 first
    (integer) 1
    127.0.0.1:6379> ZADD sset 2 second
    (integer) 1
    127.0.0.1:6379> ZADD sset 3 third
    (integer) 1
    127.0.0.1:6379> zrange sset 0 1
    1) "first"
    2) "second"
    127.0.0.1:6379> zrange sset 0 1 withscores
    1) "first"
    2) "1"
    3) "second"
    4) "2"
  6. HyperLogLog
    Use for the cardinality estimate(基数估计).

    1
    2
    3
    4
    127.0.0.1:6379> pfadd testset a b c d e f g g
    (integer) 1
    127.0.0.1:6379> pfcount testset
    (integer) 7
  7. Backup
    “SAVE” will backup the database into path of Redis.

  8. Restore
    Move the bakcup file into the path of Redis then restart the Redis Server. You can use “config get dir” for the path.