0% found this document useful (0 votes)
218 views

Redis Cheatsheet

This document provides a cheatsheet for common Redis commands. It summarizes commands for generic keys like EXISTS and DEL, as well as commands for Redis data types including strings, lists, sets, and transactions. Multi-step transactions in Redis allow executing multiple commands atomically by using MULTI, EXEC and DISCARD.

Uploaded by

foobarbaz-2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
218 views

Redis Cheatsheet

This document provides a cheatsheet for common Redis commands. It summarizes commands for generic keys like EXISTS and DEL, as well as commands for Redis data types including strings, lists, sets, and transactions. Multi-step transactions in Redis allow executing multiple commands atomically by using MULTI, EXEC and DISCARD.

Uploaded by

foobarbaz-2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

REDIS cheatsheet [v2.

0]
starting the server cd redis; ./redis-server
running the client ./redis-cli <command>
(or with no command, to enter interactive mode)

commands
Test if specified key exists.
exists key Return: 1 if exists, 0 if not
Remove the specified keys.
del key1 key2 ... keyN Return: integer > 0 if keys removed, 0 if none of the keys existed
Return the type of the value stored at key, as a string.
type key Return: "none", "string", "list", "set"
Return all keys matching pattern. Ex: keys h*llo, keys h?llo, keys h[aeo]llo
generic commands for all types

keys pattern Return: bulk reply string with keys separated by spaces
Return a randomly-selected key from the current database.
randomkey Return: the selected key, or empty string if database is empty
rename oldkey newkey Atomically renames key. renamenx fails if newkey exists and returns 0.
renamenx oldkey newkey Return: 1 if OK, 0 if oldkey doesn't exist or if it equals newkey
Returns the number of keys in the current database.
dbsize Return: integer, the number of keys
expire key seconds Sets timeout on the specified key.
expireat key unixtime Return: 1 if timeout set, 0 if key already has a timeout or doesn't exist
Returns remaining time to live, in seconds, for a key with EXPIRE set.
ttl key Return: integer number of seconds, or -1 if key doesn't exist or has no expiration
Selects a database by index (zero-based). Default database is 0.
select db-index Return: 1 if OK, 0 if error
Moves a key from current database to specified database.
move key db-index Return: 1 if OK, 0 if key doesn't exist or is already present in the target database

set key value Sets the value of key to the string value; setnx will not overwrite an existing value.
setnx key value Return: 1 if OK, 0 if error
Gets the value of key.
get key Return: string value if OK, "nil" if key does not exist
commands for strings

Atomically sets the value of key to the string value and returns old value of key.
getset key value Return: value of key prior to the new value being set ("nil" if key did not exist)
Gets the values of all specified keys.
mget key1 key2 ... keyN Return: multi-bulk reply of all values, with "nil" for any keys that do not exist

mset key1 value1 ... keyN valueN Sets the values of the keys to the string values; msetnx will not overwrite existing
msetnx key1 value1 ... keyN valueN values if any key exists. Return: 1 if all keys were set, 0 if none were set

incr key Increments/decrements value of key by 1.


decr key Return: New value after increment/decrement operation
incrby key integer Increments/decrements value of key by the integer value specified.
decrby key integer Return: New value after increment/decrement operation

Performs set of commands within a transaction, in a single step. Either all or none
multi
transactions

of the commands will be processed. exec will cause the commands to be


<command1> processed; discard will abandon all of the commands.
...
<commandn> Return:
exec or discard exec returns multi bulk reply with the return value of each command
discard returns OK
REDIS cheatsheet page 2
rpush key string Adds the string to the head (rpush) or tail (lpush) of the list at key.
lpush key string Return: 1 if exists, 0 if key exists but is not a list
Returns the length of the list at key.
llen key Return: integer length, or error if key is not a list
Returns the elements of list at key, zero-based. Negative numbers are offset from
lrange key start end the end of the list. Return: requested elements or empty list if no match
commands operating on lists

Trims list at key to contain only the specified elements.


