diff --git a/content/commands/exists.md b/content/commands/exists.md index a1faf80376..87695ca2ff 100644 --- a/content/commands/exists.md +++ b/content/commands/exists.md @@ -58,6 +58,16 @@ The user should be aware that if the same existing key is mentioned in the argum ## Examples +{{< clients-example set="cmds_generic" step="exists" >}} +SET key1 "Hello" +EXISTS key1 +EXISTS nosuchkey +SET key2 "World" +EXISTS key1 key2 nosuchkey +{{< /clients-example >}} + +Give these commands a try in the interactive console: + {{% redis-cli %}} SET key1 "Hello" EXISTS key1 diff --git a/local_examples/cmd_exists/NRedisStack/CmdsGenericExample.cs b/local_examples/cmd_exists/NRedisStack/CmdsGenericExample.cs new file mode 100644 index 0000000000..99c5887034 --- /dev/null +++ b/local_examples/cmd_exists/NRedisStack/CmdsGenericExample.cs @@ -0,0 +1,460 @@ +// EXAMPLE: cmds_generic +// HIDE_START + +using NRedisStack.Tests; +using StackExchange.Redis; + +// HIDE_END + +// REMOVE_START +namespace Doc; +[Collection("DocsTests")] +// REMOVE_END + +// HIDE_START +public class CmdsGenericExample +// REMOVE_START + (EndpointsFixture fixture) : AbstractNRedisStackTest(fixture), IDisposable +// REMOVE_END +{ + // REMOVE_START + + [SkipIfRedisFact(Comparison.LessThan, "7.0.0")] + // REMOVE_END + public void Run() + { + //REMOVE_START + // This is needed because we're constructing ConfigurationOptions in the test before calling GetConnection + SkipIfTargetConnectionDoesNotExist(EndpointsFixture.Env.Standalone); + var _ = GetCleanDatabase(EndpointsFixture.Env.Standalone); + //REMOVE_END + var muxer = ConnectionMultiplexer.Connect("localhost:6379"); + var db = muxer.GetDatabase(); + //REMOVE_START + // Clear any keys here before using them in tests. + + //REMOVE_END + // HIDE_END + + + // STEP_START copy + + // STEP_END + + // Tests for 'copy' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START del + bool delResult1 = db.StringSet("key1", "Hello"); + Console.WriteLine(delResult1); // >>> true + + bool delResult2 = db.StringSet("key2", "World"); + Console.WriteLine(delResult2); // >>> true + + long delResult3 = db.KeyDelete(["key1", "key2", "key3"]); + Console.WriteLine(delResult3); // >>> 2 + // STEP_END + + // Tests for 'del' step. + // REMOVE_START + Assert.True(delResult1); + Assert.True(delResult2); + Assert.Equal(2, delResult3); + // REMOVE_END + + + // STEP_START dump + + // STEP_END + + // Tests for 'dump' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START exists + bool existsResult1 = db.StringSet("key1", "Hello"); + Console.WriteLine(existsResult1); // >>> true + + bool existsResult2 = db.KeyExists("key1"); + Console.WriteLine(existsResult2); // >>> true + + bool existsResult3 = db.KeyExists("nosuchkey"); + Console.WriteLine(existsResult3); // >>> false + + bool existsResult4 = db.StringSet("key2", "World"); + Console.WriteLine(existsResult4); // >>> true + + long existsResult5 = db.KeyExists(["key1", "key2", "nosuchkey"]); + Console.WriteLine(existsResult5); // >>> 2 + // STEP_END + + // Tests for 'exists' step. + // REMOVE_START + Assert.True(existsResult1); + Assert.True(existsResult2); + Assert.False(existsResult3); + Assert.True(existsResult4); + Assert.Equal(2, existsResult5); + // REMOVE_END + + // STEP_START expire + bool expireResult1 = db.StringSet("mykey", "Hello"); + Console.WriteLine(expireResult1); // >>> true + + bool expireResult2 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10)); + Console.WriteLine(expireResult2); // >>> true + + TimeSpan expireResult3 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero; + Console.WriteLine(Math.Round(expireResult3.TotalSeconds)); // >>> 10 + + bool expireResult4 = db.StringSet("mykey", "Hello World"); + Console.WriteLine(expireResult4); // >>> true + + TimeSpan expireResult5 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero; + Console.WriteLine(Math.Round(expireResult5.TotalSeconds).ToString()); // >>> 0 + + bool expireResult6 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10), ExpireWhen.HasExpiry); + Console.WriteLine(expireResult6); // >>> false + + TimeSpan expireResult7 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero; + Console.WriteLine(Math.Round(expireResult7.TotalSeconds)); // >>> 0 + + bool expireResult8 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10), ExpireWhen.HasNoExpiry); + Console.WriteLine(expireResult8); // >>> true + + TimeSpan expireResult9 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero; + Console.WriteLine(Math.Round(expireResult9.TotalSeconds)); // >>> 10 + // STEP_END + + // Tests for 'expire' step. + // REMOVE_START + Assert.True(expireResult1); + Assert.True(expireResult2); + Assert.Equal(10, Math.Round(expireResult3.TotalSeconds)); + Assert.True(expireResult4); + Assert.Equal(0, Math.Round(expireResult5.TotalSeconds)); + Assert.False(expireResult6); + Assert.Equal(0, Math.Round(expireResult7.TotalSeconds)); + Assert.True(expireResult8); + Assert.Equal(10, Math.Round(expireResult9.TotalSeconds)); + db.KeyDelete("mykey"); + // REMOVE_END + + + // STEP_START expireat + + // STEP_END + + // Tests for 'expireat' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START expiretime + + // STEP_END + + // Tests for 'expiretime' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START keys + + // STEP_END + + // Tests for 'keys' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START migrate + + // STEP_END + + // Tests for 'migrate' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START move + + // STEP_END + + // Tests for 'move' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START object_encoding + + // STEP_END + + // Tests for 'object_encoding' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START object_freq + + // STEP_END + + // Tests for 'object_freq' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START object_idletime + + // STEP_END + + // Tests for 'object_idletime' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START object_refcount + + // STEP_END + + // Tests for 'object_refcount' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START persist + + // STEP_END + + // Tests for 'persist' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START pexpire + + // STEP_END + + // Tests for 'pexpire' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START pexpireat + + // STEP_END + + // Tests for 'pexpireat' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START pexpiretime + + // STEP_END + + // Tests for 'pexpiretime' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START pttl + + // STEP_END + + // Tests for 'pttl' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START randomkey + + // STEP_END + + // Tests for 'randomkey' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START rename + + // STEP_END + + // Tests for 'rename' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START renamenx + + // STEP_END + + // Tests for 'renamenx' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START restore + + // STEP_END + + // Tests for 'restore' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START scan1 + + // STEP_END + + // Tests for 'scan1' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START scan2 + + // STEP_END + + // Tests for 'scan2' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START scan3 + + // STEP_END + + // Tests for 'scan3' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START scan4 + + // STEP_END + + // Tests for 'scan4' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START sort + + // STEP_END + + // Tests for 'sort' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START sort_ro + + // STEP_END + + // Tests for 'sort_ro' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START touch + + // STEP_END + + // Tests for 'touch' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START ttl + bool ttlResult1 = db.StringSet("mykey", "Hello"); + Console.WriteLine(ttlResult1); // >>> true + + bool ttlResult2 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10)); + Console.WriteLine(ttlResult2); + + TimeSpan ttlResult3 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero; + string ttlRes = Math.Round(ttlResult3.TotalSeconds).ToString(); + Console.WriteLine(Math.Round(ttlResult3.TotalSeconds)); // >>> 10 + // STEP_END + + // Tests for 'ttl' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START type + + // STEP_END + + // Tests for 'type' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START unlink + + // STEP_END + + // Tests for 'unlink' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START wait + + // STEP_END + + // Tests for 'wait' step. + // REMOVE_START + + // REMOVE_END + + + // STEP_START waitaof + + // STEP_END + + // Tests for 'waitaof' step. + // REMOVE_START + + // REMOVE_END + + + // HIDE_START + } +} +// HIDE_END diff --git a/local_examples/cmd_exists/go-redis/cmds_generic_test.go b/local_examples/cmd_exists/go-redis/cmds_generic_test.go new file mode 100644 index 0000000000..c9b1131247 --- /dev/null +++ b/local_examples/cmd_exists/go-redis/cmds_generic_test.go @@ -0,0 +1,265 @@ +// EXAMPLE: cmds_generic +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + "math" + "time" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_del_cmd() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + // make sure we are working with fresh database + rdb.FlushDB(ctx) + rdb.Del(ctx, "key1", "key2", "key3") + // REMOVE_END + + // STEP_START del + delResult1, err := rdb.Set(ctx, "key1", "Hello", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(delResult1) // >>> OK + + delResult2, err := rdb.Set(ctx, "key2", "World", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(delResult2) // >>> OK + + delResult3, err := rdb.Del(ctx, "key1", "key2", "key3").Result() + + if err != nil { + panic(err) + } + + fmt.Println(delResult3) // >>> 2 + // STEP_END + + // Output: + // OK + // OK + // 2 +} + +func ExampleClient_exists_cmd() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + // make sure we are working with fresh database + rdb.FlushDB(ctx) + rdb.Del(ctx, "key1", "key2", "nosuchkey") + // REMOVE_END + + // STEP_START exists + existsResult1, err := rdb.Set(ctx, "key1", "Hello", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(existsResult1) // >>> OK + + existsResult2, err := rdb.Exists(ctx, "key1").Result() + + if err != nil { + panic(err) + } + + fmt.Println(existsResult2) // >>> 1 + + existsResult3, err := rdb.Exists(ctx, "nosuchkey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(existsResult3) // >>> 0 + + existsResult4, err := rdb.Set(ctx, "key2", "World", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(existsResult4) // >>> OK + + existsResult5, err := rdb.Exists(ctx, "key1", "key2", "nosuchkey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(existsResult5) // >>> 2 + // STEP_END + + // Output: + // OK + // 1 + // 0 + // OK + // 2 +} + +func ExampleClient_expire_cmd() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + // start with fresh database + rdb.FlushDB(ctx) + rdb.Del(ctx, "mykey") + // REMOVE_END + + // STEP_START expire + expireResult1, err := rdb.Set(ctx, "mykey", "Hello", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult1) // >>> OK + + expireResult2, err := rdb.Expire(ctx, "mykey", 10*time.Second).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult2) // >>> true + + expireResult3, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(math.Round(expireResult3.Seconds())) // >>> 10 + + expireResult4, err := rdb.Set(ctx, "mykey", "Hello World", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult4) // >>> OK + + expireResult5, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult5) // >>> -1ns + + expireResult6, err := rdb.ExpireXX(ctx, "mykey", 10*time.Second).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult6) // >>> false + + expireResult7, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult7) // >>> -1ns + + expireResult8, err := rdb.ExpireNX(ctx, "mykey", 10*time.Second).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult8) // >>> true + + expireResult9, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(math.Round(expireResult9.Seconds())) // >>> 10 + // STEP_END + + // Output: + // OK + // true + // 10 + // OK + // -1ns + // false + // -1ns + // true + // 10 +} + +func ExampleClient_ttl_cmd() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + // start with fresh database + rdb.FlushDB(ctx) + rdb.Del(ctx, "mykey") + // REMOVE_END + + // STEP_START ttl + ttlResult1, err := rdb.Set(ctx, "mykey", "Hello", 10*time.Second).Result() + + if err != nil { + panic(err) + } + + fmt.Println(ttlResult1) // >>> OK + + ttlResult2, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(math.Round(ttlResult2.Seconds())) // >>> 10 + // STEP_END + + // Output: + // OK + // 10 +} diff --git a/local_examples/cmd_exists/jedis/CmdsGenericExample.java b/local_examples/cmd_exists/jedis/CmdsGenericExample.java new file mode 100644 index 0000000000..eaee1d5290 --- /dev/null +++ b/local_examples/cmd_exists/jedis/CmdsGenericExample.java @@ -0,0 +1,140 @@ +// EXAMPLE: cmds_generic +// REMOVE_START +package io.redis.examples; + +import org.junit.jupiter.api.Test; +// REMOVE_END + +// HIDE_START +import redis.clients.jedis.UnifiedJedis; +import redis.clients.jedis.args.ExpiryOption; + +import static org.junit.jupiter.api.Assertions.assertEquals; +// HIDE_END + +// HIDE_START +public class CmdsGenericExample { + + @Test + public void run() { + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + + //REMOVE_START + // Clear any keys here before using them in tests. + //REMOVE_END +// HIDE_END + + // STEP_START del + String delResult1 = jedis.set("key1", "Hello"); + System.out.println(delResult1); // >>> OK + + String delResult2 = jedis.set("key2", "World"); + System.out.println(delResult2); // >>> OK + + long delResult3 = jedis.del("key1", "key2", "key3"); + System.out.println(delResult3); // >>> 2 + // STEP_END + + // Tests for 'del' step. + // REMOVE_START + assertEquals("OK", delResult1); + assertEquals("OK", delResult2); + assertEquals(2, delResult3); + // REMOVE_END + + + // STEP_START exists + String existsResult1 = jedis.set("key1", "Hello"); + System.out.println(existsResult1); // >>> OK + + boolean existsResult2 = jedis.exists("key1"); + System.out.println(existsResult2); // >>> true + + boolean existsResult3 = jedis.exists("nosuchkey"); + System.out.println(existsResult3); // >>> false + + String existsResult4 = jedis.set("key2", "World"); + System.out.println(existsResult4); // >>> OK + + long existsResult5 = jedis.exists("key1", "key2", "nosuchkey"); + System.out.println(existsResult5); // >>> 2 + // STEP_END + + // Tests for 'exists' step. + // REMOVE_START + assertEquals("OK", existsResult1); + assertEquals(true, existsResult2); + assertEquals(false, existsResult3); + assertEquals("OK", existsResult4); + assertEquals(2, existsResult5); + // REMOVE_END + + + // STEP_START expire + String expireResult1 = jedis.set("mykey", "Hello"); + System.out.println(expireResult1); // >>> OK + + long expireResult2 = jedis.expire("mykey", 10); + System.out.println(expireResult2); // >>> 1 + + long expireResult3 = jedis.ttl("mykey"); + System.out.println(expireResult3); // >>> 10 + + String expireResult4 = jedis.set("mykey", "Hello World"); + System.out.println(expireResult4); // >>> OK + + long expireResult5 = jedis.ttl("mykey"); + System.out.println(expireResult5); // >>> -1 + + long expireResult6 = jedis.expire("mykey", 10, ExpiryOption.XX); + System.out.println(expireResult6); // >>> 0 + + long expireResult7 = jedis.ttl("mykey"); + System.out.println(expireResult7); // >>> -1 + + long expireResult8 = jedis.expire("mykey", 10, ExpiryOption.NX); + System.out.println(expireResult8); // >>> 1 + + long expireResult9 = jedis.ttl("mykey"); + System.out.println(expireResult9); // >>> 10 + // STEP_END + + // Tests for 'expire' step. + // REMOVE_START + assertEquals("OK", expireResult1); + assertEquals(1, expireResult2); + assertEquals(10, expireResult3); + assertEquals("OK", expireResult4); + assertEquals(-1, expireResult5); + assertEquals(0, expireResult6); + assertEquals(-1, expireResult7); + assertEquals(1, expireResult8); + assertEquals(10, expireResult9); + jedis.del("mykey"); + // REMOVE_END + + + // STEP_START ttl + String ttlResult1 = jedis.set("mykey", "Hello"); + System.out.println(ttlResult1); // >>> OK + + long ttlResult2 = jedis.expire("mykey", 10); + System.out.println(ttlResult2); // >>> 1 + + long ttlResult3 = jedis.ttl("mykey"); + System.out.println(ttlResult3); // >>> 10 + // STEP_END + + // Tests for 'ttl' step. + // REMOVE_START + assertEquals("OK", ttlResult1); + assertEquals(1, ttlResult2); + assertEquals(10, ttlResult3); + jedis.del("mykey"); + // REMOVE_END + +// HIDE_START + jedis.close(); + } +} +// HIDE_END diff --git a/local_examples/cmd_exists/lettuce-async/CmdsGenericExample.java b/local_examples/cmd_exists/lettuce-async/CmdsGenericExample.java new file mode 100644 index 0000000000..a7a869cd07 --- /dev/null +++ b/local_examples/cmd_exists/lettuce-async/CmdsGenericExample.java @@ -0,0 +1,72 @@ +// EXAMPLE: cmds_generic +package io.redis.examples.async; + +import io.lettuce.core.*; + +import io.lettuce.core.api.async.RedisAsyncCommands; + +import io.lettuce.core.api.StatefulRedisConnection; + +import java.util.concurrent.CompletableFuture; +// REMOVE_START +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +// REMOVE_END + +public class CmdsGenericExample { + + // REMOVE_START + @Test + // REMOVE_END + public void run() { + // REMOVE_START + RedisClient redisClient = RedisClient.create("redis://localhost:6379"); + + try (StatefulRedisConnection connection = redisClient.connect()) { + RedisAsyncCommands asyncCommands = connection.async(); + asyncCommands.del("key1", "key2", "nosuchkey").toCompletableFuture().join(); + // REMOVE_END + + // STEP_START exists + CompletableFuture existsExample = asyncCommands.set("key1", "Hello").thenCompose(res1 -> { + System.out.println(res1); // >>> OK + // REMOVE_START + assertThat(res1).isEqualTo("OK"); + // REMOVE_END + + return asyncCommands.exists("key1"); + }).thenCompose(res2 -> { + System.out.println(res2); // >>> 1 + // REMOVE_START + assertThat(res2).isEqualTo(1L); + // REMOVE_END + + return asyncCommands.exists("nosuchkey"); + }).thenCompose(res3 -> { + System.out.println(res3); // >>> 0 + // REMOVE_START + assertThat(res3).isEqualTo(0L); + // REMOVE_END + + return asyncCommands.set("key2", "World"); + }).thenCompose(res4 -> { + System.out.println(res4); // >>> OK + // REMOVE_START + assertThat(res4).isEqualTo("OK"); + // REMOVE_END + + return asyncCommands.exists("key1", "key2", "nosuchkey"); + }).thenAccept(res5 -> { + System.out.println(res5); // >>> 2 + // REMOVE_START + assertThat(res5).isEqualTo(2L); + // REMOVE_END + }).toCompletableFuture(); + // STEP_END + existsExample.join(); + } finally { + redisClient.shutdown(); + } + } +} diff --git a/local_examples/cmd_exists/lettuce-reactive/CmdsGenericExample.java b/local_examples/cmd_exists/lettuce-reactive/CmdsGenericExample.java new file mode 100644 index 0000000000..a03e65634c --- /dev/null +++ b/local_examples/cmd_exists/lettuce-reactive/CmdsGenericExample.java @@ -0,0 +1,63 @@ +// EXAMPLE: cmds_generic +package io.redis.examples.reactive; + +import io.lettuce.core.*; +import io.lettuce.core.api.reactive.RedisReactiveCommands; +import io.lettuce.core.api.StatefulRedisConnection; +// REMOVE_START +import org.junit.jupiter.api.Test; +// REMOVE_END +import reactor.core.publisher.Mono; + +// REMOVE_START +import static org.assertj.core.api.Assertions.assertThat; +// REMOVE_END + +public class CmdsGenericExample { + + // REMOVE_START + @Test + // REMOVE_END + public void run() { + RedisClient redisClient = RedisClient.create("redis://localhost:6379"); + + try (StatefulRedisConnection connection = redisClient.connect()) { + RedisReactiveCommands reactiveCommands = connection.reactive(); + + // STEP_START exists + Mono existsExample = reactiveCommands.set("key1", "Hello").doOnNext(res1 -> { + System.out.println(res1); // >>> OK + // REMOVE_START + assertThat(res1).isEqualTo("OK"); + // REMOVE_END + }).then(reactiveCommands.exists("key1")).doOnNext(res2 -> { + System.out.println(res2); // >>> 1 + // REMOVE_START + assertThat(res2).isEqualTo(1L); + // REMOVE_END + }).then(reactiveCommands.exists("nosuchkey")).doOnNext(res3 -> { + System.out.println(res3); // >>> 0 + // REMOVE_START + assertThat(res3).isEqualTo(0L); + // REMOVE_END + }).then(reactiveCommands.set("key2", "World")).doOnNext(res4 -> { + System.out.println(res4); // >>> OK + // REMOVE_START + assertThat(res4).isEqualTo("OK"); + // REMOVE_END + }).then(reactiveCommands.exists("key1", "key2", "nosuchkey")).doOnNext(res5 -> { + System.out.println(res5); // >>> 2 + // REMOVE_START + assertThat(res5).isEqualTo(2L); + // REMOVE_END + }).then(); + // STEP_END + + Mono.when(existsExample).block(); + + } finally { + redisClient.shutdown(); + } + } + +} diff --git a/local_examples/cmd_exists/node-redis/cmds_generic.js b/local_examples/cmd_exists/node-redis/cmds_generic.js new file mode 100644 index 0000000000..ba35a19841 --- /dev/null +++ b/local_examples/cmd_exists/node-redis/cmds_generic.js @@ -0,0 +1,186 @@ +// EXAMPLE: cmds_generic +// REMOVE_START +import assert from "node:assert"; +// REMOVE_END + +// HIDE_START +import { createClient } from 'redis'; + +const client = createClient(); +await client.connect().catch(console.error); +// HIDE_END + +// STEP_START del +const delRes1 = await client.set('key1', 'Hello'); +console.log(delRes1); // OK + +const delRes2 = await client.set('key2', 'World'); +console.log(delRes2); // OK + +const delRes3 = await client.del(['key1', 'key2', 'key3']); +console.log(delRes3); // 2 +// REMOVE_START +assert.equal(delRes3, 2); +// REMOVE_END +// STEP_END + +// STEP_START exists +const existsRes1 = await client.set('key1', 'Hello'); +console.log(existsRes1); // OK + +const existsRes2 = await client.exists('key1'); +console.log(existsRes2); // 1 + +const existsRes3 = await client.exists('nosuchkey'); +console.log(existsRes3); // 0 + +const existsRes4 = await client.set('key2', 'World'); +console.log(existsRes4); // OK + +const existsRes5 = await client.exists(['key1', 'key2', 'nosuchkey']); +console.log(existsRes5); // 2 +// REMOVE_START +assert.equal(existsRes5, 2); +await client.del(['key1', 'key2']); +// REMOVE_END +// STEP_END + +// STEP_START expire +const expireRes1 = await client.set('mykey', 'Hello'); +console.log(expireRes1); // OK + +const expireRes2 = await client.expire('mykey', 10); +console.log(expireRes2); // 1 + +const expireRes3 = await client.ttl('mykey'); +console.log(expireRes3); // 10 + +const expireRes4 = await client.set('mykey', 'Hello World'); +console.log(expireRes4); // OK + +const expireRes5 = await client.ttl('mykey'); +console.log(expireRes5); // -1 + +const expireRes6 = await client.expire('mykey', 10, "XX"); +console.log(expireRes6); // 0 + +const expireRes7 = await client.ttl('mykey'); +console.log(expireRes7); // -1 + +const expireRes8 = await client.expire('mykey', 10, "NX"); +console.log(expireRes8); // 1 + +const expireRes9 = await client.ttl('mykey'); +console.log(expireRes9); // 10 +// REMOVE_START +await client.del('mykey'); +// REMOVE_END +// STEP_END + +// STEP_START ttl +const ttlRes1 = await client.set('mykey', 'Hello'); +console.log(ttlRes1); // OK + +const ttlRes2 = await client.expire('mykey', 10); +console.log(ttlRes2); // 1 + +const ttlRes3 = await client.ttl('mykey'); +console.log(ttlRes3); // 10 +// REMOVE_START +await client.del('mykey'); +// REMOVE_END +// STEP_END + +// STEP_START scan1 +const scan1Res1 = await client.sAdd('myset', ['1', '2', '3', 'foo', 'foobar', 'feelsgood']); +console.log(scan1Res1); // 6 + +let scan1Res2 = []; +for await (const values of client.sScanIterator('myset', { MATCH: 'f*' })) { + scan1Res2 = scan1Res2.concat(values); +} +console.log(scan1Res2); // ['foo', 'foobar', 'feelsgood'] +// REMOVE_START +console.assert(scan1Res2.sort().toString() === ['foo', 'foobar', 'feelsgood'].sort().toString()); +await client.del('myset'); +// REMOVE_END +// STEP_END + +// STEP_START scan2 +// REMOVE_START +for (let i = 1; i <= 1000; i++) { + await client.set(`key:${i}`, i); +} +// REMOVE_END + +let cursor = '0'; +let scanResult; + +scanResult = await client.scan(cursor, { MATCH: '*11*' }); +console.log(scanResult.cursor, scanResult.keys); + +scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' }); +console.log(scanResult.cursor, scanResult.keys); + +scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' }); +console.log(scanResult.cursor, scanResult.keys); + +scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' }); +console.log(scanResult.cursor, scanResult.keys); + +scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*', COUNT: 1000 }); +console.log(scanResult.cursor, scanResult.keys); +// REMOVE_START +console.assert(scanResult.keys.length === 18); +cursor = '0'; +const prefix = 'key:*'; +do { + scanResult = await client.scan(cursor, { MATCH: prefix, COUNT: 1000 }); + console.log(scanResult.cursor, scanResult.keys); + cursor = scanResult.cursor; + const keys = scanResult.keys; + if (keys.length) { + await client.del(keys); + } +} while (cursor !== '0'); +// REMOVE_END +// STEP_END + +// STEP_START scan3 +const scan3Res1 = await client.geoAdd('geokey', { longitude: 0, latitude: 0, member: 'value' }); +console.log(scan3Res1); // 1 + +const scan3Res2 = await client.zAdd('zkey', [{ score: 1000, value: 'value' }]); +console.log(scan3Res2); // 1 + +const scan3Res3 = await client.type('geokey'); +console.log(scan3Res3); // zset + +const scan3Res4 = await client.type('zkey'); +console.log(scan3Res4); // zset + +const scan3Res5 = await client.scan('0', { TYPE: 'zset' }); +console.log(scan3Res5.keys); // ['zkey', 'geokey'] +// REMOVE_START +await client.del(['geokey', 'zkey']); +// REMOVE_END +// STEP_END + +// STEP_START scan4 +const scan4Res1 = await client.hSet('myhash', { a: 1, b: 2 }); +console.log(scan4Res1); // 2 + +const scan4Res2 = await client.hScan('myhash', '0'); +console.log(scan4Res2.entries); // [{field: 'a', value: '1'}, {field: 'b', value: '2'}] + +const scan4Res3 = await client.hScan('myhash', '0', { COUNT: 10 }); +const items = scan4Res3.entries.map((item) => item.field) +console.log(items); // ['a', 'b'] +// REMOVE_START +await client.del('myhash'); +// REMOVE_END +// STEP_END + +// HIDE_START +await client.close(); +// HIDE_END diff --git a/local_examples/cmd_exists/predis/CmdsGenericTest.php b/local_examples/cmd_exists/predis/CmdsGenericTest.php new file mode 100644 index 0000000000..e83b738224 --- /dev/null +++ b/local_examples/cmd_exists/predis/CmdsGenericTest.php @@ -0,0 +1,49 @@ +// EXAMPLE: cmds_generic + 'tcp', + 'host' => '127.0.0.1', + 'port' => 6379, + 'password' => '', + 'database' => 0, + ]); + // REMOVE_START + $r->flushall(); + // REMOVE_END + + // STEP_START exists + $existsResult1 = $r->set('key1', 'Hello'); + echo $existsResult1 . PHP_EOL; // >>> OK + + $existsResult2 = $r->exists('key1'); + echo $existsResult2 . PHP_EOL; // >>> 1 + + $existsResult3 = $r->exists('nosuchkey'); + echo $existsResult3 . PHP_EOL; // >>> 0 + + $existsResult4 = $r->set('key2', 'World'); + echo $existsResult4 . PHP_EOL; // >>> OK + + $existsResult5 = $r->exists('key1', 'key2', 'nosuchkey'); + echo $existsResult5 . PHP_EOL; // >>> 2 + // STEP_END + // REMOVE_START + $this->assertEquals('OK', $existsResult1); + $this->assertEquals(1, $existsResult2); + $this->assertEquals(0, $existsResult3); + $this->assertEquals('OK', $existsResult4); + $this->assertEquals(2, $existsResult5); + // REMOVE_END + + } +} diff --git a/local_examples/cmd_exists/redis-py/cmds_generic.py b/local_examples/cmd_exists/redis-py/cmds_generic.py new file mode 100644 index 0000000000..6ea3333753 --- /dev/null +++ b/local_examples/cmd_exists/redis-py/cmds_generic.py @@ -0,0 +1,214 @@ +# EXAMPLE: cmds_generic +# HIDE_START +import redis + +r = redis.Redis(decode_responses=True) +# HIDE_END + +# STEP_START del +res = r.set("key1", "Hello") +print(res) +# >>> True + +res = r.set("key2", "World") +print(res) +# >>> True + +res = r.delete("key1", "key2", "key3") +print(res) +# >>> 2 +# REMOVE_START +assert res == 2 +# REMOVE_END +# STEP_END + +# STEP_START exists +res = r.set("key1", "Hello") +print(res) +# >>> True + +res = r.exists("key1") +print(res) +# >>> 1 + +res = r.exists("nosuchkey") +print(res) +# >>> 0 + +res = r.set("key2", "World") +print(res) +# >>> True + +res = r.exists("key1", "key2", "nosuchkey") +print(res) +# >>> 2 +# REMOVE_START +assert r.exists("key1", "key2", "nosuchkey") == 2 +r.delete("key1", "key2") +# REMOVE_END +# STEP_END + +# STEP_START expire +res = r.set("mykey", "Hello") +print(res) +# >>> True + +res = r.expire("mykey", 10) +print(res) +# >>> True + +res = r.ttl("mykey") +print(res) +# >>> 10 +# REMOVE_START +assert res == 10 +# REMOVE_END + +res = r.set("mykey", "Hello World") +print(res) +# >>> True + +res = r.ttl("mykey") +print(res) +# >>> -1 +# REMOVE_START +assert res == -1 +# REMOVE_END + +res = r.expire("mykey", 10, xx=True) +print(res) +# >>> False +# REMOVE_START +assert res == False +# REMOVE_END + +res = r.ttl("mykey") +print(res) +# >>> -1 +# REMOVE_START +assert res == -1 +# REMOVE_END + +res = r.expire("mykey", 10, nx=True) +print(res) +# >>> True +# REMOVE_START +assert res == True +# REMOVE_END + +res = r.ttl("mykey") +print(res) +# >>> 10 +# REMOVE_START +assert res == 10 +r.delete("mykey") +# REMOVE_END +# STEP_END + +# STEP_START ttl +res = r.set("mykey", "Hello") +print(res) +# >>> True + +res = r.expire("mykey", 10) +print(res) +# >>> True + +res = r.ttl("mykey") +print(res) +# >>> 10 +# REMOVE_START +assert res == 10 +r.delete("mykey") +# REMOVE_END +# STEP_END + +# STEP_START scan1 +res = r.sadd("myset", *set([1, 2, 3, "foo", "foobar", "feelsgood"])) +print(res) +# >>> 6 + +res = list(r.sscan_iter("myset", match="f*")) +print(res) +# >>> ['foobar', 'foo', 'feelsgood'] +# REMOVE_START +assert set(res) == {"foo", "foobar", "feelsgood"} +r.delete("myset") +# REMOVE_END +# STEP_END + +# STEP_START scan2 +# REMOVE_START +for i in range(1, 1001): + r.set(f"key:{i}", i) +# REMOVE_END + +cursor, key = r.scan(cursor=0, match='*11*') +print(cursor, key) + +cursor, key = r.scan(cursor, match='*11*') +print(cursor, key) + +cursor, key = r.scan(cursor, match='*11*') +print(cursor, key) + +cursor, key = r.scan(cursor, match='*11*') +print(cursor, key) + +cursor, keys = r.scan(cursor, match='*11*', count=1000) +print(cursor, keys) + +# REMOVE_START +assert len(keys) == 18 +cursor = '0' +prefix = "key:*" +while cursor != 0: + cursor, keys = r.scan(cursor=cursor, match=prefix, count=1000) + if keys: + r.delete(*keys) +# REMOVE_END +# STEP_END + +# STEP_START scan3 +res = r.geoadd("geokey", (0, 0, "value")) +print(res) +# >>> 1 + +res = r.zadd("zkey", {"value": 1000}) +print(res) +# >>> 1 + +res = r.type("geokey") +print(res) +# >>> zset + +res = r.type("zkey") +print(res) +# >>> zset + +cursor, keys = r.scan(cursor=0, _type="zset") +print(keys) +# >>> ['zkey', 'geokey'] +# REMOVE_START +assert set(keys) == {"zkey", "geokey"} +r.delete("geokey", "zkey") +# REMOVE_END +# STEP_END + +# STEP_START scan4 +res = r.hset("myhash", mapping={"a": 1, "b": 2}) +print(res) +# >>> 2 + +cursor, keys = r.hscan("myhash", 0) +print(keys) +# >>> {'a': '1', 'b': '2'} + +cursor, keys = r.hscan("myhash", 0, no_values=True) +print(keys) +# >>> ['a', 'b'] +# REMOVE_START +assert keys == ['a', 'b'] +r.delete("myhash") +# REMOVE_END +# STEP_END