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
61 changes: 58 additions & 3 deletions local_examples/cmds_hash/NRedisStack/CmdsHashExample.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// EXAMPLE: cmds_hash
using StackExchange.Redis;
using Xunit;
using System.Linq;

namespace Doc;

Expand All @@ -22,12 +23,14 @@ public void Run()

RedisValue hdelRes3 = db.HashDelete("myhash", "field1");
Console.WriteLine(hdelRes3); // >>> 0

// STEP_END

// REMOVE_START
Assert.True(hdelRes1);
Assert.Equal(1, hdelRes2);
Assert.Equal(0, hdelRes3);
db.KeyDelete("myhash");
// REMOVE_END

// STEP_START hget
bool hgetRes1 = db.HashSet("myhash", "field1", "foo");
Expand All @@ -37,12 +40,14 @@ public void Run()

RedisValue hgetRes3 = db.HashGet("myhash", "field2");
Console.WriteLine(hgetRes3); // >>> Null

// STEP_END

// REMOVE_START
Assert.True(hgetRes1);
Assert.Equal("foo", hgetRes2);
Assert.Equal(RedisValue.Null, hgetRes3);
db.KeyDelete("myhash");
// REMOVE_END

// STEP_START hset
bool hsetRes1 = db.HashSet("myhash", "field1", "Hello");
Expand All @@ -66,8 +71,9 @@ public void Run()
HashEntry[] hsetRes5 = db.HashGetAll("myhash");
Console.WriteLine($"{string.Join(", ", hsetRes5.Select(h => $"{h.Name}: {h.Value}"))}");
// >>> field1: Hello, field2: Hi, field3: World

// STEP_END

// REMOVE_START
Assert.True(hsetRes1);
Assert.Equal("Hello", hsetRes2);
Assert.Equal("Hi", hsetRes3);
Expand All @@ -77,6 +83,7 @@ public void Run()
string.Join(", ", hsetRes5.Select(h => $"{h.Name}: {h.Value}"))
);
db.KeyDelete("myhash");
// REMOVE_END

// STEP_START hgetall
db.HashSet("myhash",
Expand All @@ -93,8 +100,11 @@ public void Run()
);
// >>> field1: Hello, field2: World
// STEP_END

// REMOVE_START
Assert.Equal("field1: Hello, field2: World", string.Join(", ", hGetAllResult.Select(e => $"{e.Name}: {e.Value}")));
db.KeyDelete("myhash");
// REMOVE_END

// STEP_START hvals
db.HashSet("myhash",
Expand All @@ -109,6 +119,51 @@ public void Run()
Console.WriteLine(string.Join(", ", hValsResult));
// >>> Hello, World
// STEP_END

// REMOVE_START
Assert.Equal("Hello, World", string.Join(", ", hValsResult));
db.KeyDelete("myhash");
// REMOVE_END

// STEP_START hexpire
// Set up hash with fields
db.HashSet("myhash",
[
new("field1", "Hello"),
new("field2", "World")
]
);

ExpireResult[] hexpireRes1 = db.HashFieldExpire(
"myhash",
new RedisValue[] { "field1", "field2" },
TimeSpan.FromSeconds(10)
);
Console.WriteLine(string.Join(", ", hexpireRes1));
// >>> Success, Success

long[] hexpireRes2 = db.HashFieldGetTimeToLive(
"myhash",
new RedisValue[] { "field1", "field2" }
);
Console.WriteLine(string.Join(", ", hexpireRes2));
// >>> 10, 10 (approximately)

// Try to set expiration on non-existent field
ExpireResult[] hexpireRes3 = db.HashFieldExpire(
"myhash",
new RedisValue[] { "nonexistent" },
TimeSpan.FromSeconds(10)
);
Console.WriteLine(string.Join(", ", hexpireRes3));
// >>> NoSuchField
// STEP_END
// REMOVE_START
Assert.Equal("Success, Success", string.Join(", ", hexpireRes1));
Assert.Equal(2, hexpireRes2.Length);
Assert.True(hexpireRes2.All(ttl => ttl > 0)); // TTL should be positive
Assert.Equal("NoSuchField", string.Join(", ", hexpireRes3));
db.KeyDelete("myhash");
// REMOVE_END
}
}
51 changes: 51 additions & 0 deletions local_examples/cmds_hash/go-redis/cmds_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"fmt"
"sort"
"time"

