Skip to content

Commit

Permalink
Add a lru cache that implements the store interface
Browse files Browse the repository at this point in the history
Signed-off-by: Soule BA <[email protected]>
  • Loading branch information
souleb committed Apr 30, 2024
1 parent 305494d commit 64822d5
Show file tree
Hide file tree
Showing 8 changed files with 579 additions and 121 deletions.
72 changes: 45 additions & 27 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package cache

import (
"fmt"
"sync"
"time"
)
Expand Down Expand Up @@ -71,11 +70,12 @@ func New[T any](maxItems int, keyFunc KeyFunc[T], interval time.Duration) *Cache
return C
}

// Close closes the cache. It also stops the expiration eviction process.
func (c *Cache[T]) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return fmt.Errorf("cache already closed")
return ErrClosed
}
c.janitor.stop <- true
c.closed = true
Expand All @@ -92,21 +92,23 @@ func (c *Cache[T]) Add(object T) error {
}

c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return KeyError{object, fmt.Errorf("cache already closed")}
c.mu.Unlock()
return KeyError{object, ErrClosed}
}
_, found := c.Items[key]
if found {
return KeyError{object, fmt.Errorf("key already exists")}
c.mu.Unlock()
return KeyError{object, ErrAlreadyExists}
}

if c.MaxItems > 0 && len(c.Items) < c.MaxItems {
c.set(key, object)
c.mu.Unlock()
return nil
}

return KeyError{object, fmt.Errorf("Cache[T] is full")}
c.mu.Unlock()
return KeyError{object, ErrFull}
}

// Update adds an item to the cache, replacing any existing item.
Expand All @@ -118,22 +120,24 @@ func (c *Cache[T]) Update(object T) error {
}

c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return KeyError{object, fmt.Errorf("cache already closed")}
c.mu.Unlock()
return KeyError{object, ErrClosed}
}
_, found := c.Items[key]
if found {
c.set(key, object)
c.mu.Unlock()
return nil
}

if c.MaxItems > 0 && len(c.Items) < c.MaxItems {
c.set(key, object)
c.mu.Unlock()
return nil
}

return KeyError{object, fmt.Errorf("Cache[T] is full")}
c.mu.Unlock()
return KeyError{object, ErrFull}
}

func (c *cache[T]) set(key string, object T) {
Expand Down Expand Up @@ -169,19 +173,22 @@ func (c *Cache[T]) GetByKey(key string) (T, bool, error) {
func (c *cache[T]) get(key string) (T, bool, error) {
var res T
c.mu.RLock()
defer c.mu.RUnlock()
if c.closed {
return res, false, fmt.Errorf("cache already closed")
c.mu.RUnlock()
return res, false, ErrClosed
}
item, found := c.Items[key]
if !found {
c.mu.RUnlock()
return res, false, nil
}
if item.Expiration > 0 {
if item.Expiration < time.Now().UnixNano() {
return res, false, fmt.Errorf("key has expired")
c.mu.RUnlock()
return res, false, ErrExpired
}
}
c.mu.RUnlock()
return item.Object, true, nil
}

Expand All @@ -192,11 +199,12 @@ func (c *Cache[T]) Delete(object T) error {
return KeyError{object, err}
}
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return KeyError{object, fmt.Errorf("cache already closed")}
c.mu.Unlock()
return KeyError{object, ErrClosed}
}
delete(c.Items, key)
c.mu.Unlock()
return nil
}

Expand All @@ -206,25 +214,27 @@ func (c *Cache[T]) Delete(object T) error {
// A closed cache cannot be cleared.
func (c *cache[T]) Clear() {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
c.mu.Unlock()
return
}
c.Items = make(map[string]Item[T])
c.mu.Unlock()
}

// ListKeys returns a slice of the keys in the cache.
// If the cache is closed, ListKeys returns nil.
func (c *cache[T]) ListKeys() []string {
c.mu.RLock()
defer c.mu.RUnlock()
if c.closed {
c.mu.RUnlock()
return nil
}
keys := make([]string, 0, len(c.Items))
for k := range c.Items {
keys = append(keys, k)
}
c.mu.RUnlock()
return keys
}

Expand All @@ -236,19 +246,22 @@ func (c *Cache[T]) HasExpired(object T) (bool, error) {
}

