diff --git a/utils/redis.go b/utils/redis.go new file mode 100644 index 000000000..f177e871d --- /dev/null +++ b/utils/redis.go @@ -0,0 +1,13 @@ +package utils + +import "fmt" + +func GenRedisProtocol(cmd ...string) string { + proto := "" + proto += fmt.Sprintf("*%d\r\n", len(cmd)) + for _, arg := range cmd { + proto += fmt.Sprintf("$%d\r\n", len(arg)) + proto += fmt.Sprintf("%s\r\n", arg) + } + return proto +} diff --git a/utils/redis_test.go b/utils/redis_test.go new file mode 100644 index 000000000..99768a7ac --- /dev/null +++ b/utils/redis_test.go @@ -0,0 +1,10 @@ +package utils + +import "testing" + +func TestRedisGenProto(t *testing.T) { + cmd := GenRedisProtocol("SET", "mykey", "Hello World!") + if cmd != "*3\r\n$3\r\nSET\r\n$5\r\nmykey\r\n$12\r\nHello World!\r\n" { + t.Error("Wrong redis protocol command: " + cmd) + } +}