<h2><aname="Commands operating on string values">Commands operating on string values</a></h2><ul><li><ahref="SetCommand.html">SET</a><i>key</i><i>value</i><codename="code"class="python">set a key to a string value</code></li><li><ahref="GetCommand.html">GET</a><i>key</i><codename="code"class="python">return the string value of the key</code></li><li><ahref="GetsetCommand.html">GETSET</a><i>key</i><i>value</i><codename="code"class="python">set a key to a string returning the old value of the key</code></li><li><ahref="MgetCommand.html">MGET</a><i>key1</i><i>key2</i> ... <i>keyN</i><codename="code"class="python">multi-get, return the strings values of the keys</code></li><li><ahref="SetnxCommand.html">SETNX</a><i>key</i><i>value</i><codename="code"class="python">set a key to a string value if the key does not exist</code></li><li><ahref="SetexCommand.html">SETEX</a><i>key</i><i>time</i><i>value</i><codename="code"class="python">Set+Expire combo command</code></li><li><ahref="MsetCommand.html">MSET</a><i>key1</i><i>value1</i><i>key2</i><i>value2</i> ... <i>keyN</i><i>valueN</i><codename="code"class="python">set a multiple keys to multiple values in a single atomic operation</code></li><li><ahref="MsetCommand.html">MSETNX</a><i>key1</i><i>value1</i><i>key2</i><i>value2</i> ... <i>keyN</i><i>valueN</i><codename="code"class="python">set a multiple keys to multiple values in a single atomic operation if none of the keys already exist</code></li><li><ahref="IncrCommand.html">INCR</a><i>key</i><codename="code"class="python">increment the integer value of key</code></li><li><ahref="IncrCommand.html">INCRBY</a><i>key</i><i>integer</i><codename="code"class="python"> increment the integer value of key by integer</code></li><li><ahref="IncrCommand.html">DECR</a><i>key</i><codename="code"class="python">decrement the integer value of key</code></li><li><ahref="IncrCommand.html">DECRBY</a><i>key</i><i>integer</i><codename="code"class="python">decrement the integer value of key by integer</code></li><li><ahref="AppendCommand.html">APPEND</a><i>key</i><i>value</i><codename="code"class="python">append the specified string to the string stored at key</code></li><li><ahref="SubstrCommand.html">SUBSTR</a><i>key</i><i>start</i><i>end</i><codename="code"class="python">return a substring out of a larger string</code></li></ul>
<h2><aname="Commands operating on string values">Commands operating on string values</a></h2><ul><li><ahref="SetCommand.html">SET</a><i>key</i><i>value</i><codename="code"class="python">set a key to a string value</code></li><li><ahref="GetCommand.html">GET</a><i>key</i><codename="code"class="python">return the string value of the key</code></li><li><ahref="GetsetCommand.html">GETSET</a><i>key</i><i>value</i><codename="code"class="python">set a key to a string returning the old value of the key</code></li><li><ahref="MgetCommand.html">MGET</a><i>key1</i><i>key2</i> ... <i>keyN</i><codename="code"class="python">multi-get, return the strings values of the keys</code></li><li><ahref="SetnxCommand.html">SETNX</a><i>key</i><i>value</i><codename="code"class="python">set a key to a string value if the key does not exist</code></li><li><ahref="SetexCommand.html">SETEX</a><i>key</i><i>time</i><i>value</i><codename="code"class="python">Set+Expire combo command</code></li><li><ahref="MsetCommand.html">MSET</a><i>key1</i><i>value1</i><i>key2</i><i>value2</i> ... <i>keyN</i><i>valueN</i><codename="code"class="python">set a multiple keys to multiple values in a single atomic operation</code></li><li><ahref="MsetCommand.html">MSETNX</a><i>key1</i><i>value1</i><i>key2</i><i>value2</i> ... <i>keyN</i><i>valueN</i><codename="code"class="python">set a multiple keys to multiple values in a single atomic operation if none of the keys already exist</code></li><li><ahref="IncrCommand.html">INCR</a><i>key</i><codename="code"class="python">increment the integer value of key</code></li><li><ahref="IncrCommand.html">INCRBY</a><i>key</i><i>integer</i><codename="code"class="python"> increment the integer value of key by integer</code></li><li><ahref="IncrCommand.html">DECR</a><i>key</i><codename="code"class="python">decrement the integer value of key</code></li><li><ahref="IncrCommand.html">DECRBY</a><i>key</i><i>integer</i><codename="code"class="python">decrement the integer value of key by integer</code></li><li><ahref="AppendCommand.html">APPEND</a><i>key</i><i>value</i><codename="code"class="python">append the specified string to the string stored at key</code></li><li><ahref="SubstrCommand.html">SUBSTR</a><i>key</i><i>start</i><i>end</i><codename="code"class="python">return a substring out of a larger string</code></li></ul>
<h2><aname="Commands operating on lists">Commands operating on lists</a></h2><ul><li><ahref="RpushCommand.html">RPUSH</a><i>key</i><i>value</i><codename="code"class="python">Append an element to the tail of the List value at key</code></li><li><ahref="RpushCommand.html">LPUSH</a><i>key</i><i>value</i><codename="code"class="python">Append an element to the head of the List value at key</code></li><li><ahref="LlenCommand.html">LLEN</a><i>key</i><codename="code"class="python">Return the length of the List value at key</code></li><li><ahref="LrangeCommand.html">LRANGE</a><i>key</i><i>start</i><i>end</i><codename="code"class="python">Return a range of elements from the List at key</code></li><li><ahref="LtrimCommand.html">LTRIM</a><i>key</i><i>start</i><i>end</i><codename="code"class="python">Trim the list at key to the specified range of elements</code></li><li><ahref="LindexCommand.html">LINDEX</a><i>key</i><i>index</i><codename="code"class="python">Return the element at index position from the List at key</code></li><li><ahref="LsetCommand.html">LSET</a><i>key</i><i>index</i><i>value</i><codename="code"class="python">Set a new value as the element at index position of the List at key</code></li><li><ahref="LremCommand.html">LREM</a><i>key</i><i>count</i><i>value</i><codename="code"class="python">Remove the first-N, last-N, or all the elements matching value from the List at key</code></li><li><ahref="LpopCommand.html">LPOP</a><i>key</i><codename="code"class="python">Return and remove (atomically) the first element of the List at key</code></li><li><ahref="LpopCommand.html">RPOP</a><i>key</i><codename="code"class="python">Return and remove (atomically) the last element of the List at key</code></li><li><ahref="BlpopCommand.html">BLPOP</a><i>key1</i><i>key2</i> ... <i>keyN</i><i>timeout</i><codename="code"class="python">Blocking LPOP</code></li><li><ahref="BlpopCommand.html">BRPOP</a><i>key1</i><i>key2</i> ... <i>keyN</i><i>timeout</i><codename="code"class="python">Blocking RPOP</code></li><li><ahref="RpoplpushCommand.html">RPOPLPUSH</a><i>srckey</i><i>dstkey</i><codename="code"class="python">Return and remove (atomically) the last element of the source List stored at _srckey_ and push the same element to the destination List stored at _dstkey_</code></li></ul>
<h2><aname="Commands operating on lists">Commands operating on lists</a></h2><ul><li><ahref="RpushCommand.html">RPUSH</a><i>key</i><i>value</i><codename="code"class="python">Append an element to the tail of the List value at key</code></li><li><ahref="RpushCommand.html">LPUSH</a><i>key</i><i>value</i><codename="code"class="python">Append an element to the head of the List value at key</code></li><li><ahref="LlenCommand.html">LLEN</a><i>key</i><codename="code"class="python">Return the length of the List value at key</code></li><li><ahref="LrangeCommand.html">LRANGE</a><i>key</i><i>start</i><i>end</i><codename="code"class="python">Return a range of elements from the List at key</code></li><li><ahref="LtrimCommand.html">LTRIM</a><i>key</i><i>start</i><i>end</i><codename="code"class="python">Trim the list at key to the specified range of elements</code></li><li><ahref="LindexCommand.html">LINDEX</a><i>key</i><i>index</i><codename="code"class="python">Return the element at index position from the List at key</code></li><li><ahref="LsetCommand.html">LSET</a><i>key</i><i>index</i><i>value</i><codename="code"class="python">Set a new value as the element at index position of the List at key</code></li><li><ahref="LremCommand.html">LREM</a><i>key</i><i>count</i><i>value</i><codename="code"class="python">Remove the first-N, last-N, or all the elements matching value from the List at key</code></li><li><ahref="LpopCommand.html">LPOP</a><i>key</i><codename="code"class="python">Return and remove (atomically) the first element of the List at key</code></li><li><ahref="LpopCommand.html">RPOP</a><i>key</i><codename="code"class="python">Return and remove (atomically) the last element of the List at key</code></li><li><ahref="BlpopCommand.html">BLPOP</a><i>key1</i><i>key2</i> ... <i>keyN</i><i>timeout</i><codename="code"class="python">Blocking LPOP</code></li><li><ahref="BlpopCommand.html">BRPOP</a><i>key1</i><i>key2</i> ... <i>keyN</i><i>timeout</i><codename="code"class="python">Blocking RPOP</code></li><li><ahref="RpoplpushCommand.html">RPOPLPUSH</a><i>srckey</i><i>dstkey</i><codename="code"class="python">Return and remove (atomically) the last element of the source List stored at _srckey_ and push the same element to the destination List stored at _dstkey_</code></li></ul>
<h2><aname="Commands operating on sets">Commands operating on sets</a></h2><ul><li><ahref="SaddCommand.html">SADD</a><i>key</i><i>member</i><codename="code"class="python">Add the specified member to the Set value at key</code></li><li><ahref="SremCommand.html">SREM</a><i>key</i><i>member</i><codename="code"class="python">Remove the specified member from the Set value at key</code></li><li><ahref="SpopCommand.html">SPOP</a><i>key</i><codename="code"class="python">Remove and return (pop) a random element from the Set value at key</code></li><li><ahref="SmoveCommand.html">SMOVE</a><i>srckey</i><i>dstkey</i><i>member</i><codename="code"class="python">Move the specified member from one Set to another atomically</code></li><li><ahref="ScardCommand.html">SCARD</a><i>key</i><codename="code"class="python">Return the number of elements (the cardinality) of the Set at key</code></li><li><ahref="SismemberCommand.html">SISMEMBER</a><i>key</i><i>member</i><codename="code"class="python">Test if the specified value is a member of the Set at key</code></li><li><ahref="SinterCommand.html">SINTER</a><i>key1</i><i>key2</i> ... <i>keyN</i><codename="code"class="python">Return the intersection between the Sets stored at key1, key2, ..., keyN</code></li><li><ahref="SinterstoreCommand.html">SINTERSTORE</a><i>dstkey</i><i>key1</i><i>key2</i> ... <i>keyN</i><codename="code"class="python">Compute the intersection between the Sets stored at key1, key2, ..., keyN, and store the resulting Set at dstkey</code></li><li><ahref="SunionCommand.html">SUNION</a><i>key1</i><i>key2</i> ... <i>keyN</i><codename="code"class="python">Return the union between the Sets stored at key1, key2, ..., keyN</code></li><li><ahref="SunionstoreCommand.html">SUNIONSTORE</a><i>dstkey</i><i>key1</i><i>key2</i> ... <i>keyN</i><codename="code"class="python">Compute the union between the Sets stored at key1, key2, ..., keyN, and store the resulting Set at dstkey</code></li><li><ahref="SdiffCommand.html">SDIFF</a><i>key1</i><i>key2</i> ... <i>keyN</i><codename="code"class="python">Return the difference between the Set stored at key1 and all the Sets key2, ..., keyN</code></li><li><ahref="SdiffstoreCommand.html">SDIFFSTORE</a><i>dstkey</i><i>key1</i><i>key2</i> ... <i>keyN</i><codename="code"class="python">Compute the difference between the Set key1 and all the Sets key2, ..., keyN, and store the resulting Set at dstkey</code></li><li><ahref="SmembersCommand.html">SMEMBERS</a><i>key</i><codename="code"class="python">Return all the members of the Set value at key</code></li><li><ahref="SrandmemberCommand.html">SRANDMEMBER</a><i>key</i><codename="code"class="python">Return a random member of the Set value at key</code></li></ul>
<h2><aname="Commands operating on sets">Commands operating on sets</a></h2><ul><li><ahref="SaddCommand.html">SADD</a><i>key</i><i>member</i><codename="code"class="python">Add the specified member to the Set value at key</code></li><li><ahref="SremCommand.html">SREM</a><i>key</i><i>member</i><codename="code"class="python">Remove the specified member from the Set value at key</code></li><li><ahref="SpopCommand.html">SPOP</a><i>key</i><codename="code"class="python">Remove and return (pop) a random element from the Set value at key</code></li><li><ahref="SmoveCommand.html">SMOVE</a><i>srckey</i><i>dstkey</i><i>member</i><codename="code"class="python">Move the specified member from one Set to another atomically</code></li><li><ahref="ScardCommand.html">SCARD</a><i>key</i><codename="code"class="python">Return the number of elements (the cardinality) of the Set at key</code></li><li><ahref="SismemberCommand.html">SISMEMBER</a><i>key</i><i>member</i><codename="code"class="python">Test if the specified value is a member of the Set at key</code></li><li><ahref="SinterCommand.html">SINTER</a><i>key1</i><i>key2</i> ... <i>keyN</i><codename="code"class="python">Return the intersection between the Sets stored at key1, key2, ..., keyN</code></li><li><ahref="SinterstoreCommand.html">SINTERSTORE</a><i>dstkey</i><i>key1</i><i>key2</i> ... <i>keyN</i><codename="code"class="python">Compute the intersection between the Sets stored at key1, key2, ..., keyN, and store the resulting Set at dstkey</code></li><li><ahref="SunionCommand.html">SUNION</a><i>key1</i><i>key2</i> ... <i>keyN</i><codename="code"class="python">Return the union between the Sets stored at key1, key2, ..., keyN</code></li><li><ahref="SunionstoreCommand.html">SUNIONSTORE</a><i>dstkey</i><i>key1</i><i>key2</i> ... <i>keyN</i><codename="code"class="python">Compute the union between the Sets stored at key1, key2, ..., keyN, and store the resulting Set at dstkey</code></li><li><ahref="SdiffCommand.html">SDIFF</a><i>key1</i><i>key2</i> ... <i>keyN</i><codename="code"class="python">Return the difference between the Set stored at key1 and all the Sets key2, ..., keyN</code></li><li><ahref="SdiffstoreCommand.html">SDIFFSTORE</a><i>dstkey</i><i>key1</i><i>key2</i> ... <i>keyN</i><codename="code"class="python">Compute the difference between the Set key1 and all the Sets key2, ..., keyN, and store the resulting Set at dstkey</code></li><li><ahref="SmembersCommand.html">SMEMBERS</a><i>key</i><codename="code"class="python">Return all the members of the Set value at key</code></li><li><ahref="SrandmemberCommand.html">SRANDMEMBER</a><i>key</i><codename="code"class="python">Return a random member of the Set value at key</code></li></ul>
<h2><aname="Commands operating on sorted sets (zsets, Redis version >">Commands operating on sorted sets (zsets, Redis version ></a></h2> 1.1) ==<br/><br/><ul><li><ahref="ZaddCommand.html">ZADD</a><i>key</i><i>score</i><i>member</i><codename="code"class="python">Add the specified member to the Sorted Set value at key or update the score if it already exist</code></li><li><ahref="ZremCommand.html">ZREM</a><i>key</i><i>member</i><codename="code"class="python">Remove the specified member from the Sorted Set value at key</code></li><li><ahref="ZincrbyCommand.html">ZINCRBY</a><i>key</i><i>increment</i><i>member</i><codename="code"class="python">If the member already exists increment its score by _increment_, otherwise add the member setting _increment_ as score</code></li><li><ahref="ZrankCommand.html">ZRANK</a><i>key</i><i>member</i><codename="code"class="python">Return the rank (or index) or _member_ in the sorted set at _key_, with scores being ordered from low to high</code></li><li><ahref="ZrankCommand.html">ZREVRANK</a><i>key</i><i>member</i><codename="code"class="python">Return the rank (or index) or _member_ in the sorted set at _key_, with scores being ordered from high to low</code></li><li><ahref="ZrangeCommand.html">ZRANGE</a><i>key</i><i>start</i><i>end</i><codename="code"class="python">Return a range of elements from the sorted set at key</code></li><li><ahref="ZrangeCommand.html">ZREVRANGE</a><i>key</i><i>start</i><i>end</i><codename="code"class="python">Return a range of elements from the sorted set at key, exactly like ZRANGE, but the sorted set is ordered in traversed in reverse order, from the greatest to the smallest score</code></li><li><ahref="ZrangebyscoreCommand.html">ZRANGEBYSCORE</a><i>key</i><i>min</i><i>max</i><codename="code"class="python">Return all the elements with score >= min and score <= max (a range query) from the sorted set</code></li><li><ahref="ZcardCommand.html">ZCARD</a><i>key</i><codename="code"class="python">Return the cardinality (number of elements) of the sorted set at key</code></li><li><ahref="ZscoreCommand.html">ZSCORE</a><i>key</i><i>element</i><codename="code"class="python">Return the score associated with the specified element of the sorted set at key</code></li><li><ahref="ZremrangebyrankCommand.html">ZREMRANGEBYRANK</a><i>key</i><i>min</i><i>max</i><codename="code"class="python">Remove all the elements with rank >= min and rank <= max from the sorted set</code></li><li><ahref="ZremrangebyscoreCommand.html">ZREMRANGEBYSCORE</a><i>key</i><i>min</i><i>max</i><codename="code"class="python">Remove all the elements with score >= min and score <= max from the sorted set</code></li><li><ahref="ZunionCommand.html">ZUNION / ZINTER</a><i>dstkey</i><i>N</i><i>key1</i> ... <i>keyN</i> WEIGHTS <i>w1</i> ... <i>wN</i> AGGREGATE SUM|MIN|MAX <codename="code"class="python">Perform a union or intersection over a number of sorted sets with optional weight and aggregate</code></li></ul>
<h2><aname="Commands operating on sorted sets (zsets, Redis version >">Commands operating on sorted sets (zsets, Redis version ></a></h2> 1.1) ==<br/><br/><ul><li><ahref="ZaddCommand.html">ZADD</a><i>key</i><i>score</i><i>member</i><codename="code"class="python">Add the specified member to the Sorted Set value at key or update the score if it already exist</code></li><li><ahref="ZremCommand.html">ZREM</a><i>key</i><i>member</i><codename="code"class="python">Remove the specified member from the Sorted Set value at key</code></li><li><ahref="ZincrbyCommand.html">ZINCRBY</a><i>key</i><i>increment</i><i>member</i><codename="code"class="python">If the member already exists increment its score by _increment_, otherwise add the member setting _increment_ as score</code></li><li><ahref="ZrankCommand.html">ZRANK</a><i>key</i><i>member</i><codename="code"class="python">Return the rank (or index) or _member_ in the sorted set at _key_, with scores being ordered from low to high</code></li><li><ahref="ZrankCommand.html">ZREVRANK</a><i>key</i><i>member</i><codename="code"class="python">Return the rank (or index) or _member_ in the sorted set at _key_, with scores being ordered from high to low</code></li><li><ahref="ZrangeCommand.html">ZRANGE</a><i>key</i><i>start</i><i>end</i><codename="code"class="python">Return a range of elements from the sorted set at key</code></li><li><ahref="ZrangeCommand.html">ZREVRANGE</a><i>key</i><i>start</i><i>end</i><codename="code"class="python">Return a range of elements from the sorted set at key, exactly like ZRANGE, but the sorted set is ordered in traversed in reverse order, from the greatest to the smallest score</code></li><li><ahref="ZrangebyscoreCommand.html">ZRANGEBYSCORE</a><i>key</i><i>min</i><i>max</i><codename="code"class="python">Return all the elements with score >= min and score <= max (a range query) from the sorted set</code></li><li><ahref="ZcardCommand.html">ZCARD</a><i>key</i><codename="code"class="python">Return the cardinality (number of elements) of the sorted set at key</code></li><li><ahref="ZscoreCommand.html">ZSCORE</a><i>key</i><i>element</i><codename="code"class="python">Return the score associated with the specified element of the sorted set at key</code></li><li><ahref="ZremrangebyrankCommand.html">ZREMRANGEBYRANK</a><i>key</i><i>min</i><i>max</i><codename="code"class="python">Remove all the elements with rank >= min and rank <= max from the sorted set</code></li><li><ahref="ZremrangebyscoreCommand.html">ZREMRANGEBYSCORE</a><i>key</i><i>min</i><i>max</i><codename="code"class="python">Remove all the elements with score >= min and score <= max from the sorted set</code></li><li><ahref="ZunionstoreCommand.html">ZUNIONSTORE / ZINTERSTORE</a><i>dstkey</i><i>N</i><i>key1</i> ... <i>keyN</i> WEIGHTS <i>w1</i> ... <i>wN</i> AGGREGATE SUM|MIN|MAX <codename="code"class="python">Perform a union or intersection over a number of sorted sets with optional weight and aggregate</code></li></ul>
<h2><aname="Commands operating on hashes">Commands operating on hashes</a></h2><ul><li><ahref="HsetCommand.html">HSET</a><i>key</i><i>field</i><i>value</i><codename="code"class="python">Set the hash field to the specified value. Creates the hash if needed.</code></li><li><ahref="HgetCommand.html">HGET</a><i>key</i><i>field</i><codename="code"class="python">Retrieve the value of the specified hash field.</code></li><li><ahref="HmsetCommand.html">HMSET</a><i>key</i><i>field1</i><i>value1</i> ... <i>fieldN</i><i>valueN</i><codename="code"class="python">Set the hash fields to their respective values.</code></li><li><ahref="HincrbyCommand.html">HINCRBY</a><i>key</i><i>field</i><i>integer</i><codename="code"class="python">Increment the integer value of the hash at _key_ on _field_ with _integer_.</code></li><li><ahref="HexistsCommand.html">HEXISTS</a><i>key</i><i>field</i><codename="code"class="python">Test for existence of a specified field in a hash</code></li><li><ahref="HdelCommand.html">HDEL</a><i>key</i><i>field</i><codename="code"class="python">Remove the specified field from a hash</code></li><li><ahref="HlenCommand.html">HLEN</a><i>key</i><codename="code"class="python">Return the number of items in a hash.</code></li><li><ahref="HgetallCommand.html">HKEYS</a><i>key</i><codename="code"class="python">Return all the fields in a hash.</code></li><li><ahref="HgetallCommand.html">HVALS</a><i>key</i><codename="code"class="python">Return all the values in a hash.</code></li><li><ahref="HgetallCommand.html">HGETALL</a><i>key</i><codename="code"class="python">Return all the fields and associated values in a hash.</code></li></ul>
<h2><aname="Commands operating on hashes">Commands operating on hashes</a></h2><ul><li><ahref="HsetCommand.html">HSET</a><i>key</i><i>field</i><i>value</i><codename="code"class="python">Set the hash field to the specified value. Creates the hash if needed.</code></li><li><ahref="HgetCommand.html">HGET</a><i>key</i><i>field</i><codename="code"class="python">Retrieve the value of the specified hash field.</code></li><li><ahref="HmsetCommand.html">HMSET</a><i>key</i><i>field1</i><i>value1</i> ... <i>fieldN</i><i>valueN</i><codename="code"class="python">Set the hash fields to their respective values.</code></li><li><ahref="HincrbyCommand.html">HINCRBY</a><i>key</i><i>field</i><i>integer</i><codename="code"class="python">Increment the integer value of the hash at _key_ on _field_ with _integer_.</code></li><li><ahref="HexistsCommand.html">HEXISTS</a><i>key</i><i>field</i><codename="code"class="python">Test for existence of a specified field in a hash</code></li><li><ahref="HdelCommand.html">HDEL</a><i>key</i><i>field</i><codename="code"class="python">Remove the specified field from a hash</code></li><li><ahref="HlenCommand.html">HLEN</a><i>key</i><codename="code"class="python">Return the number of items in a hash.</code></li><li><ahref="HgetallCommand.html">HKEYS</a><i>key</i><codename="code"class="python">Return all the fields in a hash.</code></li><li><ahref="HgetallCommand.html">HVALS</a><i>key</i><codename="code"class="python">Return all the values in a hash.</code></li><li><ahref="HgetallCommand.html">HGETALL</a><i>key</i><codename="code"class="python">Return all the fields and associated values in a hash.</code></li></ul>
<h2><aname="Sorting">Sorting</a></h2><ul><li><ahref="SortCommand.html">SORT</a><i>key</i> BY <i>pattern</i> LIMIT <i>start</i><i>end</i> GET <i>pattern</i> ASC|DESC ALPHA <codename="code"class="python">Sort a Set or a List accordingly to the specified parameters</code></li></ul>
<h2><aname="Sorting">Sorting</a></h2><ul><li><ahref="SortCommand.html">SORT</a><i>key</i> BY <i>pattern</i> LIMIT <i>start</i><i>end</i> GET <i>pattern</i> ASC|DESC ALPHA <codename="code"class="python">Sort a Set or a List accordingly to the specified parameters</code></li></ul>
<!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
<!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
<b>SortCommand: Contents</b><br> <ahref="#Sorting by external keys">Sorting by external keys</a><br> <ahref="#Retrieving external keys">Retrieving external keys</a><br> <ahref="#Storing the result of a SORT operation">Storing the result of a SORT operation</a><br> <ahref="#SORT and Hashes: BY and GET by hash field">SORT and Hashes: BY and GET by hash field</a><br> <ahref="#Return value">Return value</a>
<b>SortCommand: Contents</b><br> <ahref="#Sorting by external keys">Sorting by external keys</a><br> <ahref="#Not Sorting at all">Not Sorting at all</a><br> <ahref="#Retrieving external keys">Retrieving external keys</a><br> <ahref="#Storing the result of a SORT operation">Storing the result of a SORT operation</a><br> <ahref="#SORT and Hashes: BY and GET by hash field">SORT and Hashes: BY and GET by hash field</a><br> <ahref="#Return value">Return value</a>
</pre><blockquote>the <b>BY</b> option takes a pattern (<codename="code"class="python">weight_*</code> in our example) that is usedin order to generate the key names of the weights used for sorting.Weight key names are obtained substituting the first occurrence of <codename="code"class="python">*</code>with the actual value of the elements on the list (1,2,3,4 in our example).</blockquote>
</pre><blockquote>the <b>BY</b> option takes a pattern (<codename="code"class="python">weight_*</code> in our example) that is usedin order to generate the key names of the weights used for sorting.Weight key names are obtained substituting the first occurrence of <codename="code"class="python">*</code>with the actual value of the elements on the list (1,2,3,4 in our example).</blockquote>
<blockquote>Our previous example will return just the sorted IDs. Often it isneeded to get the actual objects sorted (object_1, ..., object_4 in theexample). We can do it with the following command:</blockquote>
<blockquote>Our previous example will return just the sorted IDs. Often it isneeded to get the actual objects sorted (object_1, ..., object_4 in theexample). We can do it with the following command:</blockquote>
<h2><aname="Not Sorting at all">Not Sorting at all</a></h2><preclass="codeblock python python python python python python"name="code">
SORT mylist BY nosort
</pre><blockquote>also the <b>BY</b> option can take a "nosort" specifier. This is useful if you want to retrieve a external key (using GET, read below) but you don't want the sorting overhead.</blockquote>
</pre><blockquote>Note that <b>GET</b> can be used multiple times in order to get more keys forevery element of the original List, Set or Sorted Set sorted.</blockquote>
</pre><blockquote>Note that <b>GET</b> can be used multiple times in order to get more keys forevery element of the original List, Set or Sorted Set sorted.</blockquote>
<blockquote>Since Redis >= 1.1 it's possible to also GET the list elements itselfusing the special # pattern:</blockquote>
<blockquote>Since Redis >= 1.1 it's possible to also GET the list elements itselfusing the special # pattern:</blockquote>
</pre><h2><aname="Storing the result of a SORT operation">Storing the result of a SORT operation</a></h2><blockquote>By default SORT returns the sorted elements as its return value.Using the <b>STORE</b> option instead to return the elements SORT willstore this elements as a <ahref="Lists.html">Redis List</a> in the specified key.An example:</blockquote>
</pre><h2><aname="Storing the result of a SORT operation">Storing the result of a SORT operation</a></h2><blockquote>By default SORT returns the sorted elements as its return value.Using the <b>STORE</b> option instead to return the elements SORT willstore this elements as a <ahref="Lists.html">Redis List</a> in the specified key.An example:</blockquote>
</pre><blockquote>An interesting pattern using SORT ... STORE consists in associatingan <ahref="ExpireCommand.html">EXPIRE</a> timeout to the resulting key so that inapplications where the result of a sort operation can be cached forsome time other clients will use the cached list instead to call SORTfor every request. When the key will timeout an updated version ofthe cache can be created using SORT ... STORE again.</blockquote>
</pre><blockquote>An interesting pattern using SORT ... STORE consists in associatingan <ahref="ExpireCommand.html">EXPIRE</a> timeout to the resulting key so that inapplications where the result of a sort operation can be cached forsome time other clients will use the cached list instead to call SORTfor every request. When the key will timeout an updated version ofthe cache can be created using SORT ... STORE again.</blockquote>
<blockquote>Note that implementing this pattern it is important to avoid that multipleclients will try to rebuild the cached version of the cacheat the same time, so some form of locking should be implemented(for instance using <ahref="SetnxCommand.html">SETNX</a>).</blockquote>
<blockquote>Note that implementing this pattern it is important to avoid that multipleclients will try to rebuild the cached version of the cacheat the same time, so some form of locking should be implemented(for instance using <ahref="SetnxCommand.html">SETNX</a>).</blockquote>
<h2><aname="SORT and Hashes: BY and GET by hash field">SORT and Hashes: BY and GET by hash field</a></h2>
<h2><aname="SORT and Hashes: BY and GET by hash field">SORT and Hashes: BY and GET by hash field</a></h2>
<blockquote>It's possible to use BY and GET options against Hash fields using the following syntax:</blockquote><preclass="codeblock python python python python python python python python python"name="code">
<blockquote>It's possible to use BY and GET options against Hash fields using the following syntax:</blockquote><preclass="codeblock python python python python python python python python python python"name="code">
#sidebar <ahref="SortedSetCommandsSidebar.html">SortedSetCommandsSidebar</a><h1><aname="Redis Sorted Set Type">Redis Sorted Set Type</a></h1>Redis Sorted Sets are, similarly to <ahref="Sets.html">Sets</a>, collections of <ahref="Strings.html">Redis Strings</a>. The difference is that every member of a Sorted Set hash an <b>associated score</b> that is used in order to take this member in order.<br/><br/>The <ahref="ZADD.html">ZaddCommand</a> command is used to add a new member to a Sorted Set, specifying the score of the element. Calling ZADD against a member already present in the sorted set but using a different score will update the score for the element, moving it to the right position in order to preserve ordering.<br/><br/>It's possible to get ranges of elements from Sorted Sets in a very similar way to what happens with <ahref="Lists.html">Lists</a> and the <ahref="LrangeCommnad.html">LRANGE</a> command using the Sorted Sets <ahref="ZrangeCommand.html">ZRANGE</a> command.<br/><br/>It's also possible to get or remove ranges of elements by score using the <ahref="ZrangebyscoreCommand.html">ZRANGEBYSCORE</a> and <ahref="ZremrangebyscoreCommand.html">ZREMRANGEBYSCORE</a> commands.<br/><br/>The max number of members in a sorted set is 232-1 (4294967295, more than 4 billion of members per set).<br/><br/>Note that while Sorted Sets are already ordered, it is still possible to use the <ahref="SortCommand.html">SORT</a> command against sorted sets to get the elements in a different order.<h1><aname="Implementation details">Implementation details</a></h1>Redis Sets are implemented using a dual-ported data structure containing a skip list and an hash table. When an element is added a map between the element and the score is added to the hash table (so that given the element we get the score in O(1)), and a map between the score and the element is added in the skip list so that elements are taken in order.<br/><br/>Redis uses a special skip list implementation that is doubly linked so that it's possible to traverse the sorted set from tail to head if needed (Check the <ahref="ZRevrangeCommand.html">ZREVRANGE</a> command).<br/><br/>When <ahref="ZaddCommand.html">ZADD</a> is used in order to update the score of an element, Redis retrieve the score of the element using the hash table, so that it's fast to access the element inside the skip list (that's indexed by score) in order to update the position.<br/><br/>Like it happens for Sets the hash table resizing is a blocking operation performed synchronously so working with huge sorted sets (consisting of many millions of elements) care should be taken when mass-inserting a very big amount of elements in a Set while other clients are querying Redis at high speed.<br/><br/>It is possible that in the near future Redis will switch to skip lists even for the element => score map, so every Sorted Set will have two skip lists, one indexed by element and one indexed by score.
#sidebar <ahref="SortedSetCommandsSidebar.html">SortedSetCommandsSidebar</a><h1><aname="Redis Sorted Set Type">Redis Sorted Set Type</a></h1>Redis Sorted Sets are, similarly to <ahref="Sets.html">Sets</a>, collections of <ahref="Strings.html">Redis Strings</a>. The difference is that every member of a Sorted Set hash an <b>associated score</b> that is used in order to take this member in order.<br/><br/>The <ahref="ZaddCommand.html">ZADD</a> command is used to add a new member to a Sorted Set, specifying the score of the element. Calling ZADD against a member already present in the sorted set but using a different score will update the score for the element, moving it to the right position in order to preserve ordering.<br/><br/>It's possible to get ranges of elements from Sorted Sets in a very similar way to what happens with <ahref="Lists.html">Lists</a> and the <ahref="LrangeCommnad.html">LRANGE</a> command using the Sorted Sets <ahref="ZrangeCommand.html">ZRANGE</a> command.<br/><br/>It's also possible to get or remove ranges of elements by score using the <ahref="ZrangebyscoreCommand.html">ZRANGEBYSCORE</a> and <ahref="ZremrangebyscoreCommand.html">ZREMRANGEBYSCORE</a> commands.<br/><br/>The max number of members in a sorted set is 232-1 (4294967295, more than 4 billion of members per set).<br/><br/>Note that while Sorted Sets are already ordered, it is still possible to use the <ahref="SortCommand.html">SORT</a> command against sorted sets to get the elements in a different order.<h1><aname="Implementation details">Implementation details</a></h1>Redis Sets are implemented using a dual-ported data structure containing a skip list and an hash table. When an element is added a map between the element and the score is added to the hash table (so that given the element we get the score in O(1)), and a map between the score and the element is added in the skip list so that elements are taken in order.<br/><br/>Redis uses a special skip list implementation that is doubly linked so that it's possible to traverse the sorted set from tail to head if needed (Check the <ahref="ZRevrangeCommand.html">ZREVRANGE</a> command).<br/><br/>When <ahref="ZaddCommand.html">ZADD</a> is used in order to update the score of an element, Redis retrieve the score of the element using the hash table, so that it's fast to access the element inside the skip list (that's indexed by score) in order to update the position.<br/><br/>Like it happens for Sets the hash table resizing is a blocking operation performed synchronously so working with huge sorted sets (consisting of many millions of elements) care should be taken when mass-inserting a very big amount of elements in a Set while other clients are querying Redis at high speed.<br/><br/>It is possible that in the near future Redis will switch to skip lists even for the element => score map, so every Sorted Set will have two skip lists, one indexed by element and one indexed by score.
<i>Time complexity: O(log(N))+O(M) with N being the number of elements in the sorted set and M the number of elements returned by the command, so if M is constant (for instance you always ask for the first ten elements with LIMIT) you can consider it O(log(N))</i><blockquote>Return the all the elements in the sorted set at key with a score between_min_ and <i>max</i> (including elements with score equal to min or max).</blockquote>
<i>Time complexity: O(log(N))+O(M) with N being the number of elements in the sorted set and M the number of elements returned by the command, so if M is constant (for instance you always ask for the first ten elements with LIMIT) you can consider it O(log(N))</i><blockquote>Return the all the elements in the sorted set at key with a score between_min_ and <i>max</i> (including elements with score equal to min or max).</blockquote>
<blockquote>The elements having the same score are returned sorted lexicographically asASCII strings (this follows from a property of Redis sorted sets and does notinvolve further computation).</blockquote>
<blockquote>The elements having the same score are returned sorted lexicographically asASCII strings (this follows from a property of Redis sorted sets and does notinvolve further computation).</blockquote>
<blockquote>Using the optional LIMIT it's possible to get only a range of the matchingelements in an SQL-alike way. Note that if <i>offset</i> is large the commandsneeds to traverse the list for <i>offset</i> elements and this adds up to theO(M) figure.</blockquote>
<blockquote>Using the optional LIMIT it's possible to get only a range of the matchingelements in an SQL-alike way. Note that if <i>offset</i> is large the commandsneeds to traverse the list for <i>offset</i> elements and this adds up to theO(M) figure.</blockquote><h2><aname="Exclusive intervals and infinity">Exclusive intervals and infinity</a></h2>
<i>min</i> and <i>max</i> can be -inf and +inf, so that you are not required to know what's the greatest or smallest element in order to take, for instance, elements "up to a given value".<br/><br/>Also while the interval is for default closed (inclusive) it's possible to specify open intervals prefixing the score with a "(" character, so for instance:
<preclass="codeblock python"name="code">
ZRANGEBYSCORE zset (1.3 5
</pre>
Will return all the values with score <b>> 1.3 and <= 5</b>, while for instance:
<preclass="codeblock python python"name="code">
ZRANGEBYSCORE zset (5 (10
</pre>
Will return all the values with score <b>> 5 and < 10</b> (5 and 10 excluded).
<h2><aname="Return value">Return value</a></h2><ahref="ReplyTypes.html">Multi bulk reply</a>, specifically a list of elements in the specified score range.
<h2><aname="Return value">Return value</a></h2><ahref="ReplyTypes.html">Multi bulk reply</a>, specifically a list of elements in the specified score range.