STRLEN
Return the length of the string value stored in key.
When key does not store a string value, an error is returned.
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”STRLEN keyParameter Description
- key: The key to get the length
Detailed Explanation
Section titled “Detailed Explanation”The STRLEN command is used to get the length of the string value stored in a specified key. For non-existent keys, the command returns 0.
Redisun Usage
Section titled “Redisun Usage”In redisun, the STRLEN command is implemented through the StrlenCommand class and the strlen method in the Redisun class.
Basic Usage
Section titled “Basic Usage”Redisun redisun = Redisun.create(options -> { options.setAddress("redis://127.0.0.1:6379");});
// Set a string valueredisun.set("mykey", "Hello World");
// Get string lengthint length = redisun.strlen("mykey");System.out.println("String length: " + length); // Output: 11
// Get length of non-existent keylength = redisun.strlen("nonexistent");System.out.println("Non-existent key length: " + length); // Output: 0
// Asynchronous versionCompletableFuture<Integer> future = redisun.asyncStrlen("mykey");future.thenAccept(len -> System.out.println("Async length: " + len));- For non-existent keys, return length 0
- If key exists but is not of string type, an exception will be thrown
- The length of an empty string is 0