|  | 
| 9 | 9 | import java.util.Map; | 
| 10 | 10 | import java.util.List; | 
| 11 | 11 | import java.util.Collections; | 
|  | 12 | +import java.util.Arrays; | 
| 12 | 13 | 
 | 
| 13 | 14 | // HIDE_START | 
| 14 | 15 | import redis.clients.jedis.UnifiedJedis; | 
|  | 
| 17 | 18 | import static java.util.stream.Collectors.toList; | 
| 18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; | 
| 19 | 20 | import static org.junit.jupiter.api.Assertions.assertNull; | 
|  | 21 | +import static org.junit.jupiter.api.Assertions.assertTrue; | 
| 20 | 22 | 
 | 
| 21 | 23 | // HIDE_START | 
| 22 | 24 | public class CmdsHashExample { | 
| @@ -159,13 +161,41 @@ public void run() { | 
| 159 | 161 |         System.out.println(hValsResult2); | 
| 160 | 162 |         // >>> [Hello, World] | 
| 161 | 163 |         // STEP_END | 
| 162 |  | -        // REMOVE_START        | 
|  | 164 | +        // REMOVE_START | 
| 163 | 165 |         // Tests for 'hvals' step. | 
| 164 | 166 |         assertEquals(2, hValsResult1); | 
| 165 | 167 |         assertEquals("[Hello, World]", hValsResult2.toString()); | 
| 166 | 168 |         jedis.del("myhash"); | 
| 167 | 169 |         // REMOVE_END | 
| 168 | 170 | 
 | 
|  | 171 | +        // STEP_START hexpire | 
|  | 172 | +        // Set up hash with fields | 
|  | 173 | +        Map<String, String> hExpireExampleParams = new HashMap<>(); | 
|  | 174 | +        hExpireExampleParams.put("field1", "Hello"); | 
|  | 175 | +        hExpireExampleParams.put("field2", "World"); | 
|  | 176 | +        jedis.hset("myhash", hExpireExampleParams); | 
|  | 177 | + | 
|  | 178 | +        // Set expiration on hash fields | 
|  | 179 | +        List<Long> hExpireResult1 = jedis.hexpire("myhash", 10, "field1", "field2"); | 
|  | 180 | +        System.out.println(hExpireResult1); // >>> [1, 1] | 
|  | 181 | + | 
|  | 182 | +        // Check TTL of the fields | 
|  | 183 | +        List<Long> hExpireResult2 = jedis.httl("myhash", "field1", "field2"); | 
|  | 184 | +        System.out.println(hExpireResult2.size()); // >>> 2 | 
|  | 185 | + | 
|  | 186 | +        // Try to set expiration on non-existent field | 
|  | 187 | +        List<Long> hExpireResult3 = jedis.hexpire("myhash", 10, "nonexistent"); | 
|  | 188 | +        System.out.println(hExpireResult3); // >>> [-2] | 
|  | 189 | +        // STEP_END | 
|  | 190 | +        // REMOVE_START | 
|  | 191 | +        // Tests for 'hexpire' step. | 
|  | 192 | +        assertEquals(Arrays.asList(1L, 1L), hExpireResult1); | 
|  | 193 | +        assertEquals(2, hExpireResult2.size()); | 
|  | 194 | +        assertTrue(hExpireResult2.stream().allMatch(ttl -> ttl > 0)); // TTL should be positive | 
|  | 195 | +        assertEquals(Arrays.asList(-2L), hExpireResult3); | 
|  | 196 | +        jedis.del("myhash"); | 
|  | 197 | +        // REMOVE_END | 
|  | 198 | + | 
| 169 | 199 | // HIDE_START | 
| 170 | 200 |         jedis.close(); | 
| 171 | 201 |     } | 
|  | 
0 commit comments