Created
March 6, 2020 13:14
-
-
Save alperen/84f921994f0b61f914b281f6638c7aec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"encoding/json" | |
"fmt" | |
"log" | |
"net" | |
"strconv" | |
) | |
const cport = "6379" | |
type request struct { | |
Command string `json:"command"` | |
Args map[string]string `json:"args,omitempty"` | |
} | |
func sendCommand(command string, args map[string]string) string { | |
data := request{command, args} | |
marshaled, _ := json.Marshal(&data) | |
return string(marshaled) + "\n" | |
} | |
func createConn() *net.Conn { | |
server, err := net.Dial("tcp", fmt.Sprintf(":%s", cport)) | |
if err != nil { | |
log.Println(err) | |
panic("Unable to connect server") | |
} | |
return &server | |
} | |
func getCMD(key string) map[string]interface{} { | |
var res map[string]interface{} | |
conn := *createConn() | |
defer conn.Close() | |
cmd := sendCommand("GET", map[string]string{ | |
"key": key, | |
}) | |
fmt.Fprintf(conn, cmd) | |
message, _ := bufio.NewReader(conn).ReadBytes('\n') | |
json.Unmarshal(message, &res) | |
return res | |
} | |
func setCMD(key, value string, ttl int) map[string]interface{} { | |
var res map[string]interface{} | |
conn := *createConn() | |
defer conn.Close() | |
payload := map[string]string{ | |
"key": key, | |
"value": value, | |
} | |
if ttl != 0 { | |
payload["ttl"] = strconv.Itoa(ttl) | |
} | |
cmd := sendCommand("SET", payload) | |
fmt.Fprintf(conn, cmd) | |
message, _ := bufio.NewReader(conn).ReadBytes('\n') | |
json.Unmarshal(message, &res) | |
return res | |
} | |
func deleteCMD(key string) map[string]interface{} { | |
var res map[string]interface{} | |
conn := *createConn() | |
defer conn.Close() | |
cmd := sendCommand("DELETE", map[string]string{ | |
"key": key, | |
}) | |
fmt.Fprintf(conn, cmd) | |
message, _ := bufio.NewReader(conn).ReadBytes('\n') | |
json.Unmarshal(message, &res) | |
return res | |
} | |
func expireCMD(key string, expire string) map[string]interface{} { | |
var res map[string]interface{} | |
conn := *createConn() | |
defer conn.Close() | |
cmd := sendCommand("EXPIRE", map[string]string{ | |
"key": key, | |
"ttl": expire, | |
}) | |
fmt.Fprintf(conn, cmd) | |
message, _ := bufio.NewReader(conn).ReadBytes('\n') | |
json.Unmarshal(message, &res) | |
return res | |
} | |
func ttlCMD(key string) map[string]interface{} { | |
var res map[string]interface{} | |
conn := *createConn() | |
defer conn.Close() | |
cmd := sendCommand("TTL", map[string]string{ | |
"key": key, | |
}) | |
fmt.Fprintf(conn, cmd) | |
message, _ := bufio.NewReader(conn).ReadBytes('\n') | |
json.Unmarshal(message, &res) | |
return res | |
} | |
func incrCMD(key string) map[string]interface{} { | |
var res map[string]interface{} | |
conn := *createConn() | |
defer conn.Close() | |
cmd := sendCommand("INCR", map[string]string{ | |
"key": key, | |
}) | |
fmt.Fprintf(conn, cmd) | |
message, _ := bufio.NewReader(conn).ReadBytes('\n') | |
json.Unmarshal(message, &res) | |
return res | |
} | |
func main() { | |
set := setCMD("merhaba", "Alperen", 0) | |
log.Println(set) // map[result:map[value:Alperen] status:OK] | |
expire := expireCMD("merhaba", "15") | |
log.Println(expire) //map[status:OK] | |
//time.Sleep(3 * time.Second) | |
ttl := ttlCMD("merhaba") | |
log.Println(ttl) // map[result:map[ttl:14.9990062] status:OK] | |
setCMD("yas", "1", 0) | |
incr := incrCMD("yas") | |
log.Println(incr) | |
setCMD("yas2", "1.4", 0) | |
incr2 := incrCMD("yas2") | |
log.Println(incr2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment