-
Notifications
You must be signed in to change notification settings - Fork 22
/
cache.go
49 lines (38 loc) · 903 Bytes
/
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
package kms
import (
"context"
"fmt"
"time"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/giantswarm/operatorkit/v7/pkg/controller/context/cachekeycontext"
gocache "github.com/patrickmn/go-cache"
)
const (
expiration = 5 * time.Minute
)
type Cache struct {
cache *gocache.Cache
}
func NewCache() *Cache {
r := &Cache{
cache: gocache.New(expiration, expiration/2),
}
return r
}
func (r *Cache) Get(ctx context.Context, key string) (*kms.DescribeKeyOutput, bool) {
val, ok := r.cache.Get(key)
if ok {
return val.(*kms.DescribeKeyOutput), true
}
return &kms.DescribeKeyOutput{}, false
}
func (r *Cache) Key(ctx context.Context, id string) string {
ck, ok := cachekeycontext.FromContext(ctx)
if ok {
return fmt.Sprintf("%s/%s", ck, id)
}
return ""
}
func (r *Cache) Set(ctx context.Context, key string, val *kms.DescribeKeyOutput) {
r.cache.SetDefault(key, val)
}