"github.com/redis/go-redis/v9"
)
Expand Down Expand Up @@ -293,4 +294,54 @@ func ExampleClient_hdel() {
// 1
// 1
// 0
}

func ExampleClient_hexpire() {
ctx := context.Background()

rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password
DB: 0, // use default DB
})

// STEP_START hexpire
// Set up hash with fields
rdb.HSet(ctx, "myhash", "field1", "Hello", "field2", "World")

// Set expiration on hash fields
res1, err := rdb.HExpire(ctx, "myhash", 10*time.Second, "field1", "field2").Result()

if err != nil {
fmt.Println(err)
}

fmt.Println(res1) // >>> [1 1]

// Check TTL of the fields
res2, err := rdb.HTTL(ctx, "myhash", "field1", "field2").Result()

if err != nil {
fmt.Println(err)
}

fmt.Println(len(res2)) // >>> 2

// Try to set expiration on non-existent field
res3, err := rdb.HExpire(ctx, "myhash", 10*time.Second, "nonexistent").Result()

if err != nil {
fmt.Println(err)
}

fmt.Println(res3) // >>> [-2]

// Clean up
rdb.Del(ctx, "myhash")
// STEP_END

// Output:
// [1 1]
// 2
// [-2]
}
32 changes: 31 additions & 1 deletion local_examples/cmds_hash/jedis/CmdsHashExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;
import java.util.List;
import java.util.Collections;
import java.util.Arrays;

// HIDE_START
import redis.clients.jedis.UnifiedJedis;
Expand All @@ -17,6 +18,7 @@
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

