aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-04 21:27:44 +0200
committeralex <[email protected]>2026-07-04 21:27:44 +0200
commit9aca5ee57d43492542cd11db9ccc882a082e6ddd (patch)
treef623332adc0e42a8287c1ce136d8d6e3de78bae0 /main.go
parente6b954602e0ef5f17ce4dd8e83766d58d590984c (diff)
downloadredis-clone-9aca5ee57d43492542cd11db9ccc882a082e6ddd.tar.xz
redis-clone-9aca5ee57d43492542cd11db9ccc882a082e6ddd.zip
added exists keyword
Diffstat (limited to 'main.go')
-rw-r--r--main.go13
1 files changed, 13 insertions, 0 deletions
diff --git a/main.go b/main.go
index cedb658..0392ce5 100644
--- a/main.go
+++ b/main.go
@@ -50,6 +50,17 @@ func handleGet(conn net.Conn, key string) {
}
}
+func handleExists(conn net.Conn, key string){
+ db.mu.Lock()
+ _, exists := db.data[key]
+ db.mu.Unlock()
+ if exists {
+ conn.Write([]byte(":1\r\n"))
+ } else {
+ conn.Write([]byte(":0\r\n"))
+ }
+}
+
func handleSet(conn net.Conn, key, value string) {
db.mu.Lock()
db.data[key] = value
@@ -115,6 +126,8 @@ func handleConnection(conn net.Conn) {
handleGet(conn, args[1])
case command == "SET" && len(args) == 3:
handleSet(conn, args[1], args[2])
+ case command == "EXISTS" && len(args) == 2:
+ handleExists(conn, args[1])
default:
conn.Write([]byte("-ERR unknown command or wrong arguments\r\n"))
}