-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
43 lines (36 loc) · 714 Bytes
/
cache.go
File metadata and controls
43 lines (36 loc) · 714 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
package traefik_auth_middleware
import (
"sync"
"time"
)
const SIZE = 1024
type Cache struct {
sync.RWMutex
dirty map[string]Token
}
// Get token from cache. If token not found return status false.
func (c *Cache) Get(key string) (token Token, ok bool) {
c.RLock()
token, ok = c.dirty[key]
c.RUnlock()
return token, ok
}
// Store a token inside cache
func (c *Cache) Store(key string, t Token) {
c.Lock()
if c.dirty == nil {
c.dirty = make(map[string]Token, SIZE)
}
c.dirty[key] = t
c.Unlock()
}
// Clears cache of any expired tokens
func (c *Cache) ClearExpired() {
c.Lock()
for k, v := range c.dirty {
if v.ExpirationTime.Before(time.Now()) {
delete(c.dirty, k)
}
}
c.Unlock()
}