aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-04 21:57:40 +0200
committeralex <[email protected]>2026-07-04 21:57:40 +0200
commit7d7a61b655ebf71c2e1bf7e57a1dd345363e2775 (patch)
tree3a52f2738b467faa3f778b23143a86c1cba06bc1 /main.go
parent83cde33c09a1ba556dd82af9f8d944707dc9cde9 (diff)
downloadredis-clone-7d7a61b655ebf71c2e1bf7e57a1dd345363e2775.tar.xz
redis-clone-7d7a61b655ebf71c2e1bf7e57a1dd345363e2775.zip
added decr and ping
Diffstat (limited to 'main.go')
-rw-r--r--main.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/main.go b/main.go
index 7d440ad..3130b26 100644
--- a/main.go
+++ b/main.go
@@ -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"))
}