Skip to content
Open
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
10 changes: 9 additions & 1 deletion memg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import (
"os"
"strconv"
"strings"
"sync"
//"runtime/pprof" // Uncomment to profile
)

var CACHE map[string]string
var (
mu sync.RWMutex
CACHE map[string]string
)

func main() {

Expand Down Expand Up @@ -87,7 +91,9 @@ func handleConn(conn net.Conn) {

case "get":
key := parts[1]
mu.RLock()
val, ok := CACHE[key]
mu.RUnlock()
if ok {
length := strconv.Itoa(len(val))
conn.Write([]uint8("VALUE " + key + " 0 " + length + "\r\n"))
Expand All @@ -103,7 +109,9 @@ func handleConn(conn net.Conn) {
// Really we should read exactly 'length' bytes + \r\n
val := make([]byte, length)
reader.Read(val)
mu.Lock()
CACHE[key] = string(val)
mu.Unlock()
conn.Write([]uint8("STORED\r\n"))
}
}
Expand Down