-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache.go
90 lines (68 loc) · 2.11 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package psadm
import (
"context"
"fmt"
"time"
"github.com/patrickmn/go-cache"
)
// CachedClient is a cache-aware psadmin client.
type CachedClient struct {
cache *cache.Cache
client client
}
var _ client = &CachedClient{}
func (c *CachedClient) GetParameterWithDescription(ctx context.Context, key string) (*Parameter, error) {
ck := buildCacheKey("GetParameterWithDescription", key)
if v, found := c.cache.Get(ck); found {
return v.(*Parameter), nil
}
param, err := c.client.GetParameterWithDescription(ctx, key)
if err != nil {
return nil, err
}
c.cache.Set(ck, param, cache.DefaultExpiration)
return param, nil
}
func (c *CachedClient) GetParameter(ctx context.Context, key string) (string, error) {
ck := buildCacheKey("GetParameter", key)
if v, found := c.cache.Get(ck); found {
return v.(string), nil
}
param, err := c.client.GetParameter(ctx, key)
if err != nil {
return "", err
}
c.cache.Set(ck, param, cache.DefaultExpiration)
return param, nil
}
func (c *CachedClient) GetParameterByTime(ctx context.Context, key string, at time.Time) (*Parameter, error) {
ck := fmt.Sprintf("%s/%s/%d", "GetParameterByTime", key, at.Unix())
if v, found := c.cache.Get(ck); found {
return v.(*Parameter), nil
}
param, err := c.client.GetParameterByTime(ctx, key, at)
if err != nil {
return nil, err
}
c.cache.Set(ck, param, cache.DefaultExpiration)
return param, nil
}
func (c *CachedClient) GetParametersByPath(ctx context.Context, pathPrefix string) ([]*Parameter, error) {
ck := buildCacheKey("GetParametersByPath", pathPrefix)
if v, found := c.cache.Get(ck); found {
return v.([]*Parameter), nil
}
params, err := c.client.GetParametersByPath(ctx, pathPrefix)
if err != nil {
return nil, err
}
c.cache.Set(ck, params, cache.DefaultExpiration)
return params, nil
}
// PutParameter forwards a call to the underlying client. It doesn't do any caching.
func (c *CachedClient) PutParameter(ctx context.Context, p *Parameter, overwrite bool) error {
return c.client.PutParameter(ctx, p, overwrite)
}
func buildCacheKey(prefix, key string) string {
return fmt.Sprintf("%s/%s", prefix, key)
}