mirror of
https://github.com/foomo/posh.git
synced 2025-10-16 12:45:38 +00:00
44 lines
823 B
Go
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
|
|
}
|