-
Notifications
You must be signed in to change notification settings - Fork 11
/
cache.go
63 lines (52 loc) · 1.42 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
package main
import (
"sync"
"time"
)
// cache implements a simple cache whose items expire.
type cache struct {
sync.RWMutex
Items map[string]time.Time
TTL time.Duration
}
// newCache creates and returns a new cache with the given lifetime for cache
// items.
func newCache(ttl time.Duration) *cache {
return &cache{
Items: make(map[string]time.Time),
TTL: ttl,
}
}
// Count returns the number of elements in the cache.
func (c *cache) Count() int {
c.RLock()
defer c.RUnlock()
return len(c.Items)
}
// pruneLater prunes the given element after ttl.
func (c *cache) pruneLater(key string, ttl time.Duration) {
time.Sleep(ttl)
c.Lock()
defer c.Unlock()
delete(c.Items, key)
}
// Add adds a new string item to the cache.
func (c *cache) Add(key string) {
c.Lock()
defer c.Unlock()
c.Items[key] = time.Now().UTC()
// Spawn a goroutine that deletes the given element after TTL. Note that
// the enclave's endpoint for requesting nonces should not be exposed to
// the Internet because it would allow adversaries to request nonces at a
// high rate, thus spawning many goroutines, which would constitute a
// resource DoS attack.
go c.pruneLater(key, c.TTL)
}
// Exists returns true if the given string item exists in the cache. If the
// item exists but is expired, the function returns false.
func (c *cache) Exists(key string) bool {
c.RLock()
defer c.RUnlock()
_, exists := c.Items[key]
return exists
}