REDIS Cheatsheet - adonisv79/bytecommander.com GitHub Wiki
All commands can be found in Redis Website
Commands
Basic key value pair
SET {varname} {value} // sets a variable
SET foo 100
GET {varname} // gets a value
GET foo
// returns "100"
INCR {varname} // increments a variable
INCR foo
GET foo
// returns "101"
DECR {varname} // decrements a variable
INCR foo
GET foo
// returns "101"
EXISTS {varname} // returns 1 if variable exists, else 0
EXISTS foo
// returns 1 (integer)
DEL {varname} // deletes a variable
SET bar 250
DEL bar
GET bar
// returns (nil)
FLUSHALL // deletes all variables
FLUSHALL
GET foo
// returns (nil)
EXPIRE {varname} // adds TTL (in seconds) to a variables
SET foo 100
EXPIRE FOO 10
GET foo
// returns 100
TTL {varname} // gets the TTL (in seconds) for a variable. If the value is -2 it is expired. -1 means it never expires
TTL foo
// depending on when we set the EXPIRE, result will for example give 2 (integer) if it has passed 8 seconds from above
SETEX {varname} {ttl} {value} //sets a value with expiration
SETEX foo 30 "Hello world"
PERSIST {varname} // removes TTL in a variable
PERSIST foo
MSET { {varname} {value} } {...} //multiple setting of variables
MSET key1 "Hello" key2 "World"
APPEND {varname} {value} // appends values to the variables
APPEND key1 " World"
GET key1
// returns "Hello World"
RENAME {varname} {new name} // change a variable name
RENAME key1 greetings
Data Types
String
As seen above, strings act like normal string in any language
integer
As seen above, strings act like normal string in any language
List
List is basically an array of strings ordered by insertion. We can push to the Head (left) or the tail (right) of both array ends.
LPUSH {listname} {value} // adds a value at the head of the list
RPUSH {listname} {value} // adds a value at the tail of the list
LPUSH mylist a // list is now "a"
LPUSH mylist b // list is now "b","a"
RPUSH mylist c // list is now "b","a", "c"
LRANGE {listname} {From Index} {To Index} // gets the values from a list from index to the next index
LRANGE mylist 1 -1
//gets all items starting at index 1
LRANGE mylist 0 1
// gets values from index 0 to 1
LLEN {listname} // gives the size/length of array
LLEN mylist
// returns 3
LPOP {listname} // pops item out of a list from the left (head)
RPOP {listname} // pops item out of a list from the right (tail)
LPOP mylist
// returns 'b'. mylist is now just 'a', 'c'
LINSERT {listname} // inserts item out of a list from the left (head)
RINSERT {listname} // inserts item out of a list from the right (tail)
SETS
Unordered collection of strings that ensures all items entries are unique. adding an item which already exxists will not add it.
SADD {setname} {Value} // adds an item to set
SADD countrycodes "PH"
SADD countrycodes "US"
SADD countrycodes "JP"
SISMEMBER {setname} {value} // validates if a member exists by responding 1 = true or 0= = false
SISMEMBER countrycodes "JP"
// returns 1 (integer)
SMEMBERS {setname} // retrieve the list of set items
SMEMBERS countrycodes
// returns "PH", "US", "JP"
SCARD {setname} // retrieves the cardinality (number of elements) in a set
SCARD countrycodes
// returns 3 (integer)
SMOVE {setname-from} {setname-to} {value} // moves an element from a set to another
SMOVE countrycodes asiancountrycodes "PH"
SMOVE countrycodes asiancountrycodes "JP"
SMEMBERS countrycodes
// returns only "US"
SREM {setname} {value} // removes a value from a set
SREM asiancountrycodes "JP"
SORTED SETS
Much like sets but allows you to specify the z-order (index)
HASH
Creates a key value pair collection much like a JSON object (hashtable)
HSET {hashname} {key} {value} // sets value of a hash
HSET user:adonis fullname "Adonis Lee Villamor"
HSET user:adonis email "[email protected]"
HGET {hashname} {key} // retrieves the value of a key in hash table
HGET user:adonis fullname
// returns "Adonis Lee Villamor"
HGETALL {hashname} {key} // retrieves all the key and values in hash table
HGETALL user:adonis
// returns "fullname", "Adonis Lee Villamor", "email", "[email protected]"
HMSET {hashname} {{key} {value}} {...} // sets multiple keys and vaalues in the hash
HMSET user:saitomohito fullname "Sai Tomohito" email "[email protected]" age 49