mirror of
https://github.com/foomo/posh.git
synced 2026-07-01 15:10:03 +00:00
48 lines
886 B
Go
48 lines
886 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 any) bool {
|
|
namespaces = append(namespaces, key.(string)) //nolint:forcetypeassert
|
|
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) //nolint:forcetypeassert
|
|
}
|
|
|
|
func (c *MemoryCache) List() map[string]Namespace {
|
|
ret := map[string]Namespace{}
|
|
|
|
c.store.Range(func(k, v any) bool {
|
|
ret[k.(string)] = v.(*MemoryNamespace) //nolint:forcetypeassert
|
|
return true
|
|
})
|
|
|
|
return ret
|
|
}
|