// HIDE_START
public class CmdsHashExample {
Expand Down Expand Up @@ -159,13 +161,41 @@ public void run() {
System.out.println(hValsResult2);
// >>> [Hello, World]
// STEP_END
// REMOVE_START
// REMOVE_START
// Tests for 'hvals' step.
assertEquals(2, hValsResult1);
assertEquals("[Hello, World]", hValsResult2.toString());
jedis.del("myhash");
// REMOVE_END

// STEP_START hexpire
// Set up hash with fields
Map<String, String> hExpireExampleParams = new HashMap<>();
hExpireExampleParams.put("field1", "Hello");
hExpireExampleParams.put("field2", "World");
jedis.hset("myhash", hExpireExampleParams);

// Set expiration on hash fields
List<Long> hExpireResult1 = jedis.hexpire("myhash", 10, "field1", "field2");
System.out.println(hExpireResult1); // >>> [1, 1]

// Check TTL of the fields
List<Long> hExpireResult2 = jedis.httl("myhash", "field1", "field2");
System.out.println(hExpireResult2.size()); // >>> 2

// Try to set expiration on non-existent field
List<Long> hExpireResult3 = jedis.hexpire("myhash", 10, "nonexistent");
System.out.println(hExpireResult3); // >>> [-2]
// STEP_END
// REMOVE_START
// Tests for 'hexpire' step.
assertEquals(Arrays.asList(1L, 1L), hExpireResult1);
assertEquals(2, hExpireResult2.size());
assertTrue(hExpireResult2.stream().allMatch(ttl -> ttl > 0)); // TTL should be positive
assertEquals(Arrays.asList(-2L), hExpireResult3);
jedis.del("myhash");
// REMOVE_END

// HIDE_START
jedis.close();
}
Expand Down
46 changes: 46 additions & 0 deletions local_examples/cmds_hash/lettuce-async/CmdsHashExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,52 @@ public void run() {
// REMOVE_START
asyncCommands.del("myhash").toCompletableFuture().join();
// REMOVE_END

// STEP_START hexpire
// Set up hash with fields
Map<String, String> hExpireExampleParams = new HashMap<>();
hExpireExampleParams.put("field1", "Hello");
hExpireExampleParams.put("field2", "World");

CompletableFuture<Void> hExpireExample = asyncCommands.hset("myhash", hExpireExampleParams).thenCompose(res1 -> {
// REMOVE_START
assertThat(res1).isEqualTo(2L);
// REMOVE_END
// Set expiration on hash fields
return asyncCommands.hexpire("myhash", 10, "field1", "field2");
}).thenCompose(res2 -> {
System.out.println(res2);
// >>> [1, 1]
// REMOVE_START
assertThat(res2).isEqualTo(Arrays.asList(1L, 1L));
// REMOVE_END
// Check TTL of the fields
return asyncCommands.httl("myhash", "field1", "field2");
}).thenCompose(res3 -> {
System.out.println(res3.size());
// >>> 2
// REMOVE_START
assertThat(res3.size()).isEqualTo(2);
assertThat(res3.stream().allMatch(ttl -> ttl > 0)).isTrue(); // TTL should be positive
// REMOVE_END
// Try to set expiration on non-existent field
return asyncCommands.hexpire("myhash", 10, "nonexistent");
})
// REMOVE_START
.thenApply(res4 -> {
assertThat(res4).isEqualTo(Arrays.asList(-2L));
return res;
})
// REMOVE_END
.thenAccept(System.out::println)
// >>> -2
.toCompletableFuture();
// STEP_END

hExpireExample.join();
// REMOVE_START
asyncCommands.del("myhash").toCompletableFuture().join();
// REMOVE_END
} finally {
redisClient.shutdown();
}
Expand Down
52 changes: 52 additions & 0 deletions local_examples/cmds_hash/lettuce-reactive/CmdsHashExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,58 @@ public void run() {
// REMOVE_START
reactiveCommands.del("myhash").block();
// REMOVE_END

// STEP_START hexpire
// Set up hash with fields
Map<String, String> hExpireExampleParams = new HashMap<>();
hExpireExampleParams.put("field1", "Hello");
hExpireExampleParams.put("field2", "World");

Mono<Long> hExpireExample1 = reactiveCommands.hset("myhash", hExpireExampleParams).doOnNext(result -> {
// REMOVE_START
assertThat(result).isEqualTo(2L);
// REMOVE_END
});

hExpireExample1.block();

// Set expiration on hash fields
Mono<List<Long>> hExpireExample2 = reactiveCommands.hexpire("myhash", 10, "field1", "field2").collectList().doOnNext(result -> {
System.out.println(result);
// >>> [1, 1]
// REMOVE_START
assertThat(result).isEqualTo(Arrays.asList(1L, 1L));
// REMOVE_END
});

hExpireExample2.block();

// Check TTL of the fields
Mono<List<Long>> hExpireExample3 = reactiveCommands.httl("myhash", "field1", "field2").collectList().doOnNext(result -> {
System.out.println(result.size());
// >>> 2
// REMOVE_START
assertThat(result.size()).isEqualTo(2);
assertThat(result.stream().allMatch(ttl -> ttl > 0)).isTrue(); // TTL should be positive
// REMOVE_END
});

hExpireExample3.block();

// Try to set expiration on non-existent field
Mono<List<Long>> hExpireExample4 = reactiveCommands.hexpire("myhash", 10, "nonexistent").collectList().doOnNext(result -> {
System.out.println(result);
// >>> [-2]
// REMOVE_START
assertThat(result).isEqualTo(Arrays.asList(-2L));
// REMOVE_END
});
// STEP_END

hExpireExample4.block();
// REMOVE_START
reactiveCommands.del("myhash").block();
// REMOVE_END
} finally {
redisClient.shutdown();
}
Expand Down
27 changes: 27 additions & 0 deletions local_examples/cmds_hash/node-redis/cmds-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,33 @@ await client.del('myhash')
// REMOVE_END
// STEP_END

// STEP_START hexpire
// Set up hash with fields
await client.hSet('myhash', {
'field1': 'Hello',
'field2': 'World'
})

// Set expiration on hash fields
const res14 = await client.hExpire('myhash', ['field1', 'field2'], 10)
console.log(res14) // [1, 1]

// Check TTL of the fields
const res15 = await client.hTTL('myhash', ['field1', 'field2'])
console.log(res15) // [10, 10] (or close to 10)

// Try to set expiration on non-existent field
const res16 = await client.hExpire('myhash', ['nonexistent'], 10)
console.log(res16) // [-2]

// REMOVE_START
assert.deepEqual(res14, [1, 1]);
assert(res15.every(ttl => ttl > 0)); // TTL should be positive
assert.deepEqual(res16, [-2]);
await client.del('myhash')
// REMOVE_END
// STEP_END

// HIDE_START
await client.close();
// HIDE_END
Loading