From d984605de15b55c3be8a989600cb52faacd68cbc Mon Sep 17 00:00:00 2001 From: Jonathan Lange Date: Tue, 28 Jun 2016 11:25:31 +0100 Subject: [PATCH] Write back to the in-process cache --- app/multitenant/dynamo_collector.go | 13 ++++++++----- app/multitenant/memcache_client.go | 8 ++++---- app/multitenant/s3_client.go | 6 +++--- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/multitenant/dynamo_collector.go b/app/multitenant/dynamo_collector.go index fc281fb64..b5860910f 100644 --- a/app/multitenant/dynamo_collector.go +++ b/app/multitenant/dynamo_collector.go @@ -93,7 +93,7 @@ type DynamoDBCollector interface { // ReportStore is a thing that we can get reports from. type ReportStore interface { - FetchReports([]string) ([]report.Report, []string, error) + FetchReports([]string) (map[string]report.Report, []string, error) } type dynamoDBCollector struct { @@ -291,7 +291,10 @@ func (c *dynamoDBCollector) getReports(reportKeys []string) ([]report.Report, er if err != nil { log.Warningf("Error fetching from cache: %v", err) } - reports = append(reports, found...) + for key, report := range found { + c.inProcess.StoreReport(key, report) + reports = append(reports, report) + } if len(missing) == 0 { return reports, nil } @@ -497,13 +500,13 @@ func newInProcessStore(size int, expiration time.Duration) inProcessStore { } // FetchReports retrieves the given reports from the store. -func (c inProcessStore) FetchReports(keys []string) ([]report.Report, []string, error) { - found := []report.Report{} +func (c inProcessStore) FetchReports(keys []string) (map[string]report.Report, []string, error) { + found := map[string]report.Report{} missing := []string{} for _, key := range keys { rpt, err := c.cache.Get(key) if err == nil { - found = append(found, rpt.(report.Report)) + found[key] = rpt.(report.Report) } else { missing = append(missing, key) } diff --git a/app/multitenant/memcache_client.go b/app/multitenant/memcache_client.go index 6aed287eb..2b311c8b5 100644 --- a/app/multitenant/memcache_client.go +++ b/app/multitenant/memcache_client.go @@ -134,7 +134,7 @@ func memcacheStatusCode(err error) string { } // FetchReports gets reports from memcache. -func (c *MemcacheClient) FetchReports(keys []string) ([]report.Report, []string, error) { +func (c *MemcacheClient) FetchReports(keys []string) (map[string]report.Report, []string, error) { var found map[string]*memcache.Item err := timeRequestStatus("Get", memcacheRequestDuration, memcacheStatusCode, func() error { var err error @@ -165,17 +165,17 @@ func (c *MemcacheClient) FetchReports(keys []string) ([]report.Report, []string, ch <- result{key: key} return } - ch <- result{report: rep} + ch <- result{key: key, report: rep} }(key) } - var reports []report.Report + var reports map[string]report.Report for i := 0; i < len(keys)-len(missing); i++ { r := <-ch if r.report == nil { missing = append(missing, r.key) } else { - reports = append(reports, *r.report) + reports[r.key] = *r.report } } diff --git a/app/multitenant/s3_client.go b/app/multitenant/s3_client.go index 9dca8462d..4cb9acd0e 100644 --- a/app/multitenant/s3_client.go +++ b/app/multitenant/s3_client.go @@ -38,7 +38,7 @@ func NewS3Client(config *aws.Config, bucketName string) S3Store { } // FetchReports fetches multiple reports in parallel from S3. -func (store *S3Store) FetchReports(keys []string) ([]report.Report, []string, error) { +func (store *S3Store) FetchReports(keys []string) (map[string]report.Report, []string, error) { type result struct { key string report *report.Report @@ -55,13 +55,13 @@ func (store *S3Store) FetchReports(keys []string) ([]report.Report, []string, er }(key) } - reports := []report.Report{} + reports := map[string]report.Report{} for range keys { r := <-ch if r.err != nil { return nil, []string{}, r.err } - reports = append(reports, *r.report) + reports[r.key] = *r.report } return reports, []string{}, nil }