/
gsession_manager.go
85 lines (73 loc) · 2.22 KB
/
gsession_manager.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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gsession
import (
"context"
"time"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/os/gcache"
)
// Manager for sessions.
type Manager struct {
ttl time.Duration // TTL for sessions.
storage Storage // Storage interface for session storage.
// sessionData is the memory data cache for session TTL,
// which is available only if the Storage does not store any session data in synchronizing.
// Please refer to the implements of StorageFile, StorageMemory and StorageRedis.
sessionData *gcache.Cache
}
// New creates and returns a new session manager.
func New(ttl time.Duration, storage ...Storage) *Manager {
m := &Manager{
ttl: ttl,
sessionData: gcache.New(),
}
if len(storage) > 0 && storage[0] != nil {
m.storage = storage[0]
} else {
m.storage = NewStorageFile()
}
return m
}
// New creates or fetches the session for given session id.
// The parameter `sessionId` is optional, it creates a new one if not it's passed
// depending on Storage.New.
func (m *Manager) New(ctx context.Context, sessionId ...string) *Session {
var id string
if len(sessionId) > 0 && sessionId[0] != "" {
id = sessionId[0]
}
return &Session{
id: id,
ctx: ctx,
manager: m,
}
}
// SetStorage sets the session storage for manager.
func (m *Manager) SetStorage(storage Storage) {
m.storage = storage
}
// GetStorage returns the session storage of current manager.
func (m *Manager) GetStorage() Storage {
return m.storage
}
// SetTTL the TTL for the session manager.
func (m *Manager) SetTTL(ttl time.Duration) {
m.ttl = ttl
}
// TTL returns the TTL of the session manager.
func (m *Manager) TTL() time.Duration {
return m.ttl
}
// UpdateSessionTTL updates the ttl for given session.
func (m *Manager) UpdateSessionTTL(sessionId string, data *gmap.StrAnyMap) {
ctx := context.Background()
err := m.sessionData.Set(ctx, sessionId, data, m.ttl)
if err != nil {
intlog.Errorf(ctx, `%+v`, err)
}
}