/
redis_demo.go
77 lines (58 loc) · 1.31 KB
/
redis_demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// web server
package main
import (
"fmt"
"net"
"os"
"bytes"
"github.com/xuyu/goredis"
)
const (
CONN_HOST = ""
CONN_PORT = "3333"
CONN_TYPE = "tcp"
)
func main() {
// listning
listen, error := net.Listen(CONN_TYPE, ":"+CONN_PORT)
if error != nil {
fmt.Println("Error in Listening")
os.Exit(1)
}
// close listner when server down
defer listen.Close()
fmt.Println("Listing on" + CONN_PORT)
for {
conn, error := listen.Accept()
if error != nil {
fmt.Println("Error Acception failed")
os.Exit(1)
}
//fmt.Println("Message Received")
// create new routine to handle connection
go handleRequest(conn)
}
}
func handleRequest(conn net.Conn) {
buf := make([]byte, 1024)
reqLen, err := conn.Read(buf)
n := bytes.Index(buf, []byte{0})
if err != nil {
fmt.Println("Error reading:", err)
}
fmt.Println(reqLen)
message := "Hi , I received your message "
message += string(buf[:n-1])
storeinRadis(message, conn.RemoteAddr().String())
conn.Write([]byte(message))
conn.Close()
}
func storeinRadis(message string, addr string){
client, err := goredis.Dial(&goredis.DialConfig{Address: "127.0.0.1:6379"})
reply, err2 := client.ExecuteCommand("SET", addr, message)
err1 := reply.OKValue()
fmt.Println(err1)
fmt.Println(err)
fmt.Println(err2)
fmt.Println(reply)
}