Skip to content

Commit

Permalink
rcache: Allow disabling of rcache (#18)
Browse files Browse the repository at this point in the history
* rcache: Allow disabling of rcache

* clean up
  • Loading branch information
andrewwormald authored Oct 16, 2024
1 parent 05f78b5 commit 9452d3b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
14 changes: 12 additions & 2 deletions rsql/rcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ type rcache struct {
limit int
}

// cacheDisabled is the source of truth whether the rcache is disabled or not.
var cacheDisabled bool

// DisableCache results in all loading going straight through to the underlying loader.
func DisableCache() {
cacheDisabled = true
}

// newRCache returns a new read-through cache.
func newRCache(loader loader, name string) *rcache {
return &rcache{
Expand Down Expand Up @@ -62,7 +70,8 @@ func (c *rcache) tailUnsafe() int64 {
func (c *rcache) Load(ctx context.Context, dbc *sql.DB,
prev int64, lag time.Duration,
) ([]*reflex.Event, error) {
if res, ok := c.maybeHit(prev+1, lag); ok {
res, ok := c.maybeHit(prev+1, lag)
if !cacheDisabled && ok {
rcacheHitsCounter.WithLabelValues(c.name).Inc()
return res, nil
}
Expand Down Expand Up @@ -112,7 +121,8 @@ func (c *rcache) readThrough(ctx context.Context, dbc *sql.DB,
defer c.mu.Unlock()

// Recheck cache after waiting for lock
if res, ok := c.maybeHitUnsafe(prev+1, lag); ok {
res, ok := c.maybeHitUnsafe(prev+1, lag)
if !cacheDisabled && ok {
return res, nil
}

Expand Down
24 changes: 23 additions & 1 deletion rsql/rcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ func TestGap(t *testing.T) {

func TestRCache(t *testing.T) {
tests := []struct {
name string
name string
disabled bool

add1 int
q1 int64
len1 int
Expand Down Expand Up @@ -100,6 +102,20 @@ func TestRCache(t *testing.T) {
q2Count: 1,
cLen2: 2,
},
{
name: "disabled-miss-hit",
disabled: true,
add1: 2,
len1: 2,
total1: 1,
q1Count: 1,
cLen1: 2,

len2: 2,
total2: 2,
q2Count: 2,
cLen2: 2,
},
{
name: "miss-hit-offset",
add1: 3,
Expand Down Expand Up @@ -161,6 +177,12 @@ func TestRCache(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.disabled {
DisableCache()
t.Cleanup(func() {
cacheDisabled = false
})
}
q := newQ()
c := newRCache(q.Load, "test")
c.limit = rCacheLimit
Expand Down

0 comments on commit 9452d3b

Please sign in to comment.