edge/pkg/storage/driver/cache/lfu/map.go

52 lines
771 B
Go

package lfu
import (
"sync"
"sync/atomic"
)
type Map[K comparable, V any] struct {
size atomic.Int32
inner sync.Map
}
func (m *Map[K, V]) Get(key K) (V, bool) {
raw, exists := m.inner.Load(key)
if !exists {
return *new(V), false
}
value, ok := raw.(V)
if !ok {
return *new(V), false
}
return value, true
}
func (m *Map[K, V]) Set(key K, value V) {
_, loaded := m.inner.Swap(key, value)
if !loaded {
m.size.Add(1)
}
}
func (m *Map[K, V]) Delete(key K) {
_, loaded := m.inner.LoadAndDelete(key)
if loaded {
m.size.Add(-1)
}
}
func (m *Map[K, V]) Range(fn func(key, value any) bool) {
m.inner.Range(fn)
}
func (m *Map[K, V]) Len() int {
return int(m.size.Load())
}
func NewMap[K comparable, V any]() *Map[K, V] {
return &Map[K, V]{}
}