-
Notifications
You must be signed in to change notification settings - Fork 0
/
redisism.go
113 lines (92 loc) · 1.78 KB
/
redisism.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package Redisism
import (
"fmt"
"sync"
)
type Cache struct{}
type Request struct {
Request string
Key string
Val string
RTL int
}
type Response struct {
Val string
Msg string
}
type ServerConnection struct {
snd chan Request
rcv chan Response
storage map[string]string
owner string
Mutex sync.Mutex
}
type ClientConnection struct {
snd chan Request
rcv chan Response
}
func InitCache() *Cache {
return &Cache{}
}
func (cache *Cache) Connect(name string) *ClientConnection {
request := make(chan Request)
response := make(chan Response)
storage := make(map[string]string)
serverConn := &ServerConnection{
snd: request,
rcv: response,
owner: name,
storage: storage,
}
clientConn := &ClientConnection{
snd: request,
rcv: response,
}
fmt.Println("[Redisism] New Connection established \n", "Name : "+name)
go func(serverConn *ServerConnection) {
for {
Data := <-serverConn.snd
if Data.Request == "Set" {
serverConn.Mutex.Lock()
serverConn.storage[Data.Key] = Data.Val
serverConn.rcv <- Response{
Data.Val,
"Done",
}
serverConn.Mutex.Unlock()
} else if Data.Request == "Get" {
serverConn.Mutex.Lock()
D, HaveData := serverConn.storage[Data.Key]
if !HaveData {
serverConn.rcv <- Response{
"",
"No Data",
}
} else {
serverConn.rcv <- Response{
D,
"Done",
}
}
serverConn.Mutex.Unlock()
}
}
}(serverConn)
return clientConn
}
func (conn *ClientConnection) Set(key string, val string) {
conn.snd <- Request{
Request: "Set",
Key: key,
Val: val,
}
<-conn.rcv
}
func (conn *ClientConnection) Get(key string) string {
conn.snd <- Request{
Request: "Get",
Key: key,
}
res := <-conn.rcv
return res.Val
}