diff options
| author | alex <[email protected]> | 2026-07-06 13:35:07 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-07-06 13:35:07 +0200 |
| commit | f00398f4be24b5c3cce52c36dc71a42d1d0ccfbe (patch) | |
| tree | 1459c3502db09b664d3ceac114b2c86affd09d11 /redisDB.go | |
| parent | 8c49e03997128de4761acaaa6606d58f87d92c6c (diff) | |
| download | redis-clone-f00398f4be24b5c3cce52c36dc71a42d1d0ccfbe.tar.xz redis-clone-f00398f4be24b5c3cce52c36dc71a42d1d0ccfbe.zip | |
split the code into multiple files for better readability
Diffstat (limited to 'redisDB.go')
| -rw-r--r-- | redisDB.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/redisDB.go b/redisDB.go new file mode 100644 index 0000000..f21ac50 --- /dev/null +++ b/redisDB.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "sync" +) + +type RedisDB struct { + mu sync.Mutex + data map[string]any +} + +type SimpleString string + +func serializeRESP(v any) []byte { + switch val := v.(type) { + case string: + bulkString := fmt.Sprintf("$%d\r\n%s\r\n", len(val), val) + return []byte(bulkString) + case int: + integerString := fmt.Sprintf(":%d\r\n",val) + return []byte(integerString) + case nil: + return []byte("$-1\r\n") + case error: + errorString := fmt.Sprintf("-ERR %s\r\n",val.Error()) + return []byte(errorString) + case []string: + size := len(val) + result := []byte(fmt.Sprintf("*%d\r\n", size)) + for i := 0; i < size; i++ { + result = append(result, serializeRESP(val[i])...) + } + return result + case SimpleString: + simpleStr := fmt.Sprintf("+%s\r\n", val) + return []byte(simpleStr) + default: + return []byte("-ERR internal server error: unknown type\r\n") + } +} |
