diff --git a/src/Firefly.cs b/src/Firefly.cs index a1d2280..be3bbba 100644 --- a/src/Firefly.cs +++ b/src/Firefly.cs @@ -188,7 +188,10 @@ namespace Firefly case "LINDEX": return HandleLIndexCommand(parts.Length > 1 ? string.Join(" ", parts.Skip(1)) : ""); - + + case "LLEN": + return HandleLLengthCommand(parts.Length > 1 ? parts[1] : ""); + case "LRANGE": return HandleLRangeCommand(parts.Length > 1 ? string.Join(" ", parts.Skip(1)) : ""); diff --git a/src/ListOperations.cs b/src/ListOperations.cs index 716af73..58fb450 100644 --- a/src/ListOperations.cs +++ b/src/ListOperations.cs @@ -152,6 +152,30 @@ namespace Firefly : Encoding.UTF8.GetBytes("$-1\r\n"); } + /// + /// Handles the LLEN command which returns the length of a list. + /// + /// Command arguments containing the key + /// The length of the list or 0 if the key does not exist + static byte[] HandleLLengthCommand(string args) + { + if (string.IsNullOrWhiteSpace(args)) + { + return Encoding.UTF8.GetBytes("-ERR wrong number of arguments for 'llen' command\r\n"); + } + + string key = args.Trim(); + int length = 0; + + // Use helper function to safely read the list with read lock + ListStoreWithReadLock(key, list => + { + length = list.Count; + }); + + return Encoding.UTF8.GetBytes($":{length}\r\n"); + } + /// /// Handles the LRANGE command which returns a range of elements from a list. ///