-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionStoreNano.go
64 lines (50 loc) · 1.47 KB
/
SessionStoreNano.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
package nanostore
import (
"errors"
"unsafe"
"github.com/aerogo/nano"
"github.com/aerogo/session"
)
// SessionStoreNano is a store saving sessions in a nano database.
type SessionStoreNano struct {
collection *nano.Collection
}
// interfaceStruct reflects Go's internal interface{} structure.
type interfaceStruct struct {
Type unsafe.Pointer
Data unsafe.Pointer
}
// New creates a session store using an Aerospike database.
func New(collection *nano.Collection) *SessionStoreNano {
return &SessionStoreNano{
collection: collection,
}
}
// Get loads the initial session values from the database.
func (store *SessionStoreNano) Get(sid string) (*session.Session, error) {
record, err := store.collection.Get(sid)
if err != nil {
return nil, err
}
interfaceContainer := *(*interfaceStruct)(unsafe.Pointer(&record))
data := *(*map[string]interface{})(interfaceContainer.Data)
return session.New(sid, data), nil
}
// Set updates the session values in the database.
func (store *SessionStoreNano) Set(sid string, session *session.Session) error {
sessionData := session.Data()
// Set with nil as data means we should delete the session.
if sessionData == nil {
existed := store.collection.Delete(sid)
if !existed {
return errors.New("Session doesn't exist")
}
return nil
}
store.collection.Set(sid, &sessionData)
return nil
}
// Delete deletes the session with the given ID.
func (store *SessionStoreNano) Delete(sid string) {
store.collection.Delete(sid)
}