-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.go
325 lines (280 loc) · 8.37 KB
/
cache.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package sessions
import (
"bytes"
"context"
"encoding/gob"
"errors"
"net/http"
"sort"
"sync"
"time"
)
type contextKey string
var contextKeyCache = contextKey("cache")
var errMissingCache = errors.New("session: cache not present in request context")
type cache struct {
Data map[string]interface{}
Expiry time.Time
modified bool
destroyed bool
mu sync.Mutex
}
func newCache(lifetime time.Duration) *cache {
return &cache{
Data: make(map[string]interface{}),
Expiry: time.Now().Add(lifetime).UTC(),
}
}
func (c *cache) encode(key [32]byte) (string, error) {
var b bytes.Buffer
err := gob.NewEncoder(&b).Encode(c)
if err != nil {
return "", err
}
return encrypt(b.Bytes(), key)
}
func (c *cache) decode(token string, keys [][32]byte) error {
b, err := decrypt(token, keys)
if err != nil {
return err
}
r := bytes.NewReader(b)
return gob.NewDecoder(r).Decode(c)
}
func addCacheToRequestContext(r *http.Request, c *cache) *http.Request {
ctx := context.WithValue(r.Context(), contextKeyCache, c)
return r.WithContext(ctx)
}
func getCacheFromRequestContext(r *http.Request) *cache {
c, ok := r.Context().Value(contextKeyCache).(*cache)
if !ok {
panic(errMissingCache)
}
return c
}
// Put adds a key and corresponding value to the session data. Any existing
// value for the key will be replaced.
func (s *Session) Put(r *http.Request, key string, val interface{}) {
c := getCacheFromRequestContext(r)
c.mu.Lock()
c.Data[key] = val
c.modified = true
c.mu.Unlock()
}
// Get returns the value for a given key from the session data. The return
// value has the type interface{} so will usually need to be type asserted
// before you can use it. For example:
//
// foo, ok := session.Get(r, "foo").(string)
// if !ok {
// return errors.New("type assertion to string failed")
// }
//
// Note: Alternatives are the GetString(), GetInt(), GetBytes() and other
// helper methods which wrap the type conversion for common types.
func (s *Session) Get(r *http.Request, key string) interface{} {
c := getCacheFromRequestContext(r)
c.mu.Lock()
defer c.mu.Unlock()
return c.Data[key]
}
// Pop acts like a one-time Get. It returns the value for a given key from the
// session data and deletes the key and value from the session data. The
// return value has the type interface{} so will usually need to be type
// asserted before you can use it.
func (s *Session) Pop(r *http.Request, key string) interface{} {
c := getCacheFromRequestContext(r)
c.mu.Lock()
defer c.mu.Unlock()
val, exists := c.Data[key]
if !exists {
return nil
}
delete(c.Data, key)
c.modified = true
return val
}
// Remove deletes the given key and corresponding value from the session data.
// If the key is not present this operation is a no-op.
func (s *Session) Remove(r *http.Request, key string) {
c := getCacheFromRequestContext(r)
c.mu.Lock()
defer c.mu.Unlock()
_, exists := c.Data[key]
if !exists {
return
}
delete(c.Data, key)
c.modified = true
}
// Exists returns true if the given key is present in the session data.
func (s *Session) Exists(r *http.Request, key string) bool {
c := getCacheFromRequestContext(r)
c.mu.Lock()
_, exists := c.Data[key]
c.mu.Unlock()
return exists
}
// Keys returns a slice of all key names present in the session data, sorted
// alphabetically. If the cache contains no data then an empty slice will be
// returned.
func (s *Session) Keys(r *http.Request) []string {
c := getCacheFromRequestContext(r)
c.mu.Lock()
keys := make([]string, len(c.Data))
i := 0
for key := range c.Data {
keys[i] = key
i++
}
c.mu.Unlock()
sort.Strings(keys)
return keys
}
// Destroy deletes the current session. The session data is deleted from memory
// and the client is instructed to delete the session cookie.
//
// Any further operations on the session data *within the same request cycle*
// will result in a panic.
func (s *Session) Destroy(r *http.Request) {
c := getCacheFromRequestContext(r)
c.mu.Lock()
c.Data = nil
c.Expiry = time.Time{}
c.modified = true
c.destroyed = true
c.mu.Unlock()
}
// GetString returns the string value for a given key from the session data.
// The zero value for a string ("") is returned if the key does not exist or the
// value could not be type asserted to a string.
func (s *Session) GetString(r *http.Request, key string) string {
val := s.Get(r, key)
str, ok := val.(string)
if !ok {
return ""
}
return str
}
// GetBool returns the bool value for a given key from the session data. The
// zero value for a bool (false) is returned if the key does not exist or the
// value could not be type asserted to a bool.
func (s *Session) GetBool(r *http.Request, key string) bool {
val := s.Get(r, key)
b, ok := val.(bool)
if !ok {
return false
}
return b
}
// GetInt returns the int value for a given key from the session data. The
// zero value for an int (0) is returned if the key does not exist or the
// value could not be type asserted to an int.
func (s *Session) GetInt(r *http.Request, key string) int {
val := s.Get(r, key)
i, ok := val.(int)
if !ok {
return 0
}
return i
}
// GetFloat returns the float64 value for a given key from the session data. The
// zero value for an float64 (0) is returned if the key does not exist or the
// value could not be type asserted to a float64.
func (s *Session) GetFloat(r *http.Request, key string) float64 {
val := s.Get(r, key)
f, ok := val.(float64)
if !ok {
return 0
}
return f
}
// GetBytes returns the byte slice ([]byte) value for a given key from the session
// cache. The zero value for a slice (nil) is returned if the key does not exist
// or could not be type asserted to []byte.
func (s *Session) GetBytes(r *http.Request, key string) []byte {
val := s.Get(r, key)
b, ok := val.([]byte)
if !ok {
return nil
}
return b
}
// GetTime returns the time.Time value for a given key from the session data. The
// zero value for a time.Time object is returned if the key does not exist or the
// value could not be type asserted to a time.Time. This can be tested with the
// time.IsZero() method.
func (s *Session) GetTime(r *http.Request, key string) time.Time {
val := s.Get(r, key)
t, ok := val.(time.Time)
if !ok {
return time.Time{}
}
return t
}
// PopString returns the string value for a given key and then deletes it from the
// session data. The zero value for a string ("") is returned if the key does not
// exist or the value could not be type asserted to a string.
func (s *Session) PopString(r *http.Request, key string) string {
val := s.Pop(r, key)
str, ok := val.(string)
if !ok {
return ""
}
return str
}
// PopBool returns the bool value for a given key and then deletes it from the
// session data. The zero value for a bool (false) is returned if the key does not
// exist or the value could not be type asserted to a bool.
func (s *Session) PopBool(r *http.Request, key string) bool {
val := s.Pop(r, key)
b, ok := val.(bool)
if !ok {
return false
}
return b
}
// PopInt returns the int value for a given key and then deletes it from the
// session data. The zero value for an int (0) is returned if the key does not
// exist or the value could not be type asserted to an int.
func (s *Session) PopInt(r *http.Request, key string) int {
val := s.Pop(r, key)
i, ok := val.(int)
if !ok {
return 0
}
return i
}
// PopFloat returns the float64 value for a given key and then deletes it from the
// session data. The zero value for an float64 (0) is returned if the key does not
// exist or the value could not be type asserted to a float64.
func (s *Session) PopFloat(r *http.Request, key string) float64 {
val := s.Pop(r, key)
f, ok := val.(float64)
if !ok {
return 0
}
return f
}
// PopBytes returns the byte slice ([]byte) value for a given key and then deletes
// it from the from the session data. The zero value for a slice (nil) is returned
// if the key does not exist or could not be type asserted to []byte.
func (s *Session) PopBytes(r *http.Request, key string) []byte {
val := s.Pop(r, key)
b, ok := val.([]byte)
if !ok {
return nil
}
return b
}
// PopTime returns the time.Time value for a given key and then deletes it from the
// session data. The zero value for a time.Time object is returned if the key does
// not exist or the value could not be type asserted to a time.Time.
func (s *Session) PopTime(r *http.Request, key string) time.Time {
val := s.Pop(r, key)
t, ok := val.(time.Time)
if !ok {
return time.Time{}
}
return t
}