# Redis Cheatsheet # All the commands you need to know redis-server /path/redis.conf # start redis with the related configuration file redis-cli # opens a redis prompt # Strings. APPEND key value # append a value to a key BITCOUNT key [start end] # count set bits in a string SET key value # set value in key SETNX key value # set if not exist value in key MSET key value [key value ...] # set multiple keys to multiple values MSETNX key value [key value ...] # set multiple keys to multiple values, only if none of the keys exist GET key # get value in key GETRANGE key value # get a substring value of a key and return its old value MGET key [key ...] # get the values of all the given keys INCR key # increment value in key INCRBY key increment # increment the integer value of a key by the given amount INCRBYFLOAT key increment # increment the float value of a key by the given amount DECR key # decrement the integer value of key by one DECRBY key decrement # decrement the integer value of a key by the given number DEL key # delete key EXPIRE key 120 # key will be deleted in 120 seconds TTL key # returns the number of seconds until a key is deleted # Lists. # A list is a series of ordered values. RPUSH list value # puts the new value at the end of the list LPUSH list value # puts the new value at the start of the list LRANGE list 0 1 # gives a subset of the list LLEN list # returns the current length of the list LPOP list # removes the first element from the list and returns it RPOP list # removes the last element from the list and returns it # Sets. # A set is similar to a list, except it does not have a specific order and each element may only appear once. SADD myset value # adds the given value to the set SREM myset value # removes the given value from the set SISMEMBER myset value # tests if the given value is in the set. SMEMBERS myset # returns a list of all the members of this set SUNION myset otherset # combines two or more sets and returns the list of all elements # Sorted Sets # A sorted set is similar to a regular set, but now each value has an associated score. # This score is used to sort the elements in the set. ZADD mysset score value # adds the given value with the score to the set ZRANGE mysset 0 2 # returns a subset of the sorted set ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] # return a range of members in a sorted set, by score # Hashes # Hashes are maps between string fields and string values, so they are the perfect data type to represent objects. HSET user:1000 name "John Smith" # sets name to user #1000 HSET user:1000 email "john@smith.com" # sets email to user #1000 HGET user:1001 name # returns the name for the user #1000 HGETALL user:1000 # gets bak the saved data for the hash HMSET user:1001 name "John Smith" email "john@smith.com" # sets multiple fields at once HINCRBY user:1000 visits 1 # increments value in hash by X HDEL user:1000 visits # deletes value in hash