...
1 package requestservice
2
3 import (
4 "sync"
5 "time"
6
7 "github.com/hashicorp/go-version"
8
9 "edge-infra.dev/pkg/sds/emergencyaccess/types"
10 )
11
12 const (
13 defaultExpiration = 15 * time.Minute
14 )
15
16 type versionCache struct {
17 mu sync.RWMutex
18 cache map[types.Target]*version.Version
19 expiration time.Duration
20 }
21
22 func (c *versionCache) Get(key types.Target) (val *version.Version, ok bool) {
23 c.mu.RLock()
24 defer c.mu.RUnlock()
25 val, ok = c.cache[key]
26 return val, ok
27 }
28
29 func (c *versionCache) Insert(key types.Target, val *version.Version) {
30 c.mu.Lock()
31 c.cache[key] = val
32 c.mu.Unlock()
33 go func() {
34 time.Sleep(c.expiration)
35 c.Delete(key)
36 }()
37 }
38
39 func (c *versionCache) Delete(key types.Target) {
40 c.mu.Lock()
41 defer c.mu.Unlock()
42 delete(c.cache, key)
43 }
44
View as plain text