Try this tutorial
-
Set a key-value
SETSET server:name "fido" -
Get value of a key
GETGET server:name -
Check existing(1: true, 0 false)
EXISTSEXISTS server:name⇒ 1EXISTS server:aaa⇒0 -
Increment by 1
INCRSET c 1INCR c⇒ 2 -
Increment by x
INCRBYINCRBY c 10⇒ 12INCRandINCRBYare atomic operation, so don’t need to care about concurrence -
Set Expire
EXPIRESET resource:lock "Redis Demo"
EXPIRE resource:lock 120then the key will expire after 120 secondscheck expire time
TTL resource:lockwill return how many seconds before the key will be deletedTTL 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 20SET resource:lock "2"TTL resource:lock⇒ -1 -
Cancel Expire Time
PERSISTPERSIST resource:lock -
Combine the SET and EXPIRES together
SET xxx EX nSET resource:lock "Redis Demo 3" EX 5 -
List Operation
LPUSHRPUSHLLENLRANGELPOPRPOP-
Put new element at the end of the list
RPUSHRPUSH friends "Alice"RPUSH friends "Bob" -
Put new element at the start of the list
LPUSHLPUSH friends "Lucy" -
Show elements in list
LRANGELRANGE friends 0 -1⇒ 1) “Lucy” 2) “Alice” 3) “Bob” -
Pop(means remove from list and return) first element
LPOPLPOP friends⇒ “Lucy” -
Pop last element
RPOPRPOP friends⇒ “Bob” -
Append multiple element
RPUSHRPUSH friends 1 2 3⇒ 1) “Alice” 2) “1” 3) “2” 4) “3” -
Insert multiple element at the start of the list
LPUSHLPUSH friends 0⇒ 1) 0 2) “Alice” 3) “1” 4) “2” 5) “3” -
Get Length of the list
LLENLLEN friends⇒ 5
-
-
Set Operation
SADDSREMSISMEMBERSMEMBERSSUNIONSPOPSRANDMEMBERSet 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 return0, otherwise1SADD superpowers "a" "b" "c" -
Remove one element from Set
SREM, will return0(didn’t remove) or1(remove success)SREM superpowers "a"⇒1SREM superpowers "d"⇒0 (d doesn’t exist) -
Test element exist in Set
SISMEMBER, will return 0(not exist) or 1(exist)SISMEMBER superpowers "b"⇒ 1SISMEMBER superpowers "d"⇒ 0 -
Return a list including all elements in the Set
SMEMBERSSMEMBERS 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 onceSADD newset 1SADD newset 2SADD 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
SPOPBecause Set are not ordered, the returned (and removed) elements are totally casual in this case
SADD letters a b c d e fSPOP letters 3⇒ e f d(not fixed) -
Return 1 or more element(s) from Set
SRANDMEMBERSRANDMEMBER letters 3⇒ f c e(not fixed)
-
-
Sorted Set Operation(After Redis v1.2)
ZADDZRANGE-
Add 1 or more element to Sorted Set
ZADD [key] [score] [member]ZADD t 2 AZADD t 1 bZRANGE 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”
-