ltrim key start end Return: 1 if OK, error if key is not a list
Returns the element at the specified index of the list key.
lindex key index Return: the requested item; empty string if no such element; error if key isn't a list
Sets the element of list key at index to the specified value.
lset key index value Return: 1 if OK, error if index out of range or key isn't a list
Removes count number of items from the list that have the specified value. Count
lrem key count value 0 will remove all; negative count starts from the end. Return: # items removed
lpop key string Atomically removes and returns the first (lpop) or last (rpop) element from list
rpop key string key. Return: the element, or "nil" if empty/nonexistent list; error if key isn't a list
blpop key1...keyN timeout Blocking pop, returns when a specified list contains an element.
brpop key1...keyN timeout Return: key and popped value, or "nil" if operation times out
Atomically returns last element from srckey and pushes as first element to
rpoplpush srckey destkey destkey. Return: element popped/pushed, or "nil" if srckey empty or nonexistent

Adds member to the set stored at key.


sadd key member Return: 1 if OK, 0 if element was already a set member; error if key isn't a set
Removes member from set key.
srem key member Return: 1 if OK, 0 element not a set member; error if key isn't a set
spop key Returns random element from set key. spop will remove the element.
srandmember key Return: element, or nil object if key is empty or doesn't exist
commands operating on sets

Atomically moves member from set srckey to set dstkey.


smove srckey dstkey member
Return: 1 if OK, 0 if element not found in srckey; error if either key isn't a set
Returns the number of elements in set key.
scard key Return: integer number of elements; 0 if empty or key doesn't exist
Return whether member is in set key.
sismember key member Return: 1 if element is a member, 0 if not or if key doesn't exist

sinter key1 key2...keyN Returns the members resulting from intersection of sets specified. sinterstore will
sinterstore dstkey key1...keyN store results in new set and return status code.

sunion key1 key2...keyN Returns the members resulting from union of sets specified. sunionstore will store
sunionstore dstkey key1...keyN results in new set and return status code.

sdiff key1 key2...keyN Returns the members resulting from the difference between the first set and the
sdiffstore dstkey key1...keyN rest. sdiffstore will store results in new set and return status code.
Returns all of the members of set key. This is sinter, for only one set.
smembers key Return: the members

sort key [by pattern] [limit start count] [get pattern] [asc|desc] [alpha] [store dstkey]
Sorts the elements in the list, set, or sorted set at key. Default sort is numeric, ascending. Specifying asc or desc will sort in ascending or
descending order. Specifying alpha will sort alphabetically. limit will return count number of elements beginning at offset start (zero-
based). store will put the results of the sort into a list with key dstkey.

Specifying "by pattern" will sort using the values at keys generated using the pattern. For example, if the list/set being sorted contains the
values 1, 2, 3 then "sort by weight_*" will sort using the values at keys "weight_1", "weight_2", "weight_3".

Specifying "get pattern" will retrieve the values stored at keys generated using the pattern. For example, "get items_*" will return the
values at keys items_1, items_2, items_3 if the list/set being sorted contains the values 1, 2, 3.
REDIS cheatsheet page 3
Adds member to zset key, with specified score.
zadd key score member Return: 1 if added, 0 if element was already a member and score was updated
Removes member from zset key.
zrem key member Return: 1 if removed, 0 if element was not a member
Increments score of member by incr and updates element's position in zset.
zincrby key incr member Return: integer, the new score of member after the increment
zrank key member Returns the rank of the member in the sorted set, zero-based; returns nil if
zrevrank key member member doesn't exist. zrevrank returns the rank in high-to-low order.
commands operating on sorted sets

