-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
114 lines (98 loc) · 2.45 KB
/
session.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
package sessionmanager
import (
"errors"
"fmt"
"sync"
"time"
"github.com/google/uuid"
)
// Session is the struct implementation for session
// ID is the unique id for session
// Data is the data for session
type Session struct {
ID string
Data map[string]interface{}
m *sync.RWMutex
ExpirationTime time.Time
Expired bool
Active bool
}
// Verify that Session implements ISession
var _ ISession = (*Session)(nil)
// NewSession is the constructor for session by default expiration time is 30 minutes
// and the session is active you can edit this values by setting the ExpirationTime and Active fields
func NewSession(data map[string]interface{}) *Session {
sessionId := uuid.New().String()
if data == nil {
data = make(map[string]interface{})
}
return &Session{
ID: sessionId,
Data: data,
m: &sync.RWMutex{},
Active: true,
ExpirationTime: time.Now().Add(time.Minute * 5),
Expired: false,
}
}
// Get a value from session
func (s *Session) Get(key string) (interface{}, error) {
s.m.RLock()
defer s.m.RUnlock()
if _, ok := s.Data[key]; !ok {
return nil, errors.New("key not found")
}
return s.Data[key], nil
}
// Set a value to session
func (s *Session) Set(key string, value interface{}) error {
s.m.Lock()
defer s.m.Unlock()
if _, ok := s.Data[key]; ok {
return fmt.Errorf("key %s already exists, for replace delete it first", key)
}
s.Data[key] = value
return nil
}
// Delete a value from session
func (s *Session) Delete(key string) error {
s.m.Lock()
defer s.m.Unlock()
if _, ok := s.Data[key]; !ok {
return errors.New("key not found")
}
delete(s.Data, key)
return nil
}
// SessionId returns the session id
func (s *Session) SessionId() string {
return s.ID
}
// SetExpirationTime sets the expiration time for session in case you
// want to change the default expiration time
func (s *Session) SetExpirationTime(expirationTime time.Time) {
s.m.Lock()
s.ExpirationTime = expirationTime
s.m.Unlock()
}
// IsExpired returns true if the session is expired
func (s *Session) IsExpired() bool {
s.m.RLock()
if s.Expired {
return true
}
s.m.RUnlock()
if s.Active && time.Now().After(s.ExpirationTime) {
s.m.Lock()
s.Expired = true
s.Active = false
s.m.Unlock()
}
return s.Expired
}
// IsActive returns true if the session is active
func (s *Session) IsActive() bool {
s.m.RLock()
defer s.m.RUnlock()
return s.Active
}