posh/pkg/cache/memorycache.go
Kevin Franklin Kim 3a81a2482f
feat: use sync map
2025-04-04 16:32:53 +02:00

44 lines
823 B
Go

package cache
import (
"sync"
)
type MemoryCache struct {
store sync.Map
}
func NewMemoryCache() *MemoryCache {
return &MemoryCache{
store: sync.Map{},
}
}
func (c *MemoryCache) Clear(namespaces ...string) {
if len(namespaces) == 0 {
c.store.Range(func(key, value interface{}) bool {
namespaces = append(namespaces, key.(string))
return true
})
}
for _, namespace := range namespaces {
c.Get(namespace).Delete()
}
}
func (c *MemoryCache) Get(namespace string) Namespace {
value, _ := c.store.LoadOrStore(namespace, &MemoryNamespace{
store: sync.Map{},
})
return value.(*MemoryNamespace)
}
func (c *MemoryCache) List() map[string]Namespace {
ret := map[string]Namespace{}
c.store.Range(func(k, v interface{}) bool {
ret[k.(string)] = v.(*MemoryNamespace)
return true
})
return ret
}