feat: implement lfu based cache strategy
This commit is contained in:
14
pkg/storage/driver/cache/lfu/memory/cache_test.go
vendored
Normal file
14
pkg/storage/driver/cache/lfu/memory/cache_test.go
vendored
Normal 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]()
|
||||
})
|
||||
}
|
40
pkg/storage/driver/cache/lfu/memory/memory.go
vendored
Normal file
40
pkg/storage/driver/cache/lfu/memory/memory.go
vendored
Normal 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]{}
|
Reference in New Issue
Block a user