From bbb2ce30ffa1b23a01b42c3dc53aaf355b18d77a Mon Sep 17 00:00:00 2001 From: "David W. Dougherty" Date: Fri, 16 Aug 2024 14:51:37 -0700 Subject: [PATCH 1/8] DOC-4040: (DOC-4076 & DOC-4077) changes for HSET and HGET commands --- content/commands/hget/index.md | 12 +++++++++++- content/commands/hset/index.md | 25 +++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/content/commands/hget/index.md b/content/commands/hget/index.md index 87a477cd23..f4a105c21f 100644 --- a/content/commands/hget/index.md +++ b/content/commands/hget/index.md @@ -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 %}} - diff --git a/content/commands/hset/index.md b/content/commands/hset/index.md index 9d08bcb908..eb440d9b13 100644 --- a/content/commands/hset/index.md +++ b/content/commands/hset/index.md @@ -68,6 +68,28 @@ 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 @@ -75,5 +97,4 @@ HSET myhash field2 "Hi" field3 "World" HGET myhash field2 HGET myhash field3 HGETALL myhash -{{% /redis-cli %}} - +{{% /redis-cli %}} \ No newline at end of file From 8e02540badc523d9371fc484eaf79186bb512947 Mon Sep 17 00:00:00 2001 From: "David W. Dougherty" Date: Tue, 20 Aug 2024 11:57:10 -0700 Subject: [PATCH 2/8] DOC-4040: (DOC-4078) changes for INCR command --- content/commands/incr/index.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/content/commands/incr/index.md b/content/commands/incr/index.md index c4a19c3ee0..3e0314e8a9 100644 --- a/content/commands/incr/index.md +++ b/content/commands/incr/index.md @@ -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 consol: + {{% 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 From f32fb842e9424d0e7b75e51fb8802ab9d8973e06 Mon Sep 17 00:00:00 2001 From: "David W. Dougherty" Date: Tue, 20 Aug 2024 15:04:44 -0700 Subject: [PATCH 3/8] DOC-4040: (DOC-4080) changes for INCR command --- content/commands/incr/index.md | 2 +- content/commands/zadd/index.md | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/content/commands/incr/index.md b/content/commands/incr/index.md index 3e0314e8a9..aff53218b4 100644 --- a/content/commands/incr/index.md +++ b/content/commands/incr/index.md @@ -76,7 +76,7 @@ representation of the integer. "11" {{< /clients-example >}} -Give this command a try in the interactive consol: +Give this command a try in the interactive console: {{% redis-cli %}} SET mykey "10" diff --git a/content/commands/zadd/index.md b/content/commands/zadd/index.md index 64ae9303a0..bb7c1be317 100644 --- a/content/commands/zadd/index.md +++ b/content/commands/zadd/index.md @@ -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 %}} - From e4d527feb255b729e12c59d9301c83d922e581cb Mon Sep 17 00:00:00 2001 From: "David W. Dougherty" Date: Wed, 21 Aug 2024 14:29:49 -0700 Subject: [PATCH 4/8] DOC-4040: (DOC-4081) changes for ZRANGE command --- content/commands/zrange/index.md | 50 ++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/content/commands/zrange/index.md b/content/commands/zrange/index.md index 16251c9779..dc75b36a43 100644 --- a/content/commands/zrange/index.md +++ b/content/commands/zrange/index.md @@ -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 %}} \ No newline at end of file From 51ff0a9eb142eb8b304eef5937b90961fea151a7 Mon Sep 17 00:00:00 2001 From: "David W. Dougherty" Date: Fri, 23 Aug 2024 09:08:17 -0700 Subject: [PATCH 5/8] DOC-4040: (DOC-4082) changes for EXPIRE command --- content/commands/expire/index.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/content/commands/expire/index.md b/content/commands/expire/index.md index 123ecc3d31..12d09a187a 100644 --- a/content/commands/expire/index.md +++ b/content/commands/expire/index.md @@ -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 From 0402b294969aeea7c85ebbeeccde7cc1cf0576f0 Mon Sep 17 00:00:00 2001 From: "David W. Dougherty" Date: Fri, 23 Aug 2024 09:47:17 -0700 Subject: [PATCH 6/8] DOC-4040: (DOC-4083) changes for TTL command --- content/commands/ttl/index.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/content/commands/ttl/index.md b/content/commands/ttl/index.md index b545991351..674eb4d16b 100644 --- a/content/commands/ttl/index.md +++ b/content/commands/ttl/index.md @@ -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 %}} - From 8e3f9cd3dcbc101ffa75b5f01fdac87b6d9b148f Mon Sep 17 00:00:00 2001 From: "David W. Dougherty" Date: Fri, 23 Aug 2024 10:13:53 -0700 Subject: [PATCH 7/8] DOC-4040: (DOC-4084) changes for DEL command --- content/commands/del/index.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/content/commands/del/index.md b/content/commands/del/index.md index 1f59c38933..d8053f5d0e 100644 --- a/content/commands/del/index.md +++ b/content/commands/del/index.md @@ -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" From a28dd54f2a9b27dac5aea9a4d15a77eb0b1050a0 Mon Sep 17 00:00:00 2001 From: "David W. Dougherty" Date: Mon, 26 Aug 2024 13:25:36 -0700 Subject: [PATCH 8/8] DOC-4040: (DOC-4079) changes for SCAN command --- content/commands/scan/index.md | 68 +++++++++++++++------------------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/content/commands/scan/index.md b/content/commands/scan/index.md index 1565c7ab4b..d9462b7eb1 100644 --- a/content/commands/scan/index.md +++ b/content/commands/scan/index.md @@ -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" @@ -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" @@ -155,33 +155,32 @@ To do so, just append the `MATCH ` 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" @@ -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. @@ -219,20 +217,20 @@ 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. @@ -240,20 +238,20 @@ It is important to note that the **TYPE** filter is also applied after elements 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 @@ -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 %}}