Repository: JanDeDobbeleer/oh-my-posh
Pull request #1289 (2021-11-23 10:14:58)

refactor(cache): add correct unit indication
### Prerequisites

- [x] I have read and understood the `CONTRIBUTING` guide
- [x] The commit message follows the [conventional commits][cc] guidelines
- [x] Tests for the changes have been added (for bug fixes/features)
- [x] Docs have been added / updated (for bug fixes/features)

### Description

- TTL -> int
- explained TTL is in minutes rather than seconds

<!---

Tips:

If you're not comfortable with working with Git, we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/src/environment.go b/src/environment.go
index e1d9ec6b..fd975461 100644
--- a/src/environment.go
+++ b/src/environment.go
@@ -52,8 +52,8 @@ type cache interface {
 	init(home string)
 	close()
 	get(key string) (string, bool)
-	// ttl in seconds
-	set(key, value string, ttl int64)
+	// ttl in minutes
+	set(key, value string, ttl int)
 }
 
 type environmentInfo interface {
diff --git a/src/environment_cache.go b/src/environment_cache.go
index 3e9db87a..a8b9d3df 100644
--- a/src/environment_cache.go
+++ b/src/environment_cache.go
@@ -13,7 +13,7 @@ const (
 type cacheObject struct {
 	Value     string `json:"value"`
 	Timestamp int64  `json:"timestamp"`
-	TTL       int64  `json:"ttl"`
+	TTL       int    `json:"ttl"`
 }
 
 type fileCache struct {
@@ -49,6 +49,8 @@ func (fc *fileCache) close() {
 	}
 }
 
+// returns the value for the given key as long as
+// the TTL (minutes) is not expired
 func (fc *fileCache) get(key string) (string, bool) {
 	val, found := fc.cache.get(key)
 	if !found {
@@ -58,7 +60,7 @@ func (fc *fileCache) get(key string) (string, bool) {
 	if !ok {
 		return "", false
 	}
-	expired := time.Now().Unix() >= (co.Timestamp + co.TTL*60)
+	expired := time.Now().Unix() >= (co.Timestamp + int64(co.TTL)*60)
 	if expired {
 		fc.cache.remove(key)
 		return "", false
@@ -66,7 +68,8 @@ func (fc *fileCache) get(key string) (string, bool) {
 	return co.Value, true
 }
 
-func (fc *fileCache) set(key, value string, ttl int64) {
+// sets the value for the given key with a TTL (minutes)
+func (fc *fileCache) set(key, value string, ttl int) {
 	fc.cache.set(key, &cacheObject{
 		Value:     value,
 		Timestamp: time.Now().Unix(),
diff --git a/src/segment_owm.go b/src/segment_owm.go
index 56ebe921..8c375634 100644
--- a/src/segment_owm.go
+++ b/src/segment_owm.go
@@ -76,7 +76,7 @@ func (d *owm) string() string {
 }
 
 func (d *owm) getResult() (*owmDataResponse, error) {
-	cacheTimeout := int64(d.props.getInt(CacheTimeout, DefaultCacheTimeout))
+	cacheTimeout := d.props.getInt(CacheTimeout, DefaultCacheTimeout)
 	response := new(owmDataResponse)
 	if cacheTimeout > 0 {
 		// check if data stored in cache


Test patch:
diff --git a/src/environment_cache_mock_test.go b/src/environment_cache_mock_test.go
index b6bf1e0c..96c6e09d 100644
--- a/src/environment_cache_mock_test.go
+++ b/src/environment_cache_mock_test.go
@@ -39,6 +39,6 @@ func (_m *MockedCache) init(home string) {
 }
 
 // set provides a mock function with given fields: key, value, ttl
-func (_m *MockedCache) set(key, value string, ttl int64) {
+func (_m *MockedCache) set(key, value string, ttl int) {
 	_m.Called(key, value, ttl)
 }