zrange key start end Returns elements in zset key within the specified index range, sorted in order (or
zrevrange key start end reverse with zrevrange). Option: "withscores" will also return scores.
zrangebyscore key min max
Returns elements in zset key with scores within the specified range.
[limit offset count]
Option "withscores" will also return scores with the elements.
[withscores]
zremrangebyrank key Removes elements from zset key with rank between start and end. Negative
start end numbers will start from the end. Return: integer, number of elements removed
zremrangebyscore key Removes elements from zset key with scores between min and max.
min max Return: integer, number of elements removed
Returns the number of elements in the zset key.
zcard key Return: integer, the number of elements; returns 0 if key doesn't exist
Returns the score of the specified element in zset key.
zscore key element Return: the score, as a string; or "nil" if key or element don't exist
Creates union or intersection of N sorted sets named by keys k1...kN, and stores
zunion/zinter dstkey N
it in key dstkey. The number of input keys, N, must be specified. The weights
k1...kN option will multiply the scores by the provided weights. The aggregate option
[ weights w1...wN ] changes how the scores are aggregated into the new set. Returns the number of
[ aggregate sum|min|max ] elements in the new set at dstkey.
zsets available in redis 1.1 and later. zrank, zrevrank, zrangebyscore [withscores], zremrangebyrank in redis 1.3.4. zunion/zinter in redis 1.3.5.

Sets hash field to the value. Will create a new hash if key does not exist. hsetnx
hset key field value will do nothing if field already exists.
hsetnx key field value Return: 1 if new field was created, 0 if existing field was updated or already exists

Returns value of field stored in hash key.


hget key field Return: value of field, or nil if field or key do not exist
commands operating on hashes

hmset key field1 Sets fields to the values provided, replacing existing values if any. Creates new
value1 .. fieldN valueN hash if none exists at key.
Increments value in field. Negative values will decrement. If field does not exist or
hincrby key field value holds a string, will reset to zero first. Return: the new value of field

hexists key field Returns 1 if field exists in hash at key. Returns 0 if key or field don't exist.

Removes field from hash stored at key.


hdel key field Return: 1 if field removed, 0 if field was not present

hlen key Returns the number of fields in hash stored at key, or 0 if key does not exist.

hkeys key Returns the keys or values of the hash stored at key.
hvals key
Returns the keys and values of the hash stored at key, as a multi bulk reply in the
hgetall key form field1, value1, ..., fieldN, valueN.
hash commands available in redis 1.3.10.
REDIS cheatsheet page 4
subscribe channel1 ... Subscribes the client to the specified channels.
channelN
psubscribe pattern1 ... Subscribes the client to any channels matching the specified patterns.
publish / subscribe commands

patternN
Unsubscribes the client from the specified channels, or from all channels if no
unsubscribe channels are specified. Returns a message for each unsubscribed channel, and the
[channel1 ... channelN] number of channels still subscribed to.

Unsubscribes the client from channels matching the specified patterns, or from all
punsubscribe channels if no patterns are specified. Returns a message for each unsubscribed
[pattern1 ... patternN] channel, and the number of channels still subscribed to.

Sends a message that will be received by all clients subscribed to the specified
publish channel message channel. Returns the number of clients that received the message.

When a message is received by a client, the message type will be message or pmessage to indicate how the client is
subscribed to the originating channel. If pmessage, the pattern matched will be provided. For both, the originating
channel and the message are then provided.
publish/subscribe commands in redis 1.3.8.

Saves all databases to disk. Connection requests will not be served during the
save save. Returns OK when complete.
Saves all databases to disk in the background. Redis forks and writes so the
bgsave parent process continues to process connection requests.
Returns integer unix time of last successful save. This can be used following a
lastsave bgsave to check if it was successful.

bgrewriteaof Rewrites the Append Only File in the background.


persistence and control commands

shutdown Stops all clients, saves databases, and quits.

info Returns information and statistics about the server.

Used to view commands for debugging. Telnet to redis server then enter monitor
monitor command. Enter quit to end the session.
slaveof host port Makes server the replication slave of the redis server at host/port. The "no one"
slaveof no one form turns off replication, making the server a master.

quit Tells server to close the connection immediately.

Authorizes client using the provided password, if redis server is configured with
auth password requirepass. Returns OK or error if password is incorrect.
Deletes all keys in the currently-selected database.
flushdb Return: 1 -- this command never fails
Deletes all keys in all existing databases.
flushall Return: 1 -- this command never fails

redis site: http://code.google.com/p/redis/


mailing list: http://groups.google.com/group/redis-db

redis cheat sheet v2.0 by mason jones, apr.2010 http://masonoise.wordpress.com


Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

You might also like