diff --git a/rsql/rcache.go b/rsql/rcache.go index 894283d..7842ee5 100644 --- a/rsql/rcache.go +++ b/rsql/rcache.go @@ -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{ @@ -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 } @@ -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 } diff --git a/rsql/rcache_test.go b/rsql/rcache_test.go index 91f259f..616455d 100644 --- a/rsql/rcache_test.go +++ b/rsql/rcache_test.go @@ -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 @@ -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, @@ -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