-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcache.go
174 lines (150 loc) · 4.33 KB
/
cache.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package sessions
import (
"sync"
"time"
)
// cache implements a simple LRU write-though cache for user sessions. It
// is used implicitly by all sessions functions.
//
// Member functions should not be called while sessions are locked.
type cache struct {
sync.Mutex
sessions map[string]*Session
}
// sessions is the global sessions cache.
var sessions *cache
// initCache initializes the global sessions cache.
func initCache() {
sessions = &cache{
sessions: make(map[string]*Session),
}
}
// Get returns a session with the given ID from the cache. If the session is not
// cached, the persistence layer is asked to load and return the session. If no
// such session exists, a nil session may be returned. This function does not
// update the session's last access date.
func (c *cache) Get(id string) (*Session, error) {
c.Lock()
defer c.Unlock()
// Do we have a cached session?
session, ok := c.sessions[id]
if !ok {
// Not cached. Query the persistence layer for a session.
var err error
session, err = Persistence.LoadSession(id)
if err != nil {
return nil, err
}
if session != nil {
// Save it in the cache.
if MaxSessionCacheSize != 0 {
c.compact(1)
c.sessions[id] = session
}
// Store ID.
session.Lock()
session.id = id
session.Unlock()
}
}
return session, nil
}
// Set inserts or updates a session in the cache. Since this is a write-through
// cache, the persistence layer is also triggered to save the session.
func (c *cache) Set(session *Session) error {
c.Lock()
defer c.Unlock()
session.Lock()
session.lastAccess = time.Now()
id := session.id
session.Unlock()
// Try to compact the cache.
var requiredSpace int
if _, ok := c.sessions[id]; !ok {
requiredSpace = 1
}
c.compact(requiredSpace)
// Save in cache.
if MaxSessionCacheSize != 0 {
c.sessions[id] = session
}
// Write through to database.
if err := Persistence.SaveSession(id, session); err != nil {
return err
}
return nil
}
// Delete deletes a session. A logged-in user will be logged out.
func (c *cache) Delete(id string) error {
c.Lock()
defer c.Unlock()
// Remove from cache.
delete(c.sessions, id)
// Remove from database.
return Persistence.DeleteSession(id)
}
// compact drops sessions from the cache to make space for the given number
// of sessions. It also drops sessions that have been in the cache longer than
// SessionCacheExpiry. The number of dropped sessions are returned. Dropped
// sessions are updated in the persistence layer to update the last access time.
//
// This function does not synchronize concurrent access to the cache.
func (c *cache) compact(requiredSpace int) (int, error) {
// Check for old sessions.
for id, session := range c.sessions {
session.RLock()
age := time.Since(session.lastAccess)
session.RUnlock()
if age > SessionCacheExpiry {
if err := Persistence.SaveSession(id, session); err != nil {
return 0, err
}
delete(c.sessions, id)
}
}
// Cache may still grow.
if MaxSessionCacheSize < 0 || len(c.sessions)+requiredSpace <= MaxSessionCacheSize {
return 0, nil
}
// Drop the oldest sessions.
var dropped int
if requiredSpace > MaxSessionCacheSize {
requiredSpace = MaxSessionCacheSize // We can't request more than is allowed.
}
for len(c.sessions)+requiredSpace > MaxSessionCacheSize {
// Find oldest sessions and delete them.
var (
oldestAccessTime time.Time
oldestSessionID string
)
for id, session := range c.sessions {
session.RLock()
before := session.lastAccess.Before(oldestAccessTime)
session.RUnlock()
if oldestSessionID == "" || before {
oldestSessionID = id
oldestAccessTime = session.lastAccess
}
}
if err := Persistence.SaveSession(oldestSessionID, c.sessions[oldestSessionID]); err != nil {
return 0, err
}
delete(c.sessions, oldestSessionID)
dropped++
}
return dropped, nil
}
// PurgeSessions removes all sessions from the local cache. The current cache
// content is also saved via the persistence layer, to update the session last
// access times.
func PurgeSessions() {
sessions.Lock()
defer sessions.Unlock()
// Update all sessions in the database.
for id, session := range sessions.sessions {
Persistence.SaveSession(id, session)
// We only do this to update the last access time. Errors are not that
// bad.
}
sessions.sessions = make(map[string]*Session, MaxSessionCacheSize)
}