From 414581cf687f404ffb87d2e62a1d17d36f8b13a9 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Tue, 5 Mar 2024 08:43:56 +0100 Subject: [PATCH] refactor: move cache out of shell --- src/platform/cache.go | 78 +++++++++++++++++++++++++++++++++++++++++++ src/platform/shell.go | 75 ----------------------------------------- 2 files changed, 78 insertions(+), 75 deletions(-) diff --git a/src/platform/cache.go b/src/platform/cache.go index 0e8298ba..f807b572 100644 --- a/src/platform/cache.go +++ b/src/platform/cache.go @@ -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) +} diff --git a/src/platform/shell.go b/src/platform/shell.go index 81941794..e1d70831 100644 --- a/src/platform/shell.go +++ b/src/platform/shell.go @@ -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