Skip to content

Commit

Permalink
feat: cache capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Sep 23, 2021
1 parent 8d35689 commit 7d2001c
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 5 deletions.
30 changes: 30 additions & 0 deletions src/concurrent_map.go
@@ -0,0 +1,30 @@
package main

import "sync"

type concurrentMap struct {
values map[string]string
lock sync.RWMutex
}

func newConcurrentMap() *concurrentMap {
return &concurrentMap{
values: make(map[string]string),
lock: sync.RWMutex{},
}
}

func (c *concurrentMap) set(key, value string) {
c.lock.Lock()
defer c.lock.Unlock()
c.values[key] = value
}

func (c *concurrentMap) get(key string) (string, bool) {
c.lock.RLock()
defer c.lock.RUnlock()
if val, ok := c.values[key]; ok {
return val, true
}
return "", false
}
29 changes: 25 additions & 4 deletions src/environment.go
Expand Up @@ -48,6 +48,13 @@ type fileInfo struct {
isDir bool
}

type cache interface {
init(home string)
close()
get(key string) (string, bool)
set(key, value string)
}

type environmentInfo interface {
getenv(key string) string
getcwd() string
Expand Down Expand Up @@ -77,6 +84,8 @@ type environmentInfo interface {
isWsl() bool
stackCount() int
getTerminalWidth() (int, error)
cache() cache
close()
}

type commandCache struct {
Expand Down Expand Up @@ -128,17 +137,20 @@ func (t *tracer) trace(start time.Time, function string, args ...string) {
}

type environment struct {
args *args
cwd string
cmdCache *commandCache
tracer *tracer
args *args
cwd string
cmdCache *commandCache
fileCache *fileCache
tracer *tracer
}

func (env *environment) init(args *args) {
env.args = args
env.cmdCache = &commandCache{
commands: newConcurrentMap(),
}
env.fileCache = &fileCache{}
env.fileCache.init(env.homeDir())
tracer := &tracer{
debug: *args.Debug,
}
Expand Down Expand Up @@ -461,6 +473,15 @@ func (env *environment) stackCount() int {
return *env.args.StackCount
}

func (env *environment) cache() cache {
return env.fileCache
}

func (env *environment) close() {
env.fileCache.close()
env.tracer.close()
}

func cleanHostName(hostName string) string {
garbage := []string{
".lan",
Expand Down
49 changes: 49 additions & 0 deletions src/environment_cache.go
@@ -0,0 +1,49 @@
package main

import (
"fmt"
"io/ioutil"
"strings"
)

const (
cachePath = "/.omp.cache"
)

type fileCache struct {
cache *concurrentMap
home string
}

func (fc *fileCache) init(home string) {
fc.cache = newConcurrentMap()
fc.home = home
content, err := ioutil.ReadFile(home + cachePath)
if err != nil {
return
}
for _, line := range strings.Split(string(content), "\n") {
if len(line) == 0 || !strings.Contains(line, "=") {
continue
}
kv := strings.SplitN(line, "=", 2)
fc.set(kv[0], kv[1])
}
}

func (fc *fileCache) close() {
var sb strings.Builder
for key, value := range fc.cache.values {
cacheEntry := fmt.Sprintf("%s=%s\n", key, value)
sb.WriteString(cacheEntry)
}
_ = ioutil.WriteFile(fc.home+cachePath, []byte(sb.String()), 0644)
}

func (fc *fileCache) get(key string) (string, bool) {
return fc.cache.get(key)
}

func (fc *fileCache) set(key, value string) {
fc.cache.set(key, value)
}
2 changes: 1 addition & 1 deletion src/main.go
Expand Up @@ -160,7 +160,7 @@ func main() {
flag.Parse()
env := &environment{}
env.init(args)
defer env.tracer.close()
defer env.close()
if *args.Millis {
fmt.Print(time.Now().UnixNano() / 1000000)
return
Expand Down
9 changes: 9 additions & 0 deletions src/segment_path_test.go
Expand Up @@ -153,6 +153,15 @@ func (env *MockedEnvironment) getTerminalWidth() (int, error) {
return args.Int(0), args.Error(1)
}

func (env *MockedEnvironment) cache() cache {
args := env.Called(nil)
return args.Get(0).(cache)
}

func (env *MockedEnvironment) close() {
_ = env.Called(nil)
}

const (
homeBill = "/home/bill"
homeJan = "/usr/home/jan"
Expand Down

0 comments on commit 7d2001c

Please sign in to comment.