Thursday, May 7, 2015

Some useful Redis commands - quick view

SET foo bar
GET foo
-----------------
x = GET foo
x = x+1
SET foo 1 // chance of misleading

SET is not synchronized, so use INCR for safer side
INCR x
-----------------
LPUSH mylist a (now mylist holds 'a')
LPUSH mylist b (now mylist holds 'b','a')
RPUSH mylist c (now mylist holds 'b','a','c')
LPUSH mylist d
LPUSH mylist e
LPUSH mylist f (now mylist holds 'f','e','d','b','a','a)
LRANGE mylist 0 1 => b,a
LRANGE mylist 2 4 => d,b,a
LRANGE mylist 3 -1 => b,a,a // index -1 means till end
LLEN mylist => 6

-----------------
*** If you want to maintain order of insertion then use LIST other wise use SET
*** List can have duplicates
*** Set don't have duplicates and doesn't maintain insertion order
*** ZSet : Sorted Sets with score(priority)
-----------------
SADD is the add to set operation
SREM is the remove from set operation
SISMEMBER is the test if member operation
SINTER is the perform intersection operation
SCARD to get the cardinality (the number of elements)
SMEMBERS to return all the members of a Set
---
SADD myset a
SADD myset b
SADD myset foo
SADD myset bar
SCARD myset => 4
SMEMBERS myset => bar,a,foo,b //  Not the order in which we inserted
---
SADD mynewset b
SADD mynewset foo
SADD mynewset hello
SINTER myset mynewset => foo,b // intersection between myset mynewset
-----------------

-----------------
ZADD zset 10 a
ZADD zset 5 b
ZADD zset 12.55 c
ZRANGE zset 0 -1 => b,a,c

ZSCORE zset a => 10 // returns zscore
ZSCORE zset XKXK => NUNLL // return null if key not existed
-----------------
HMSET myuser name Salvatore surname Sanfilippo country Italy // Sets key values of a variable
HGET myuser surname => Sanfilippo // Retrieves key value
HEXISTS is key exist
HINCRBY increment an hash field

-----------------
more: http://redis.io/commands
http://redis.io/topics/twitter-clone

No comments:

Post a Comment