diff options
| author | alex <[email protected]> | 2026-07-04 21:57:40 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-07-04 21:57:40 +0200 |
| commit | 7d7a61b655ebf71c2e1bf7e57a1dd345363e2775 (patch) | |
| tree | 3a52f2738b467faa3f778b23143a86c1cba06bc1 | |
| parent | 83cde33c09a1ba556dd82af9f8d944707dc9cde9 (diff) | |
| download | redis-clone-7d7a61b655ebf71c2e1bf7e57a1dd345363e2775.tar.xz redis-clone-7d7a61b655ebf71c2e1bf7e57a1dd345363e2775.zip | |
added decr and ping
| -rw-r--r-- | main.go | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -102,6 +102,31 @@ func handleIncr(conn net.Conn, key string){ } +func handleDecr(conn net.Conn, key string){ + db.mu.Lock() + value, exists := db.data[key] + if exists { + currentInt, err := strconv.Atoi(value) + if err != nil { + conn.Write([]byte("-ERR value is not an integer or out of range\r\n")) + } + + newValueStr := strconv.Itoa(currentInt-1) + db.data[key] = newValueStr + conn.Write([]byte(":" + newValueStr + "\r\n")) + }else{ + db.data[key] = "-1" + conn.Write([]byte(":-1\r\n")) + } + + db.mu.Unlock() + +} + +func handlePing(conn net.Conn) { + conn.Write([]byte("+PONG\r\n")) +} + func parseRESP(reader *bufio.Reader) ([]string, error) { line, err := reader.ReadString('\n') if err != nil { @@ -167,6 +192,10 @@ func handleConnection(conn net.Conn) { handleDel(conn, args[1]) case command == "INCR" && len(args) == 2: handleIncr(conn, args[1]) + case command == "DECR" && len(args) == 2: + handleDecr(conn, args[1]) + case command == "PING" && len(args) == 1: + handlePing(conn) default: conn.Write([]byte("-ERR unknown command or wrong arguments\r\n")) } |
