Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementation of the SETNX command #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Here's a full example of a Redis clone that accepts:

- SET key value
- GET key
- SETNX key value
- DEL key
- PING
- QUIT
Expand Down Expand Up @@ -96,6 +97,22 @@ func main() {
} else {
conn.WriteBulk(val)
}
case "setnx":
if len(cmd.Args) != 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
mu.RLock()
_, ok := items[string(cmd.Args[1])]
mu.RUnlock()
if ok {
conn.WriteInt(0)
return
}
mu.Lock()
items[string(cmd.Args[1])] = cmd.Args[2]
mu.Unlock()
conn.WriteInt(1)
case "del":
if len(cmd.Args) != 2 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
Expand Down
16 changes: 16 additions & 0 deletions example/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ func main() {
} else {
conn.WriteBulk(val)
}
case "setnx":
if len(cmd.Args) != 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
mu.RLock()
_, ok := items[string(cmd.Args[1])]
mu.RUnlock()
if ok {
conn.WriteInt(0)
return
}
mu.Lock()
items[string(cmd.Args[1])] = cmd.Args[2]
mu.Unlock()
conn.WriteInt(1)
case "del":
if len(cmd.Args) != 2 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
Expand Down
1 change: 1 addition & 0 deletions example/mux/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func main() {
mux.HandleFunc("quit", handler.quit)
mux.HandleFunc("set", handler.set)
mux.HandleFunc("get", handler.get)
mux.HandleFunc("setnx", handler.setnx)
mux.HandleFunc("del", handler.delete)

err := redcon.ListenAndServe(addr,
Expand Down
22 changes: 22 additions & 0 deletions example/mux/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ func (h *Handler) get(conn redcon.Conn, cmd redcon.Command) {
}
}

func (h *Handler) setnx(conn redcon.Conn, cmd redcon.Command) {
if len(cmd.Args) != 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}

h.itemsMux.RLock()
_, ok := h.items[string(cmd.Args[1])]
h.itemsMux.RUnlock()

if ok {
conn.WriteInt(0)
return
}

h.itemsMux.Lock()
h.items[string(cmd.Args[1])] = cmd.Args[2]
h.itemsMux.Unlock()

conn.WriteInt(1)
}

func (h *Handler) delete(conn redcon.Conn, cmd redcon.Command) {
if len(cmd.Args) != 2 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
Expand Down
16 changes: 16 additions & 0 deletions example/tls/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ func main() {
} else {
conn.WriteBulk(val)
}
case "setnx":
if len(cmd.Args) != 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
mu.RLock()
_, ok := items[string(cmd.Args[1])]
mu.RUnlock()
if ok {
conn.WriteInt(0)
return
}
mu.Lock()
items[string(cmd.Args[1])] = cmd.Args[2]
mu.Unlock()
conn.WriteInt(1)
case "del":
if len(cmd.Args) != 2 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
Expand Down