aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-04 21:51:07 +0200
committeralex <[email protected]>2026-07-04 21:51:07 +0200
commit83cde33c09a1ba556dd82af9f8d944707dc9cde9 (patch)
treee109ab3d3b71446cf72793fac07af77d85d1cd31
parent332fdd08e62613d04c7d1f00d56817c59fda297f (diff)
downloadredis-clone-83cde33c09a1ba556dd82af9f8d944707dc9cde9.tar.xz
redis-clone-83cde33c09a1ba556dd82af9f8d944707dc9cde9.zip
added incr keyword
-rw-r--r--main.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/main.go b/main.go
index a2448ba..7d440ad 100644
--- a/main.go
+++ b/main.go
@@ -81,6 +81,27 @@ func handleDel(conn net.Conn, key string){
db.mu.Unlock()
}
+func handleIncr(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 parseRESP(reader *bufio.Reader) ([]string, error) {
line, err := reader.ReadString('\n')
if err != nil {
@@ -144,6 +165,8 @@ func handleConnection(conn net.Conn) {
handleExists(conn, args[1])
case command == "DEL" && len(args) == 2:
handleDel(conn, args[1])
+ case command == "INCR" && len(args) == 2:
+ handleIncr(conn, args[1])
default:
conn.Write([]byte("-ERR unknown command or wrong arguments\r\n"))
}