41 lines
763 B
Go
41 lines
763 B
Go
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]{}
|