feat: use sync map

This commit is contained in:
Kevin Franklin Kim 2025-04-04 16:32:53 +02:00
parent f2fece8fe3
commit 3a81a2482f
No known key found for this signature in database
5 changed files with 60 additions and 43 deletions

2
pkg/cache/cache.go vendored
View File

@ -2,6 +2,6 @@ package cache
type Cache interface { type Cache interface {
Get(namespace string) Namespace Get(namespace string) Namespace
Clear(namespace string) Clear(namespaces ...string)
List() map[string]Namespace List() map[string]Namespace
} }

View File

@ -1,32 +1,43 @@
package cache package cache
type MemoryCache map[string]MemoryNamespace import (
"sync"
)
func NewMemoryCache() MemoryCache { type MemoryCache struct {
return MemoryCache{} store sync.Map
} }
func (c MemoryCache) Clear(namespace string) { func NewMemoryCache() *MemoryCache {
if namespace == "" { return &MemoryCache{
for _, value := range c { store: sync.Map{},
value.Delete("")
}
} else {
c.Get(namespace).Delete("")
} }
} }
func (c MemoryCache) Get(namespace string) Namespace { func (c *MemoryCache) Clear(namespaces ...string) {
if _, ok := c[namespace]; !ok { if len(namespaces) == 0 {
c[namespace] = MemoryNamespace{} c.store.Range(func(key, value interface{}) bool {
namespaces = append(namespaces, key.(string))
return true
})
}
for _, namespace := range namespaces {
c.Get(namespace).Delete()
} }
return c[namespace]
} }
func (c MemoryCache) List() map[string]Namespace { 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{} ret := map[string]Namespace{}
for s, namespace := range c { c.store.Range(func(k, v interface{}) bool {
ret[s] = namespace ret[k.(string)] = v.(*MemoryNamespace)
} return true
})
return ret return ret
} }

View File

@ -1,38 +1,44 @@
package cache package cache
import "github.com/c-bata/go-prompt" import (
"sync"
type MemoryNamespace map[string]interface{} "github.com/c-bata/go-prompt"
)
func (c MemoryNamespace) Delete(key string) { type MemoryNamespace struct {
if key == "" { store sync.Map
for key := range c { }
delete(c, key)
} func (c *MemoryNamespace) Delete(keys ...string) {
if len(keys) == 0 {
c.store.Clear()
} else { } else {
delete(c, key) for _, key := range keys {
} c.store.Delete(key)
}
func (c MemoryNamespace) Get(key string, cb func() interface{}) interface{} {
if _, ok := c[key]; !ok {
if cb == nil {
return nil
} }
c[key] = cb()
} }
return c[key]
} }
func (c MemoryNamespace) Keys() []string { func (c *MemoryNamespace) Get(key string, cb func() any) any {
keys := make([]string, 0, len(c)) value, ok := c.store.Load(key)
for k := range c { if !ok && cb != nil {
keys = append(keys, k) value = cb()
c.store.Store(key, value)
} }
return value
}
func (c *MemoryNamespace) Keys() []string {
var keys []string
c.store.Range(func(k, v interface{}) bool {
keys = append(keys, k.(string))
return true
})
return keys return keys
} }
func (c MemoryNamespace) GetSuggests(key string, cb func() interface{}) []prompt.Suggest { func (c *MemoryNamespace) GetSuggests(key string, cb func() any) []prompt.Suggest {
if v, ok := c.Get(key, cb).([]prompt.Suggest); ok { if v, ok := c.Get(key, cb).([]prompt.Suggest); ok {
return v return v
} }

View File

@ -7,6 +7,6 @@ import (
type Namespace interface { type Namespace interface {
Get(key string, cb func() any) any Get(key string, cb func() any) any
Keys() []string Keys() []string
Delete(key string) Delete(keys ...string)
GetSuggests(key string, cb func() any) []prompt.Suggest GetSuggests(key string, cb func() any) []prompt.Suggest
} }

View File

@ -106,7 +106,7 @@ func (c *Cache) clear(ctx context.Context, r *readline.Readline) error {
} }
} else { } else {
c.l.Info("clearing all caches") c.l.Info("clearing all caches")
c.cache.Clear("") c.cache.Clear()
} }
return nil return nil
} }