Skip to content

Commit

Permalink
perf(schema): replace LRUExpire cache
Browse files Browse the repository at this point in the history
  • Loading branch information
aruiz14 committed Jul 18, 2024
1 parent aec8218 commit a4782c8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/internal/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package cache

import "time"
import (
"os"
"time"
)

const cacheBackendEnvVar = "CATTLE_STEVE_CACHE_BACKEND"

type Cache[K, V any] interface {
Get(K) (V, bool)
Expand All @@ -11,5 +16,10 @@ type Cache[K, V any] interface {
}

func NewCache[K, V any](maxSize int, ttl time.Duration) Cache[K, V] {
return newLRUExpire[K, V](maxSize, ttl)
switch os.Getenv(cacheBackendEnvVar) {
case "LRU", "lru":
return newLRUExpire[K, V](maxSize, ttl)
default:
return newExpiring[K, V](maxSize, ttl)
}
}
46 changes: 46 additions & 0 deletions pkg/internal/cache/expiring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cache

import (
"time"

"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/cache"
)

func newExpiring[K, V any](maxSize int, ttl time.Duration) Cache[K, V] {
return &wrapExpiring[K, V]{maxSize: maxSize, ttl: ttl, cache: cache.NewExpiring()}
}

// wrapExpiring imposes some static type restrictions around a generic cache.Expiring cache
type wrapExpiring[K, V any] struct {
maxSize int
ttl time.Duration
cache *cache.Expiring
}

func (w wrapExpiring[K, V]) Get(k K) (V, bool) {
if v, ok := w.cache.Get(k); ok {
return v.(V), true
}
// zero value of V
return *new(V), false
}

func (w wrapExpiring[K, V]) Set(k K, v V) {
w.cache.Set(k, v, w.ttl)
if current, max := w.cache.Len(), w.maxSize; current >= max {
logrus.WithField("max", max).WithField("current", current).Warnf("cache reached soft limit")
}
}

func (w wrapExpiring[K, V]) Delete(k K) {
w.cache.Delete(k)
}

func (w wrapExpiring[K, V]) Len() int {
return w.cache.Len()
}

func (w wrapExpiring[K, V]) Reset() {
w.cache = cache.NewExpiring()
}

0 comments on commit a4782c8

Please sign in to comment.