-
Notifications
You must be signed in to change notification settings - Fork 263
DEV: add TCEs to HEXPIRE command page #2253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
||
|
@@ -110,5 +111,40 @@ public void Run() | |
// >>> Hello, World | ||
// STEP_END | ||
Assert.Equal("Hello, World", string.Join(", ", hValsResult)); | ||
db.KeyDelete("myhash"); | ||
|
||
// STEP_START hexpire | ||
// Set up hash with fields | ||
db.HashSet("myhash", | ||
[ | ||
new("field1", "Hello"), | ||
new("field2", "World") | ||
] | ||
); | ||
|
||
// Set expiration on hash fields using raw Execute | ||
RedisResult hexpireRes1 = db.Execute("HEXPIRE", "myhash", 10, "FIELDS", 2, "field1", "field2"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually implemented in the library now ( |
||
Console.WriteLine(string.Join(", ", (RedisValue[])hexpireRes1)); | ||
// >>> 1, 1 | ||
|
||
// Check TTL of the fields using raw Execute | ||
RedisResult hexpireRes2 = db.Execute("HTTL", "myhash", "FIELDS", 2, "field1", "field2"); | ||
RedisValue[] ttlValues = (RedisValue[])hexpireRes2; | ||
Console.WriteLine(ttlValues.Length); | ||
// >>> 2 | ||
|
||
// Try to set expiration on non-existent field | ||
RedisResult hexpireRes3 = db.Execute("HEXPIRE", "myhash", 10, "FIELDS", 1, "nonexistent"); | ||
Console.WriteLine(string.Join(", ", (RedisValue[])hexpireRes3)); | ||
// >>> -2 | ||
// STEP_END | ||
|
||
RedisValue[] expireResult1 = (RedisValue[])hexpireRes1; | ||
RedisValue[] expireResult3 = (RedisValue[])hexpireRes3; | ||
Assert.Equal("1, 1", string.Join(", ", expireResult1)); | ||
Assert.Equal(2, ttlValues.Length); | ||
Assert.True(ttlValues.All(ttl => (int)ttl > 0)); // TTL should be positive | ||
Assert.Equal("-2", string.Join(", ", expireResult3)); | ||
db.KeyDelete("myhash"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -184,6 +184,49 @@ 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"); | ||||||||||||||||||||||||||||||||||||
}).thenAccept(res4 -> { | ||||||||||||||||||||||||||||||||||||
System.out.println(res4); | ||||||||||||||||||||||||||||||||||||
// >>> [-2] | ||||||||||||||||||||||||||||||||||||
// REMOVE_START | ||||||||||||||||||||||||||||||||||||
assertThat(res4).isEqualTo(Arrays.asList(-2L)); | ||||||||||||||||||||||||||||||||||||
// REMOVE_END | ||||||||||||||||||||||||||||||||||||
}).toCompletableFuture(); | ||||||||||||||||||||||||||||||||||||
Comment on lines
+217
to
+223
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works fine, but the visible code reduces to
Suggested change
|
||||||||||||||||||||||||||||||||||||
// STEP_END | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
hExpireExample.join(); | ||||||||||||||||||||||||||||||||||||
// REMOVE_START | ||||||||||||||||||||||||||||||||||||
asyncCommands.del("myhash").toCompletableFuture().join(); | ||||||||||||||||||||||||||||||||||||
// REMOVE_END | ||||||||||||||||||||||||||||||||||||
} finally { | ||||||||||||||||||||||||||||||||||||
redisClient.shutdown(); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,34 @@ public function testCmdsHash(): void | |
$this->assertEquals('OK', $hValsResult1); | ||
$this->assertEquals(['Hello', 'World'], $hValsResult2); | ||
|
||
// STEP_START hexpire | ||
echo "\n--- HEXPIRE Command ---\n"; | ||
// Clean up first | ||
$this->redis->del('myhash'); | ||
Comment on lines
+126
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't usually have these extra explanatory lines in the example output (bit of Augie randomness, I guess :-) ). Also, we'd normally have the |
||
|
||
// Set up hash with fields | ||
$hExpireResult1 = $this->redis->hmset('myhash', ['field1' => 'Hello', 'field2' => 'World']); | ||
echo "HMSET myhash field1 Hello field2 World: " . ($hExpireResult1 ? 'OK' : 'FAIL') . "\n"; // >>> OK | ||
|
||
// Set expiration on hash fields | ||
$hExpireResult2 = $this->redis->hexpire('myhash', 10, ['field1', 'field2']); | ||
echo "HEXPIRE myhash 10 FIELDS field1 field2: " . json_encode($hExpireResult2) . "\n"; // >>> [1,1] | ||
|
||
// Check TTL of the fields | ||
$hExpireResult3 = $this->redis->httl('myhash', ['field1', 'field2']); | ||
echo "HTTL myhash FIELDS field1 field2 count: " . count($hExpireResult3) . "\n"; // >>> 2 | ||
|
||
// Try to set expiration on non-existent field | ||
$hExpireResult4 = $this->redis->hexpire('myhash', 10, ['nonexistent']); | ||
echo "HEXPIRE myhash 10 FIELDS nonexistent: " . json_encode($hExpireResult4) . "\n"; // >>> [-2] | ||
// STEP_END | ||
|
||
$this->assertEquals('OK', $hExpireResult1); | ||
$this->assertEquals([1, 1], $hExpireResult2); | ||
$this->assertEquals(2, count($hExpireResult3)); | ||
$this->assertTrue(array_reduce($hExpireResult3, function($carry, $ttl) { return $carry && $ttl > 0; }, true)); // TTL should be positive | ||
$this->assertEquals([-2], $hExpireResult4); | ||
|
||
echo "\n=== All Hash Commands Tests Passed! ===\n"; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key deletion is in a
REMOVE
section in the other examples, and the assertion above probably should be visible to users either.