Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions content/commands/del/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ A key is ignored if it does not exist.

## Examples

{{< clients-example cmds_generic del >}}
> SET key1 "Hello"
"OK"
> SET key2 "World"
"OK"
> DEL key1 key2 key3
(integer) 2
{{< /clients-example >}}

Give these commands a try in the interactive console:

{{% redis-cli %}}
SET key1 "Hello"
SET key2 "World"
Expand Down
23 changes: 23 additions & 0 deletions content/commands/expire/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,29 @@ are now fixed.

## Examples

{{< clients-example cmds_generic expire >}}
> SET mykey "Hello"
"OK"
> EXPIRE mykey 10
(integer) 1
> TTL mykey
(integer) 10
> SET mykey "Hello World"
"OK"
> TTL mykey
(integer) -1
> EXPIRE mykey 10 XX
(integer) 0
> TTL mykey
(integer) -1
> EXPIRE mykey 10 NX
(integer) 1
> TTL mykey
(integer) 10
{{< /clients-example >}}

Give these commands a try in the interactive console:

{{% redis-cli %}}
SET mykey "Hello"
EXPIRE mykey 10
Expand Down
12 changes: 11 additions & 1 deletion content/commands/hget/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,19 @@ Returns the value associated with `field` in the hash stored at `key`.

## Examples

{{< clients-example cmds_hash hget >}}
> HSET myhash field1 "foo"
(integer) 1
> HGET myhash field1
"foo"
> HGET myhash field2
(nil)
{{< /clients-example >}}

Give these commands a try in the interactive console:

{{% redis-cli %}}
HSET myhash field1 "foo"
HGET myhash field1
HGET myhash field2
{{% /redis-cli %}}

25 changes: 23 additions & 2 deletions content/commands/hset/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,33 @@ If `key` doesn't exist, a new key holding a hash is created.

## Examples

{{< clients-example cmds_hash hset >}}
> HSET myhash field1 "Hello"
(integer) 1
> HGET myhash field1
"Hello"
> HSET myhash field2 "Hi" field3 "World"
(integer) 2
> HGET myhash field2
"Hi"
> HGET myhash field3
"World"
> HGETALL myhash
1) "field1"
2) "Hello"
3) "field2"
4) "Hi"
5) "field3"
6) "World"
{{< /clients-example >}}

Give these commands a try in the interactive console:

{{% redis-cli %}}
HSET myhash field1 "Hello"
HGET myhash field1
HSET myhash field2 "Hi" field3 "World"
HGET myhash field2
HGET myhash field3
HGETALL myhash
{{% /redis-cli %}}

{{% /redis-cli %}}
12 changes: 11 additions & 1 deletion content/commands/incr/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,23 @@ representation of the integer.

## Examples

{{< clients-example cmds_string incr >}}
> SET mykey "10"
"OK"
> INCR mykey
(integer) 11
> GET mykey
"11"
{{< /clients-example >}}

Give this command a try in the interactive console:

{{% redis-cli %}}
SET mykey "10"
INCR mykey
GET mykey
{{% /redis-cli %}}


## Pattern: Counter

The counter pattern is the most obvious thing you can do with Redis atomic
Expand Down
68 changes: 30 additions & 38 deletions content/commands/scan/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ SCAN is a cursor based iterator. This means that at every call of the command, t
An iteration starts when the cursor is set to 0, and terminates when the cursor returned by the server is 0. The following is an example of SCAN iteration:

