blob: 8b22bde0bbb12eb0739e257a0df4e9ef4f065da4 (
plain)
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
|
package cache
import (
"sync"
"time"
cache "github.com/patrickmn/go-cache"
)
type memstore struct {
store *cache.Cache
mux *sync.Mutex
}
var expiration = 10 * time.Minute
func newMemStore() Store {
return &memstore{
store: cache.New(expiration, time.Minute),
mux: &sync.Mutex{},
}
}
func (m *memstore) LoadOrCreate(domain string) *Entry {
m.mux.Lock()
defer m.mux.Unlock()
if entry, exists := m.store.Get(domain); exists {
return entry.(*Entry)
}
entry := newCacheEntry(domain)
m.store.SetDefault(domain, entry)
return entry
}
func (m *memstore) ReplaceOrCreate(domain string, entry *Entry) *Entry {
m.mux.Lock()
defer m.mux.Unlock()
m.store.Delete(domain)
m.store.SetDefault(domain, entry)
return entry
}
|