Skip to content

Commit cd40ddb

Browse files
committed
cache by service instead of etag
1 parent 0788847 commit cd40ddb

4 files changed

Lines changed: 24 additions & 21 deletions

File tree

agentcfg/cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
)
2929

3030
const (
31-
cleanupInterval time.Duration = 60 * time.Second
31+
cleanupInterval = 60 * time.Second
3232
)
3333

3434
type cache struct {
@@ -45,7 +45,7 @@ func newCache(logger *logp.Logger, exp time.Duration) *cache {
4545

4646
func (c *cache) fetch(q Query, fetch func() (Result, error)) (Result, error) {
4747
// return from cache if possible
48-
value, found := c.gocache.Get(q.Etag)
48+
value, found := c.gocache.Get(q.toString())
4949
if found && value != nil {
5050
return value.(Result), nil
5151
}
@@ -54,7 +54,7 @@ func (c *cache) fetch(q Query, fetch func() (Result, error)) (Result, error) {
5454
if err != nil {
5555
return result, err
5656
}
57-
c.gocache.SetDefault(result.Source.Etag, result)
57+
c.gocache.SetDefault(q.toString(), result)
5858
if !authorized(q.IsRum, result.Source.Agent) {
5959
return zeroResult(), nil
6060
}

agentcfg/cache_test.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,36 @@ func newCacheSetup(service string, exp time.Duration, init bool) cacheSetup {
4747
result: defaultResult,
4848
}
4949
if init {
50-
setup.c.gocache.SetDefault(setup.result.Source.Etag, setup.result)
50+
setup.c.gocache.SetDefault(setup.q.toString(), setup.result)
5151
}
5252
return setup
5353
}
5454

5555
func TestCache_fetchAndAdd(t *testing.T) {
5656
exp := time.Second
57-
for name, tc := range map[string]struct {
58-
fn func() (Result, error)
59-
init bool
60-
61-
doc Result
62-
fail bool
57+
for name, testCase := range map[string]struct {
58+
fetchFunc func() (Result, error)
59+
init bool
60+
doc Result
61+
shouldFail bool
6362
}{
64-
"DocFromCache": {fn: testFn, init: true, doc: defaultResult},
65-
"DocFromFunctionFails": {fn: testFnErr, fail: true},
66-
"DocFromFunction": {fn: testFn, doc: externalResult},
67-
"EmptyDocFromFunction": {fn: testFnSettingsNil, doc: Result{Source{Settings: Settings{}}}},
68-
"NilDocFromFunction": {fn: testFnNil},
63+
"DocFromCache": {fetchFunc: testFn, init: true, doc: defaultResult},
64+
"DocFromFunctionFails": {fetchFunc: testFnErr, shouldFail: true},
65+
"DocFromFunction": {fetchFunc: testFn, doc: externalResult},
66+
"EmptyDocFromFunction": {fetchFunc: testFnSettingsNil, doc: Result{Source{Settings: Settings{}}}},
67+
"NilDocFromFunction": {fetchFunc: testFnNil},
6968
} {
7069
t.Run(name, func(t *testing.T) {
71-
setup := newCacheSetup(name, exp, tc.init)
70+
setup := newCacheSetup(name, exp, testCase.init)
7271

73-
doc, err := setup.c.fetch(setup.q, tc.fn)
74-
assert.Equal(t, tc.doc, doc)
75-
if tc.fail {
72+
doc, err := setup.c.fetch(setup.q, testCase.fetchFunc)
73+
assert.Equal(t, testCase.doc, doc)
74+
if testCase.shouldFail {
7675
require.Error(t, err)
7776
} else {
7877
assert.NoError(t, err)
7978
//ensure value is cached afterwards
80-
cachedDoc, error := setup.c.fetch(setup.q, tc.fn)
79+
cachedDoc, error := setup.c.fetch(setup.q, testCase.fetchFunc)
8180
require.NoError(t, error)
8281
assert.Equal(t, doc, cachedDoc)
8382
}

agentcfg/fetch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestFetcher_Fetch(t *testing.T) {
9393
fetch(fetcher, 0.8, 0.5)
9494

9595
// after key is expired, fetch from Kibana again
96-
fetcher.cache.gocache.Delete("123")
96+
fetcher.cache.gocache.Delete(query(t.Name()).toString())
9797
fetch(fetcher, 0.7, 0.7)
9898

9999
})

agentcfg/model.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ type Query struct {
5757
IsRum bool `json:"-"`
5858
}
5959

60+
func (q Query) toString() string {
61+
return q.Service.Name + q.Service.Environment
62+
}
63+
6064
// Service holds supported attributes for querying configuration
6165
type Service struct {
6266
Name string `json:"name"`

0 commit comments

Comments
 (0)