c.mu.RLock()
defer c.mu.RUnlock()
if c.closed {
return false, KeyError{object, fmt.Errorf("cache already closed")}
c.mu.RUnlock()
return false, KeyError{object, ErrClosed}
}
item, ok := c.Items[key]
if !ok {
c.mu.RUnlock()
return true, nil
}
if item.Expiration > 0 {
if item.Expiration < time.Now().UnixNano() {
c.mu.RUnlock()
return true, nil
}
}
c.mu.RUnlock()
return false, nil
}

Expand All @@ -261,15 +274,16 @@ func (c *Cache[T]) SetExpiration(object T, expiration time.Duration) error {
}

c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return KeyError{object, fmt.Errorf("cache already closed")}
c.mu.Unlock()
return KeyError{object, ErrClosed}
}
item, ok := c.Items[key]
if ok {
item.Expiration = time.Now().Add(expiration).UnixNano()
c.Items[key] = item
}
c.mu.Unlock()
return nil
}

Expand All @@ -282,34 +296,38 @@ func (c *Cache[T]) GetExpiration(object T) (time.Duration, error) {
return 0, KeyError{object, err}
}
c.mu.RLock()
defer c.mu.RUnlock()
if c.closed {
return 0, KeyError{object, fmt.Errorf("cache already closed")}
c.mu.RUnlock()
return 0, KeyError{object, ErrClosed}
}
item, ok := c.Items[key]
if !ok {
return 0, KeyError{object, fmt.Errorf("key not found")}
c.mu.RUnlock()
return 0, KeyError{object, ErrNotFound}
}
if item.Expiration > 0 {
if item.Expiration < time.Now().UnixNano() {
return 0, KeyError{object, fmt.Errorf("key has expired")}
c.mu.RUnlock()
return 0, KeyError{object, ErrExpired}
}
}
c.mu.RUnlock()
return time.Duration(item.Expiration - time.Now().UnixNano()), nil
}

// DeleteExpired deletes all expired items from the cache.
func (c *cache[T]) DeleteExpired() {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
c.mu.Unlock()
return
}
for k, v := range c.Items {
if v.Expiration > 0 && v.Expiration < time.Now().UnixNano() {
delete(c.Items, k)
}
}
c.mu.Unlock()
}

type janitor[T any] struct {
Expand Down
16 changes: 12 additions & 4 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"testing"
"time"

"github.com/fluxcd/cli-utils/pkg/object"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
kc "k8s.io/client-go/tools/cache"
)

Expand Down Expand Up @@ -114,12 +116,18 @@ func TestCache(t *testing.T) {
t.Run("Add expiring keys", func(t *testing.T) {
g := NewWithT(t)
// new cache with a cleanup interval of 1 second
cache := New[NamespacedNameObject](2, NamespacedNameObjectKeyFunc, 1*time.Second)
cache := New[IdentifiableObject](2, IdentifiableObjectKeyFunc, 1*time.Second)

// Add an object representing an expiring token
obj := NamespacedNameObject{
Namespace: "test-ns",
Name: "test",
obj := IdentifiableObject{
ObjMetadata: object.ObjMetadata{
Namespace: "test-ns",
Name: "test",
GroupKind: schema.GroupKind{
Group: "test-group",
Kind: "TestObject",
},
},
Object: struct {
token string
}{
Expand Down
9 changes: 9 additions & 0 deletions cache/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ package cache

import "fmt"

var (
// ErrNotFound is returned when an item is not found in the cache.
ErrNotFound = fmt.Errorf("not found")
ErrAlreadyExists = fmt.Errorf("already exists")
ErrClosed = fmt.Errorf("cache closed")
ErrFull = fmt.Errorf("cache full")
ErrExpired = fmt.Errorf("key has expired")
)

// KeyError will be returned any time a KeyFunc gives an error; it includes the object
// at fault.
type KeyError struct {
Expand Down
32 changes: 22 additions & 10 deletions cache/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/fluxcd/pkg/cache
go 1.22.0

require (
github.com/fluxcd/cli-utils v0.36.0-flux.7
github.com/onsi/gomega v1.32.0
github.com/prometheus/client_golang v1.19.0
k8s.io/apimachinery v0.30.0
Expand All @@ -12,44 +13,55 @@ require (

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/go-errors/errors v1.5.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.19.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.30.0 // indirect
k8s.io/cli-runtime v0.30.0 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.17.1 // indirect
sigs.k8s.io/kustomize/kyaml v0.17.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading

0 comments on commit 64822d5

Please sign in to comment.