feat: implement lfu based cache strategy
All checks were successful
arcad/edge/pipeline/head This commit looks good
arcad/edge/pipeline/pr-master This commit looks good

This commit is contained in:
2024-01-06 16:48:14 +01:00
parent b9c08f647c
commit a276b92a03
29 changed files with 1858 additions and 165 deletions

View File

@ -0,0 +1,14 @@
package memory
import (
"testing"
"forge.cadoles.com/arcad/edge/pkg/storage/driver/cache/lfu"
"forge.cadoles.com/arcad/edge/pkg/storage/driver/cache/lfu/testsuite"
)
func TestCacheWithMemoryStore(t *testing.T) {
testsuite.TestCacheWithStore(t, func(testName string) lfu.Store[string, string] {
return NewStore[string, string]()
})
}

View File

@ -0,0 +1,40 @@
package memory
import (
"forge.cadoles.com/arcad/edge/pkg/storage/driver/cache/lfu"
"github.com/pkg/errors"
)
type Store[K comparable, V any] struct {
index *lfu.Map[K, V]
}
// Delete implements Store.
func (s *Store[K, V]) Delete(key K) error {
s.index.Delete(key)
return nil
}
// Get implements Store.
func (s *Store[K, V]) Get(key K) (V, error) {
value, exists := s.index.Get(key)
if !exists {
return *new(V), errors.WithStack(lfu.ErrNotFound)
}
return value, nil
}
// Set implements Store.
func (s *Store[K, V]) Set(key K, value V) error {
s.index.Set(key, value)
return nil
}
func NewStore[K comparable, V any]() *Store[K, V] {
return &Store[K, V]{
index: lfu.NewMap[K, V](),
}
}
var _ lfu.Store[string, int] = &Store[string, int]{}