Skip to content

Redis Basic

Try this tutorial

http://try.redis.io/

  • Set a key-value SET

    SET server:name "fido"

  • Get value of a key GET

    GET server:name

  • Check existing(1: true, 0 false) EXISTS

    EXISTS server:name⇒ 1

    EXISTS server:aaa⇒0

  • Increment by 1 INCR

    SET c 1

    INCR c ⇒ 2

  • Increment by x INCRBY

    INCRBY c 10 ⇒ 12

    INCR and INCRBY are atomic operation, so don’t need to care about concurrence

  • Set Expire EXPIRE

    SET resource:lock "Redis Demo"
    EXPIRE resource:lock 120 then the key will expire after 120 seconds

    check expire time

    TTL resource:lock will return how many seconds before the key will be deleted

    TTL will return -2 when key not exist

    when key never expires, the TTL will return -1

    when key set expires and set again, it will never be expired

    SET resource:lock "1"

    EXPIRES resource:lock 20

    SET resource:lock "2"

    TTL resource:lock ⇒ -1

  • Cancel Expire Time PERSIST

    PERSIST resource:lock

  • Combine the SET and EXPIRES together SET xxx EX n

    SET resource:lock "Redis Demo 3" EX 5

  • List Operation LPUSH RPUSH LLEN LRANGE LPOP RPOP

    • Put new element at the end of the list RPUSH

      RPUSH friends "Alice"

      RPUSH friends "Bob"

    • Put new element at the start of the list LPUSH

      LPUSH friends "Lucy"

    • Show elements in list LRANGE

      LRANGE friends 0 -1 ⇒ 1) “Lucy” 2) “Alice” 3) “Bob”

    • Pop(means remove from list and return) first element LPOP

      LPOP friends ⇒ “Lucy”

    • Pop last element RPOP

      RPOP friends ⇒ “Bob”

    • Append multiple element RPUSH

      RPUSH friends 1 2 3 ⇒ 1) “Alice” 2) “1” 3) “2” 4) “3”

    • Insert multiple element at the start of the list LPUSH

      LPUSH friends 0 ⇒ 1) 0 2) “Alice” 3) “1” 4) “2” 5) “3”

    • Get Length of the list LLEN

      LLEN friends ⇒ 5

  • Set Operation SADD SREM SISMEMBER SMEMBERS SUNION SPOP SRANDMEMBER

    Set does not have a specific order and each element may only appear once.

    • Add one or more element(s) to Set SADD, add an existing element will return 0, otherwise 1

      SADD superpowers "a" "b" "c"

    • Remove one element from Set SREM, will return 0(didn’t remove) or 1(remove success)

      SREM superpowers "a" ⇒1

      SREM superpowers "d" ⇒0 (d doesn’t exist)

    • Test element exist in Set SISMEMBER, will return 0(not exist) or 1(exist)

      SISMEMBER superpowers "b" ⇒ 1

      SISMEMBER superpowers "d" ⇒ 0

    • Return a list including all elements in the Set SMEMBERS

      SMEMBERS superpowers ⇒ 1) “c” 2)“b”

    • Combine two or more sets and return a list of all elements SUNION, element existing in multi sets only return once

      SADD newset 1

      SADD newset 2

      SADD newset "b"

      SUNION newset superpowers ⇒ 1) “c” 2) “1” 3) “b” 4) “2” (order not fixed)

    • Remove and Return 1 or more element(s) from Set SPOP

      Because Set are not ordered, the returned (and removed) elements are totally casual in this case

      SADD letters a b c d e f

      SPOP letters 3 ⇒ e f d(not fixed)

    • Return 1 or more element(s) from Set SRANDMEMBER

      SRANDMEMBER letters 3 ⇒ f c e(not fixed)

  • Sorted Set Operation(After Redis v1.2) ZADD ZRANGE

    • Add 1 or more element to Sorted Set ZADD [key] [score] [member]

      ZADD t 2 A

      ZADD t 1 b

      ZRANGE t 0 -1 ⇒ 1) “b” 2) “A”

      if element already exist, then update the score

    • List the element in a sorted Set

      ZRANGE t 0 -1 ⇒ 1) “b” 2) “A”

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x