Skip to content

Commit

Permalink
Merge pull request #4 from creativecreature/refresh-deletions
Browse files Browse the repository at this point in the history
fix: Improve the refresh deletions
  • Loading branch information
viccon authored May 4, 2024
2 parents 2a9949d + eae040c commit 3a2d875
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
7 changes: 7 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,10 @@ func (c *Client) set(key string, value any, isMissingRecord bool) bool {
shard := c.getShard(key)
return shard.set(key, value, isMissingRecord)
}

// get retrieves a single value from the cache.
func (c *Client) get(key string) (any, bool) {
shard := c.getShard(key)
value, ok, _, _ := shard.get(key)
return value, ok
}
1 change: 0 additions & 1 deletion get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ func TestGetFetchBatchStampedeProtection(t *testing.T) {
<-fetchObserver.FetchCompleted
fetchObserver.AssertRequestedRecords(t, ids)
fetchObserver.AssertFetchCount(t, 1)
fetchObserver.Clear()

// Set the clock to be just before the min cache refresh threshold.
// This should not be enough to make the cache call our fetchFn.
Expand Down
4 changes: 2 additions & 2 deletions passthrough_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ func TestHalfPassthroughBatch(t *testing.T) {
}

// It's not possible to know how many requests we'll let through. We expect
// half, which would be 50, but let's use 10 as a margin of safety.
safetyMargin := 10
// half, which would be 50, but let's use 15 as a margin of safety.
safetyMargin := 15
for i := 0; i < (numPassthroughs/2)-safetyMargin; i++ {
<-fetchObserver.FetchCompleted
}
Expand Down
20 changes: 15 additions & 5 deletions refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ func refreshBatch[T any](c *Client, ids []string, keyFn KeyFn, fetchFn BatchFetc
return
}

if c.storeMisses && len(response) < len(ids) {
for _, id := range ids {
if v, ok := response[id]; !ok {
c.set(keyFn(id), v, true)
}
// Check if any of the records have been deleted at the data source.
for _, id := range ids {
_, okCache := c.get(keyFn(id))
v, okResponse := response[id]

if okResponse {
continue
}

if !c.storeMisses && !okResponse && okCache {
c.Delete(keyFn(id))
}

if c.storeMisses && !okResponse {
c.set(keyFn(id), v, true)
}
}

Expand Down

0 comments on commit 3a2d875

Please sign in to comment.