Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d35689
commit 7d2001c
Showing
5 changed files
with
114 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters