-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
56 lines (45 loc) · 904 Bytes
/
cache.go
File metadata and controls
56 lines (45 loc) · 904 Bytes
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
//go:build !wasm
package user
import (
"github.com/tinywasm/orm"
"sync"
"time"
)
type sessionCache struct {
mu sync.RWMutex
items map[string]Session
}
func newSessionCache() *sessionCache {
return &sessionCache{
items: make(map[string]Session),
}
}
func (c *sessionCache) warmUp(db *orm.DB) error {
qb := db.Query(&Session{}).Where(Session_.ExpiresAt).Gt(time.Now().Unix())
sessions, err := ReadAllSession(qb)
if err != nil {
return err
}
c.mu.Lock()
defer c.mu.Unlock()
for _, s := range sessions {
c.items[s.ID] = *s
}
return nil
}
func (c *sessionCache) set(id string, s Session) {
c.mu.Lock()
defer c.mu.Unlock()
c.items[id] = s
}
func (c *sessionCache) get(id string) (Session, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
s, ok := c.items[id]
return s, ok
}
func (c *sessionCache) delete(id string) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.items, id)
}