-
Notifications
You must be signed in to change notification settings - Fork 3
/
driver.go
218 lines (188 loc) · 4.37 KB
/
driver.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package session
import (
"context"
"encoding/json"
"strings"
"time"
"github.com/abulo/ratel/redis"
)
type Session struct {
Driver *redis.Client
Name string
TTL int64 // seconds
}
// https://laravel.com/docs/5.8/session
// Pushing To Array Session Values
// session.Put('user.teams', 'developers'); => {user: {teams: "developer"}}
func (this *Session) Put(ctx context.Context, key string, value interface{}) error {
var h = this.Driver
var bytes []byte
var err error
var content string
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
m := make(map[string]interface{})
// fmt.Printf("Exists %s?\n", this.Name)
if h.Exists(ctx, this.Name).Val() == 1 {
content = h.Get(ctx, this.Name).Val()
} else {
content = "{}"
}
// fmt.Printf("%s => %s", this.Name, content)
json.Unmarshal([]byte(content), &m)
if err != nil {
return err
}
var keys = strings.Split(key, ".")
var depth = len(keys)
if depth < 2 {
m[key] = value
} else {
m = setSliceMap(m, keys, value)
}
bytes, _ = json.Marshal(m)
h.Set(ctx, this.Name, string(bytes), time.Duration(this.TTL)*time.Second)
return nil
}
// s.Get
func (this *Session) Get(ctx context.Context, key string) interface{} {
var h = this.Driver
var m map[string]interface{}
var content string
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
content = h.Get(ctx, this.Name).Val()
json.Unmarshal([]byte(content), &m)
var keys = strings.Split(key, ".")
var n = len(keys)
if n < 2 {
return m[key]
}
return getSliceMap(m, keys)
}
func (this *Session) Remove(ctx context.Context, key string) {
var h = this.Driver
var m map[string]interface{}
var content string
var bytes []byte
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
content = h.Get(ctx, this.Name).Val()
json.Unmarshal([]byte(content), &m)
var keys = strings.Split(key, ".")
var n = len(keys)
if n < 2 {
delete(m, key)
bytes, _ = json.Marshal(m)
h.Set(ctx, this.Name, string(bytes), time.Duration(this.TTL)*time.Second)
return
}
delSliceMap(m, keys)
bytes, _ = json.Marshal(m)
h.Set(ctx, this.Name, string(bytes), time.Duration(this.TTL)*time.Second)
}
// sess.Put("info.age", "28")
func setSliceMap(m map[string]interface{}, keys []string, value interface{}) map[string]interface{} {
var itMap = m
var i int
var limit = len(keys) - 1
for i = 0; i < limit; i++ {
_, ok := itMap[keys[i]]
if !ok {
// } else {
itMap[keys[i]] = make(map[string]interface{})
}
itMap = itMap[keys[i]].(map[string]interface{})
}
itMap[keys[limit]] = value
return m
}
func getSliceMap(m map[string]interface{}, keys []string) interface{} {
var itMap = m
var i int
var limit = len(keys) - 1
var v interface{}
var ok bool
for i = 0; i < limit; i++ {
v, ok = itMap[keys[i]]
if !ok {
break
}
itMap = v.(map[string]interface{})
}
v, ok = itMap[keys[i]]
if !ok {
return nil
}
return v
}
func delSliceMap(m map[string]interface{}, keys []string) {
var itMap = m
var i int
var limit = len(keys) - 1
var v interface{}
var ok bool
for i = 0; i < limit; i++ {
v, ok = itMap[keys[i]]
if !ok {
break
}
itMap = v.(map[string]interface{})
}
v, ok = itMap[keys[i]]
if !ok {
return
}
delete(itMap, keys[i])
}
/*
// TO BE DONE
func (this *Session) Has(key string) bool {
var h = this.getDriver()
return h.Exists(this.Name).Val() == 1
}
// TO BE DONE
func (this *Session) Push(key string, e interface{}) {
}
*/
//Destroy 释放
func (this *Session) Destroy(ctx context.Context) int64 {
var h = this.Driver
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
return h.Del(ctx, this.Name).Val()
}
// func TestSession_Put(t *testing.T) {
// var sess = Session{
// Name: "sess_1",
// Driver: client,
// TTL: 0,
// }
// var userID int64
// userID = 2147483647
// sess.Put("id", strconv.FormatInt(userID, 10))
// // sess.Put("id", userID)
// sess.Put("name", "mingzhanghui")
// sess.Put("info.age", "29")
// sess.Put("info.gender", "male")
// sess.Put("a.b.c.d", "1")
// }
// func TestSession_Get(t *testing.T) {
// var sess = Session{
// Name: "sess_1",
// Driver: client,
// }
// userId := sess.Get("id")
// // t.Logf("%d\n", userId.(int64))
// assert.Equal(t, userId, "2147483647")
// gender := sess.Get("info.gender")
// t.Logf(gender.(string))
// assert.Equal(t, gender, "male")
// foo := sess.Get("a.b.c.d")
// t.Logf(foo.(string))
// assert.Equal(t, foo, "1")
// }