package main import ( "fmt" "sync" "time" ) type Item struct { Value any ExpiresAt *time.Time } type RedisDB struct { mu sync.Mutex data map[string]Item } 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") } }