mirror of
https://github.com/foomo/posh.git
synced 2025-10-16 12:45:38 +00:00
35 lines
654 B
Go
35 lines
654 B
Go
package cache
|
|
|
|
import "github.com/c-bata/go-prompt"
|
|
|
|
type MemoryNamespace map[string]interface{}
|
|
|
|
func (c MemoryNamespace) Delete(key string) {
|
|
delete(c, 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 {
|
|
keys := make([]string, 0, len(c))
|
|
for k := range c {
|
|
keys = append(keys, k)
|
|
}
|
|
return keys
|
|
}
|
|
|
|
func (c MemoryNamespace) GetSuggests(key string, cb func() interface{}) []prompt.Suggest {
|
|
if v, ok := c.Get(key, cb).([]prompt.Suggest); ok {
|
|
return v
|
|
}
|
|
return nil
|
|
}
|