-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
40 lines (37 loc) · 1.17 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
package sess
import (
"context"
"time"
)
type Session struct {
sessionID string
storeKey string
hub Hub
rw SessionHttpReadWriter
}
func (s Session) existed(ctx context.Context) (existed bool, err error) {
return s.hub.store.StoreKeyExists(ctx, s.storeKey)
}
func (s Session) ID() (sessionID string) {
return s.sessionID
}
func (s Session) Get(ctx context.Context, field string) (value string, hasValue bool, err error) {
return s.hub.store.Get(ctx, s.storeKey, field)
}
func (s Session) Set(ctx context.Context, field string, value string) (err error) {
return s.hub.store.Set(ctx, s.storeKey, field, value)
}
func (s Session) Delete(ctx context.Context, field string) (err error) {
return s.hub.store.Delete(ctx, s.storeKey, field)
}
func (s Session) Destroy(ctx context.Context) (err error) {
// 如果是 cookie 场景则需要删除 cookie
err = s.rw.Destroy(ctx, s.hub.option) // indivisible begin
if err != nil { // indivisible end
return
}
return s.hub.store.Destroy(ctx, s.storeKey)
}
func (s Session) SessionRemainingTTL(ctx context.Context) (ttl time.Duration, err error) {
return s.hub.store.StoreKeyRemainingTTL(ctx, s.storeKey)
}