Created
May 1, 2014 02:34
-
-
Save meisinger/849523c10a56a14511e4 to your computer and use it in GitHub Desktop.
an approach to creating redis commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RedisCommand | |
{ | |
static byte[] GenerateCommand(byte[][] arguments) | |
{ | |
using (var stream = new MemoryStream()) | |
{ | |
var request = CreateIndicator('*', arguments.Length); | |
stream.Write(request, 0, request.Length); | |
stream.WriteNewLine(); | |
foreach (var argument in arguments) | |
{ | |
var argumentRequest = CreateIndicator('$', argument.Length); | |
stream.Write(argumentRequest, 0, argumentRequest.Length); | |
stream.WriteNewLine(); | |
stream.Write(argument, 0, argument.Length); | |
stream.WriteNewLine(); | |
} | |
return stream.ToArray(); | |
} | |
} | |
static byte[] CreateIndicator(char indicator, int count) | |
{ | |
var value = count.ToString(); | |
var length = value.Length; | |
var bytes = new byte[length + 1]; | |
bytes[0] = (byte)indicator; | |
for (var index = 0; index < length; index++) | |
{ | |
bytes[index + 1] = (byte)value[index]; | |
} | |
return bytes; | |
} | |
} | |
public class MemoryStreamExtensions | |
{ | |
private static readonly byte[] newline = new byte[] { 13, 10 }; | |
public static void WriteNewLine(this MemoryStream stream) | |
{ | |
stream.Write(newline, 0, 2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment