Write back to the in-process cache

This commit is contained in:
Jonathan Lange
2016-06-28 11:25:31 +01:00
parent 5ec422c7a3
commit d984605de1
3 changed files with 15 additions and 12 deletions

View File

@@ -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)
}

View File

@@ -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
}
}

View File

@@ -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
}