-
Notifications
You must be signed in to change notification settings - Fork 21
/
store.go
112 lines (90 loc) · 2.22 KB
/
store.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
package session
import (
"time"
"github.com/savsgio/gotils/strconv"
)
func newDictValue() Dict {
return Dict{
KV: make(map[string]interface{}),
}
}
// NewStore returns a new empty store
func NewStore() *Store {
return &Store{
data: newDictValue(),
}
}
// Get returns a value from the given key
func (s *Store) Get(key string) interface{} {
return s.data.KV[key]
}
// GetBytes returns a value from the given key
func (s *Store) GetBytes(key []byte) interface{} {
return s.Get(strconv.B2S(key))
}
// GetAll returns all stored values
func (s *Store) GetAll() Dict {
return s.data
}
// Ptr returns the internal store pointer
func (s *Store) Ptr() *Dict {
return &s.data
}
// Set saves a value for the given key
func (s *Store) Set(key string, value interface{}) {
s.data.KV[key] = value
}
// SetBytes saves a value for the given key
func (s *Store) SetBytes(key []byte, value interface{}) {
s.Set(strconv.B2S(key), value)
}
// Delete deletes a value from the given key
func (s *Store) Delete(key string) {
delete(s.data.KV, key)
}
// DeleteBytes deletes a value from the given key
func (s *Store) DeleteBytes(key []byte) {
s.Delete(strconv.B2S(key))
}
// Flush removes all stored values
func (s *Store) Flush() {
for k := range s.data.KV {
delete(s.data.KV, k)
}
}
// GetSessionID returns the session id
func (s *Store) GetSessionID() []byte {
s.lock.RLock()
id := s.sessionID
s.lock.RUnlock()
return id
}
// SetSessionID sets the session id
func (s *Store) SetSessionID(id []byte) {
s.lock.Lock()
s.sessionID = id
s.lock.Unlock()
}
// HasExpirationChanged checks wether the expiration has been changed
func (s *Store) HasExpirationChanged() bool {
return s.Get(expirationAttrKey) != nil
}
// GetExpiration returns the expiration for current session
func (s *Store) GetExpiration() time.Duration {
expiration, ok := s.Get(expirationAttrKey).(int64)
if !ok {
return s.defaultExpiration
}
return time.Duration(expiration)
}
// SetExpiration sets the expiration for current session
func (s *Store) SetExpiration(expiration time.Duration) error {
s.Set(expirationAttrKey, int64(expiration))
return nil
}
// Reset resets the store
func (s *Store) Reset() {
s.Flush()
s.sessionID = s.sessionID[:0]
s.defaultExpiration = 0
}