```
redis 127.0.0.1:6379> scan 0
> scan 0
1) "17"
2) 1) "key:12"
2) "key:8"
Expand All @@ -89,7 +89,7 @@ redis 127.0.0.1:6379> scan 0
9) "key:3"
10) "key:7"
11) "key:1"
redis 127.0.0.1:6379> scan 17
> scan 17
1) "0"
2) 1) "key:5"
2) "key:18"
Expand Down Expand Up @@ -155,33 +155,32 @@ To do so, just append the `MATCH <pattern>` arguments at the end of the `SCAN` c

This is an example of iteration using **MATCH**:

```
redis 127.0.0.1:6379> sadd myset 1 2 3 foo foobar feelsgood
{{< clients-example cmds_generic scan1 >}}
> sadd myset 1 2 3 foo foobar feelsgood
(integer) 6
redis 127.0.0.1:6379> sscan myset 0 match f*
> sscan myset 0 match f*
1) "0"
2) 1) "foo"
2) "feelsgood"
3) "foobar"
redis 127.0.0.1:6379>
```
{{< /clients-example >}}

It is important to note that the **MATCH** filter is applied after elements are retrieved from the collection, just before returning data to the client. This means that if the pattern matches very little elements inside the collection, `SCAN` will likely return no elements in most iterations. An example is shown below:

```
redis 127.0.0.1:6379> scan 0 MATCH *11*
{{< clients-example cmds_generic scan2 >}}
> scan 0 MATCH *11*
1) "288"
2) 1) "key:911"
redis 127.0.0.1:6379> scan 288 MATCH *11*
> scan 288 MATCH *11*
1) "224"
2) (empty list or set)
redis 127.0.0.1:6379> scan 224 MATCH *11*
> scan 224 MATCH *11*
1) "80"
2) (empty list or set)
redis 127.0.0.1:6379> scan 80 MATCH *11*
> scan 80 MATCH *11*
1) "176"
2) (empty list or set)
redis 127.0.0.1:6379> scan 176 MATCH *11* COUNT 1000
> scan 176 MATCH *11* COUNT 1000
1) "0"
2) 1) "key:611"
2) "key:711"
Expand All @@ -201,8 +200,7 @@ redis 127.0.0.1:6379> scan 176 MATCH *11* COUNT 1000
16) "key:811"
17) "key:511"
18) "key:11"
redis 127.0.0.1:6379>
```
{{< /clients-example >}}

As you can see most of the calls returned zero elements, but the last call where a `COUNT` of 1000 was used in order to force the command to do more scanning for that iteration.

Expand All @@ -219,41 +217,41 @@ You can use the `TYPE` option to ask `SCAN` to only return objects that match a

The `type` argument is the same string name that the [`TYPE`]({{< relref "/commands/type" >}}) command returns. Note a quirk where some Redis types, such as GeoHashes, HyperLogLogs, Bitmaps, and Bitfields, may internally be implemented using other Redis types, such as a string or zset, so can't be distinguished from other keys of that same type by `SCAN`. For example, a ZSET and GEOHASH:

```
redis 127.0.0.1:6379> GEOADD geokey 0 0 value
{{< clients-example cmds_generic scan3 >}}
> GEOADD geokey 0 0 value
(integer) 1
redis 127.0.0.1:6379> ZADD zkey 1000 value
> ZADD zkey 1000 value
(integer) 1
redis 127.0.0.1:6379> TYPE geokey
> TYPE geokey
zset
redis 127.0.0.1:6379> TYPE zkey
> TYPE zkey
zset
redis 127.0.0.1:6379> SCAN 0 TYPE zset
> SCAN 0 TYPE zset
1) "0"
2) 1) "geokey"
2) "zkey"
```
{{< /clients-example >}}

It is important to note that the **TYPE** filter is also applied after elements are retrieved from the database, so the option does not reduce the amount of work the server has to do to complete a full iteration, and for rare types you may receive no elements in many iterations.

## The NOVALUES option

When using [`HSCAN`]({{< relref "/commands/hscan" >}}), you can use the `NOVALUES` option to make Redis return only the keys in the hash table without their corresponding values.

```
redis 127.0.0.1:6379> HSET myhash a 1 b 2
{{< clients-example cmds_generic scan4 >}}
> HSET myhash a 1 b 2
OK
redis 127.0.0.1:6379> HSCAN myhash 0
> HSCAN myhash 0
1) "0"
2) 1) "a"
2) "1"
3) "b"
4) "2"
redis 127.0.0.1:6379> HSCAN myhash 0 NOVALUES
> HSCAN myhash 0 NOVALUES
1) "0"
2) 1) "a"
2) "b"
```
{{< /clients-example >}}

## Multiple parallel iterations

Expand Down Expand Up @@ -292,15 +290,9 @@ For more information about managing keys, please refer to the [The Redis Keyspac

## Additional examples

Iteration of a Hash value.
Give the following commands, showing iteration of a hash key, a try in the interactive console:

```
redis 127.0.0.1:6379> hmset hash name Jack age 33
OK
redis 127.0.0.1:6379> hscan hash 0
1) "0"
2) 1) "name"
2) "Jack"
3) "age"
4) "33"
```
{{% redis-cli %}}
HMSET hash name Jack age 33
HSCAN hash 0
{{% /redis-cli %}}
12 changes: 11 additions & 1 deletion content/commands/ttl/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,19 @@ See also the [`PTTL`]({{< relref "/commands/pttl" >}}) command that returns the

## Examples

{{< clients-example cmds_generic ttl >}}
> SET mykey "Hello"
"OK"
> EXPIRE mykey 10
(integer) 1
> TTL mykey
(integer) 10
{{< /clients-example >}}

Give these commands a try in the interactive console:

{{% redis-cli %}}
SET mykey "Hello"
EXPIRE mykey 10
TTL mykey
{{% /redis-cli %}}

21 changes: 20 additions & 1 deletion content/commands/zadd/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,29 @@ If the user inserts all the elements in a sorted set with the same score (for ex

## Examples

{{< clients-example cmds_sorted_set zadd >}}
> ZADD myzset 1 "one"
(integer) 1
> ZADD myzset 1 "uno"
(integer) 1
> ZADD myzset 2 "two" 3 "three"
(integer) 2
> ZRANGE myzset 0 -1 WITHSCORES
1) "one"
2) "1"
3) "uno"
4) "1"
5) "two"
6) "2"
7) "three"
8) "3"
{{< /clients-example >}}

Give these commands a try in the interactive console:

{{% redis-cli %}}
ZADD myzset 1 "one"
ZADD myzset 1 "uno"
ZADD myzset 2 "two" 3 "three"
ZRANGE myzset 0 -1 WITHSCORES
{{% /redis-cli %}}

50 changes: 35 additions & 15 deletions content/commands/zrange/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,26 +196,46 @@ The binary nature of the comparison allows to use sorted sets as a general purpo

## Examples

{{% redis-cli %}}
ZADD myzset 1 "one" 2 "two" 3 "three"
ZRANGE myzset 0 -1
ZRANGE myzset 2 3
ZRANGE myzset -2 -1
{{% /redis-cli %}}

{{< clients-example cmds_sorted_set zrange1 >}}
> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
> ZRANGE myzset 0 -1
1) "one"
2) "two"
3) "three"
> ZRANGE myzset 2 3
1) "three"
> ZRANGE myzset -2 -1
1) "two"
2) "three"
{{< /clients-example >}}

The following example using `WITHSCORES` shows how the command returns always an array, but this time, populated with *element_1*, *score_1*, *element_2*, *score_2*, ..., *element_N*, *score_N*.

{{% redis-cli %}}
ZADD myzset 1 "one" 2 "two" 3 "three"
ZRANGE myzset 0 1 WITHSCORES
{{% /redis-cli %}}

{{< clients-example cmds_sorted_set zrange2 >}}
> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
> ZRANGE myzset 0 1 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
{{< /clients-example >}}

This example shows how to query the sorted set by score, excluding the value `1` and up to infinity, returning only the second element of the result:

{{< clients-example cmds_sorted_set zrange3 >}}
> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
> ZRANGE myzset (1 +inf BYSCORE LIMIT 1 1
1) "three"
{{< /clients-example >}}

Give these commands a try in the interactive console:

{{% redis-cli %}}
ZADD myzset 1 "one" 2 "two" 3 "three"
ZRANGE myzset (1 +inf BYSCORE LIMIT 1 1
{{% /redis-cli %}}

ZRANGE myzset 0 -1
ZRANGE myzset 2 3
ZRANGE myzset -2 -1
{{% /redis-cli %}}