-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
memory.go
59 lines (49 loc) · 1.06 KB
/
memory.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
package jwt
import (
"github.com/firmeve/firmeve/kernel/contract"
"time"
)
type Memory struct {
jwts map[string]int64
audiences map[string][]string
}
func NewMemoryStore() contract.JwtStore {
return &Memory{
jwts: make(map[string]int64, 0),
audiences: make(map[string][]string, 0),
}
}
func (m *Memory) Has(id string) bool {
if v, ok := m.jwts[id]; ok {
// 判断时间
if v <= time.Now().Unix() {
// clear过期数据
m.Clear(id)
return false
}
return true
}
return false
}
func (m *Memory) Put(id string, audience contract.JwtAudience, lifetime time.Time) error {
m.jwts[id] = lifetime.Unix()
aud := audience.Audience()
if _, ok := m.audiences[aud]; !ok {
m.audiences[aud] = make([]string, 0)
}
m.audiences[aud] = append(m.audiences[aud], id)
return nil
}
func (m *Memory) Forget(audience contract.JwtAudience) error {
aud := audience.Audience()
if v, ok := m.audiences[aud]; ok {
for _, id := range v {
m.Clear(id)
}
delete(m.audiences, aud)
}
return nil
}
func (m *Memory) Clear(id string) {
delete(m.jwts, id)
}