refactor: move cache out of shell

This commit is contained in:
Jan De Dobbeleer 2024-03-05 08:43:56 +01:00 committed by Jan De Dobbeleer
parent 6500282575
commit 414581cf68
2 changed files with 78 additions and 75 deletions

View file

@ -2,15 +2,46 @@ package platform
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"sync"
"time"
)
type Cache interface {
Init(home string)
Close()
// Gets the value for a given key.
// Returns the value and a boolean indicating if the key was found.
// In case the ttl expired, the function returns false.
Get(key string) (string, bool)
// Sets a value for a given key.
// The ttl indicates how many minutes to cache the value.
Set(key, value string, ttl int)
// Deletes a key from the cache.
Delete(key string)
}
const (
CacheFile = "/omp.cache"
)
func pid() string {
pid := os.Getenv("POSH_PID")
if len(pid) == 0 {
pid = strconv.Itoa(os.Getppid())
}
return pid
}
var (
TEMPLATECACHE = fmt.Sprintf("template_cache_%s", pid())
TOGGLECACHE = fmt.Sprintf("toggle_cache_%s", pid())
PROMPTCOUNTCACHE = fmt.Sprintf("prompt_count_cache_%s", pid())
)
type cacheObject struct {
Value string `json:"value"`
Timestamp int64 `json:"timestamp"`
@ -91,3 +122,50 @@ func (fc *fileCache) Delete(key string) {
fc.cache.Delete(key)
fc.dirty = true
}
type commandCache struct {
commands *ConcurrentMap
}
func (c *commandCache) set(command, path string) {
c.commands.Set(command, path)
}
func (c *commandCache) get(command string) (string, bool) {
cacheCommand, found := c.commands.Get(command)
if !found {
return "", false
}
command, ok := cacheCommand.(string)
return command, ok
}
type TemplateCache struct {
Root bool
PWD string
Folder string
Shell string
ShellVersion string
UserName string
HostName string
Code int
Env map[string]string
Var SimpleMap
OS string
WSL bool
PromptCount int
SHLVL int
Segments *ConcurrentMap
SegmentsCache SimpleMap
initialized bool
sync.RWMutex
}
func (t *TemplateCache) AddSegmentData(key string, value any) {
t.Segments.Set(key, value)
}
func (t *TemplateCache) RemoveSegmentData(key string) {
t.Segments.Delete(key)
}

View file

@ -37,20 +37,6 @@ const (
CMD = "cmd"
)
func pid() string {
pid := os.Getenv("POSH_PID")
if len(pid) == 0 {
pid = strconv.Itoa(os.Getppid())
}
return pid
}
var (
TEMPLATECACHE = fmt.Sprintf("template_cache_%s", pid())
TOGGLECACHE = fmt.Sprintf("toggle_cache_%s", pid())
PROMPTCOUNTCACHE = fmt.Sprintf("prompt_count_cache_%s", pid())
)
type Flags struct {
ErrorCode int
PipeStatus string
@ -92,20 +78,6 @@ type FileInfo struct {
IsDir bool
}
type Cache interface {
Init(home string)
Close()
// Gets the value for a given key.
// Returns the value and a boolean indicating if the key was found.
// In case the ttl expired, the function returns false.
Get(key string) (string, bool)
// Sets a value for a given key.
// The ttl indicates how many minutes to cache the value.
Set(key, value string, ttl int)
// Deletes a key from the cache.
Delete(key string)
}
type HTTPRequestModifier func(request *http.Request)
type WindowsRegistryValueType string
@ -168,36 +140,6 @@ type SystemInfo struct {
Disks map[string]disk.IOCountersStat
}
type TemplateCache struct {
Root bool
PWD string
Folder string
Shell string
ShellVersion string
UserName string
HostName string
Code int
Env map[string]string
Var SimpleMap
OS string
WSL bool
PromptCount int
SHLVL int
Segments *ConcurrentMap
SegmentsCache SimpleMap
initialized bool
sync.RWMutex
}
func (t *TemplateCache) AddSegmentData(key string, value any) {
t.Segments.Set(key, value)
}
func (t *TemplateCache) RemoveSegmentData(key string) {
t.Segments.Delete(key)
}
type Environment interface {
Getenv(key string) string
Pwd() string
@ -253,23 +195,6 @@ type Environment interface {
Trace(start time.Time, args ...string)
}
type commandCache struct {
commands *ConcurrentMap
}
func (c *commandCache) set(command, path string) {
c.commands.Set(command, path)
}
func (c *commandCache) get(command string) (string, bool) {
cacheCommand, found := c.commands.Get(command)
if !found {
return "", false
}
command, ok := cacheCommand.(string)
return command, ok
}
type Shell struct {
CmdFlags *Flags
Var SimpleMap