Skip to content

Commit

Permalink
Refactor: replace savedValue with value for simplification
Browse files Browse the repository at this point in the history
Signed-off-by: Itamar Holder <[email protected]>
  • Loading branch information
iholder101 committed Apr 9, 2024
1 parent 3e51359 commit 17eb529
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions tools/cache/time-defined-cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
type TimeDefinedCache[T any] struct {
minRefreshDuration time.Duration
lastRefresh *time.Time
savedValue T
value T
reCalcFunc func() (T, error)
valueLock *sync.Mutex
}
Expand Down Expand Up @@ -65,17 +65,17 @@ func (t *TimeDefinedCache[T]) Get() (T, error) {
}

if t.lastRefresh != nil && t.minRefreshDuration.Nanoseconds() != 0 && time.Since(*t.lastRefresh) <= t.minRefreshDuration {
return t.savedValue, nil
return t.value, nil
}

value, err := t.reCalcFunc()
if err != nil {
return t.savedValue, err
return t.value, err
}

t.setWithoutLock(value)

return t.savedValue, nil
return t.value, nil
}

func (t *TimeDefinedCache[T]) Set(value T) {
Expand All @@ -88,7 +88,7 @@ func (t *TimeDefinedCache[T]) Set(value T) {
}

func (t *TimeDefinedCache[T]) setWithoutLock(value T) {
t.savedValue = value
t.value = value

if t.lastRefresh == nil || t.minRefreshDuration.Nanoseconds() != 0 {
t.lastRefresh = k6tpointer.P(time.Now())
Expand Down

0 comments on commit 17eb529

Please sign in to comment.