aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-04 21:34:52 +0200
committeralex <[email protected]>2026-07-04 21:34:52 +0200
commit332fdd08e62613d04c7d1f00d56817c59fda297f (patch)
tree5a3fc2a3878e1b0a2e6ac283ddcac8cf7f035d2c
parent9aca5ee57d43492542cd11db9ccc882a082e6ddd (diff)
downloadredis-clone-332fdd08e62613d04c7d1f00d56817c59fda297f.tar.xz
redis-clone-332fdd08e62613d04c7d1f00d56817c59fda297f.zip
added del keyword
-rw-r--r--main.go16
1 files changed, 16 insertions, 0 deletions
diff --git a/main.go b/main.go
index 0392ce5..a2448ba 100644
--- a/main.go
+++ b/main.go
@@ -67,6 +67,20 @@ func handleSet(conn net.Conn, key, value string) {
db.mu.Unlock()
conn.Write([]byte("+OK\r\n"))
}
+
+func handleDel(conn net.Conn, key string){
+ db.mu.Lock()
+ _, exists := db.data[key]
+
+ if exists {
+ delete(db.data, key)
+ conn.Write([]byte(":1\r\n"))
+ } else {
+ conn.Write([]byte(":0\r\n"))
+ }
+ db.mu.Unlock()
+}
+
func parseRESP(reader *bufio.Reader) ([]string, error) {
line, err := reader.ReadString('\n')
if err != nil {
@@ -128,6 +142,8 @@ func handleConnection(conn net.Conn) {
handleSet(conn, args[1], args[2])
case command == "EXISTS" && len(args) == 2:
handleExists(conn, args[1])
+ case command == "DEL" && len(args) == 2:
+ handleDel(conn, args[1])
default:
conn.Write([]byte("-ERR unknown command or wrong arguments\r\n"))
}