-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpcserverforredis.go
196 lines (176 loc) · 3.19 KB
/
rpcserverforredis.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
"log"
"net"
"net/http"
"net/rpc"
)
type RequestRedis struct{
/*
1)support key match:point and RE[1,2]
2)value:point [3]
3)the first version is just string forms
4)no condition read all k{v}[4]
*/
QueryType int
Offsets []int //0 for key and 1 for value
Value string //this is prama eg:a="b" b is para
Count int
}
type ResultRedis struct{
Result string //the same as pg and csv
}
type RedisXX struct{
}
func (c *RedisXX)Require(args *RequestRedis,reply *ResultRedis)error{
v:=""
log.Println(args.QueryType)
log.Println(args)
switch args.QueryType {
case 1:
v=queryRedisPointQueryForKey(args.Offsets,args.Value)
case 2:
v=queryRedisREForKey(args.Offsets,args.Value)
case 4:
v=queryRedisNoCondtion(args.Offsets)
}
reply.Result=v
log.Println(v)
return nil
}
func queryRedisPointQueryForKey(offset []int,para string)string{
//连接redis
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println(err)
return ""
}
defer c.Close()
//通过Do函数,发送redis命令
//redis.
v, err := redis.String(c.Do("GET", para))
if err != nil {
fmt.Println(err) //this mean no result
return ""
}
if len(offset)==1 {
//
if offset[0]==0 {
return para
}
if offset[0]==1{
return v
}
}else{
if offset[0]==0 {
return para+"#"+v
}else{
return v+"#"+para
}
}
return v
}
func queryRedisREForKey(offset []int,para string)string{
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println(err)
return ""
}
defer c.Close()
keys, err := redis.Strings(c.Do("KEYS", para))
v := ""
if err != nil {
return v
}
for i, key := range keys {
//fmt.Println(key)
if i>0 {
v+=","
}
value, _ := redis.String(c.Do("GET", key)) //must exsit
//compose one reuslt
oneResult :=""
if len(offset)==1 {
if offset[0]==0 {
oneResult+=key
}
if offset[0]==1{
oneResult+=value
}
}else{
if offset[0]==0 {
oneResult= key+"#"+value
}else{
oneResult = value+"#"+key
}
}
v+=oneResult
}
return v
}
func queryRedisNoCondtion(offset []int)string{
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println(err)
return ""
}
defer c.Close()
keys, err := redis.Strings(c.Do("KEYS", "*"))
v := ""
if err != nil {
return v
}
for i, key := range keys {
//fmt.Println(key)
if i>0 {
v+=","
}
value, _ := redis.String(c.Do("GET", key)) //must exsit
//compose one reuslt
oneResult :=""
if len(offset)==1 {
if offset[0]==0 {
oneResult+=key
}
if offset[0]==1{
oneResult+=value
}
}else{
if offset[0]==0 {
oneResult= key+"#"+value
}else{
oneResult = value+"#"+key
}
}
v+=oneResult
}
return v
}
func testget(){
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println(err)
return
}
defer c.Close()
//通过Do函数,发送redis命令
//redis.
v, err := redis.String(c.Do("GET", "lanhaihailanlanhai"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(v)
}
func main(){
rxx := new(RedisXX)
rpc.Register(rxx)
rpc.HandleHTTP()
l,e:=net.Listen("tcp","0.0.0.0:5435")
if e!=nil {
log.Println(e)
}
http.Serve(l,nil)
}