diff --git a/content/commands/exists.md b/content/commands/exists.md index a1faf8037..87695ca2f 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/new_tces/NRedisStack/CmdsGenericExample.cs b/local_examples/new_tces/NRedisStack/CmdsGenericExample.cs new file mode 100644 index 000000000..11723b022 --- /dev/null +++ b/local_examples/new_tces/NRedisStack/CmdsGenericExample.cs @@ -0,0 +1,156 @@ +// EXAMPLE: cmds_generic +// HIDE_START +using NRedisStack.Tests; +using StackExchange.Redis; + +public class CmdsGenericExample +{ + public void Run() + { + var muxer = ConnectionMultiplexer.Connect("localhost:6379"); + var db = muxer.GetDatabase(); +// HIDE_END + + // Tests for 'copy' step. + + // 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 + + // Tests for 'del' step. + // STEP_END + + // Tests for 'dump' step. + + // 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 + + // Tests for 'exists' step. + // STEP_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 + + // Tests for 'expire' step. + // STEP_END + + // Tests for 'expireat' step. + + // Tests for 'expiretime' step. + + // Tests for 'keys' step. + + // Tests for 'migrate' step. + + // Tests for 'move' step. + + // Tests for 'object_encoding' step. + + // Tests for 'object_freq' step. + + // Tests for 'object_idletime' step. + + // Tests for 'object_refcount' step. + + // Tests for 'persist' step. + + // Tests for 'pexpire' step. + + // Tests for 'pexpireat' step. + + // Tests for 'pexpiretime' step. + + // Tests for 'pttl' step. + + // Tests for 'randomkey' step. + + // Tests for 'rename' step. + + // Tests for 'renamenx' step. + + // Tests for 'restore' step. + + // Tests for 'scan1' step. + + // Tests for 'scan2' step. + + // Tests for 'scan3' step. + + // Tests for 'scan4' step. + + // Tests for 'sort' step. + + // Tests for 'sort_ro' step. + + // Tests for 'touch' step. + + // 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 + + // Tests for 'ttl' step. + // STEP_END + + // Tests for 'type' step. + + // Tests for 'unlink' step. + + // Tests for 'wait' step. + + // Tests for 'waitaof' step. + +// HIDE_START + } +} +// HIDE_END diff --git a/local_examples/new_tces/go-redis/cmds_generic_test.go b/local_examples/new_tces/go-redis/cmds_generic_test.go new file mode 100644 index 000000000..8edc79417 --- /dev/null +++ b/local_examples/new_tces/go-redis/cmds_generic_test.go @@ -0,0 +1,262 @@ +// EXAMPLE: cmds_generic +package example_commands_test + +import ( + "context" + "fmt" + "math" + "time" + + "github.com/redis/go-redis/v9" +) + +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 +} + +// STEP_START exists +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 + + 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 + + // Output: + // OK + // 1 + // 0 + // OK + // 2 +} +// STEP_END + +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/new_tces/jedis/CmdsGenericExample.java b/local_examples/new_tces/jedis/CmdsGenericExample.java new file mode 100644 index 000000000..54bb2eb50 --- /dev/null +++ b/local_examples/new_tces/jedis/CmdsGenericExample.java @@ -0,0 +1,94 @@ +// EXAMPLE: cmds_generic +// HIDE_START +import redis.clients.jedis.UnifiedJedis; +import redis.clients.jedis.args.ExpiryOption; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CmdsGenericExample { + + public void run() { + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); +// 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 + + // Tests for 'del' step. + // STEP_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 + + // Tests for 'exists' step. + // STEP_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 + + // Tests for 'expire' step. + // STEP_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 + + // Tests for 'ttl' step. + // STEP_END + +// HIDE_START + jedis.close(); + } +} +// HIDE_END diff --git a/local_examples/new_tces/lettuce/CmdsGeneric-lettuce-async-Example.java b/local_examples/new_tces/lettuce/CmdsGeneric-lettuce-async-Example.java new file mode 100644 index 000000000..bbc33d46b --- /dev/null +++ b/local_examples/new_tces/lettuce/CmdsGeneric-lettuce-async-Example.java @@ -0,0 +1,64 @@ +// EXAMPLE: cmds_generic +// HIDE_START +import io.lettuce.core.RedisClient; +import io.lettuce.core.api.StatefulRedisConnection; +import io.lettuce.core.api.async.RedisAsyncCommands; + +import java.util.concurrent.CompletableFuture; + +import static org.junit.jupiter.api.Assertions.assertThat; + +public class CmdsGenericAsyncExample { + + public void run() { + RedisClient redisClient = RedisClient.create("redis://localhost:6379"); + + try (StatefulRedisConnection connection = redisClient.connect()) { + RedisAsyncCommands asyncCommands = connection.async(); +// HIDE_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 + +// HIDE_START + existsExample.join(); + } finally { + redisClient.shutdown(); + } + } +} +// HIDE_END diff --git a/local_examples/new_tces/lettuce/CmdsGeneric-lettuce-reactive-Example.java b/local_examples/new_tces/lettuce/CmdsGeneric-lettuce-reactive-Example.java new file mode 100644 index 000000000..62ab5d722 --- /dev/null +++ b/local_examples/new_tces/lettuce/CmdsGeneric-lettuce-reactive-Example.java @@ -0,0 +1,55 @@ +// EXAMPLE: cmds_generic +// HIDE_START +import io.lettuce.core.RedisClient; +import io.lettuce.core.api.StatefulRedisConnection; +import io.lettuce.core.api.reactive.RedisReactiveCommands; +import reactor.core.publisher.Mono; + +import static org.junit.jupiter.api.Assertions.assertThat; + +public class CmdsGenericReactiveExample { + + public void run() { + RedisClient redisClient = RedisClient.create("redis://localhost:6379"); + + try (StatefulRedisConnection connection = redisClient.connect()) { + RedisReactiveCommands reactiveCommands = connection.reactive(); +// HIDE_END + + // 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 + +// HIDE_START + existsExample.block(); + } finally { + redisClient.shutdown(); + } + } +} +// HIDE_END diff --git a/local_examples/new_tces/node-redis/cmds_generic.js b/local_examples/new_tces/node-redis/cmds_generic.js new file mode 100644 index 000000000..ba35a1984 --- /dev/null +++ b/local_examples/new_tces/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/new_tces/predis/CmdsGenericExample.php b/local_examples/new_tces/predis/CmdsGenericExample.php new file mode 100644 index 000000000..6bade2649 --- /dev/null +++ b/local_examples/new_tces/predis/CmdsGenericExample.php @@ -0,0 +1,74 @@ +// EXAMPLE: cmds_generic + 'tcp', + 'host' => '127.0.0.1', + 'port' => 6379, +]); +// HIDE_END + +// STEP_START del +$delResult1 = $client->set('key1', 'Hello'); +echo $delResult1 . "\n"; // >>> OK + +$delResult2 = $client->set('key2', 'World'); +echo $delResult2 . "\n"; // >>> OK + +$delResult3 = $client->del(['key1', 'key2', 'key3']); +echo $delResult3 . "\n"; // >>> 2 +// STEP_END + +// STEP_START exists +$existsResult1 = $client->set('key1', 'Hello'); +echo $existsResult1 . "\n"; // >>> OK + +$existsResult2 = $client->exists('key1'); +echo $existsResult2 . "\n"; // >>> 1 + +$existsResult3 = $client->exists('nosuchkey'); +echo $existsResult3 . "\n"; // >>> 0 + +$existsResult4 = $client->set('key2', 'World'); +echo $existsResult4 . "\n"; // >>> OK + +$existsResult5 = $client->exists(['key1', 'key2', 'nosuchkey']); +echo $existsResult5 . "\n"; // >>> 2 +// STEP_END + +// STEP_START expire +$expireResult1 = $client->set('mykey', 'Hello'); +echo $expireResult1 . "\n"; // >>> OK + +$expireResult2 = $client->expire('mykey', 10); +echo $expireResult2 . "\n"; // >>> 1 + +$expireResult3 = $client->ttl('mykey'); +echo $expireResult3 . "\n"; // >>> 10 + +$expireResult4 = $client->set('mykey', 'Hello World'); +echo $expireResult4 . "\n"; // >>> OK + +$expireResult5 = $client->ttl('mykey'); +echo $expireResult5 . "\n"; // >>> -1 +// STEP_END + +// STEP_START ttl +$ttlResult1 = $client->set('mykey', 'Hello'); +echo $ttlResult1 . "\n"; // >>> OK + +$ttlResult2 = $client->expire('mykey', 10); +echo $ttlResult2 . "\n"; // >>> 1 + +$ttlResult3 = $client->ttl('mykey'); +echo $ttlResult3 . "\n"; // >>> 10 +// STEP_END + +// HIDE_START +$client->disconnect(); +// HIDE_END +?> diff --git a/local_examples/new_tces/redis-py/cmds_generic.py b/local_examples/new_tces/redis-py/cmds_generic.py new file mode 100644 index 000000000..6ea333375 --- /dev/null +++ b/local_examples/new_tces/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