Skip to content

Commit

Permalink
- fix after review
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Aug 31, 2022
1 parent 81024e1 commit e4dc56e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
10 changes: 4 additions & 6 deletions storage/timecache/timeCacheCore.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,20 @@ func (tcc *timeCacheCore) upsert(key string, value interface{}, duration time.Du

// put will add the key, value and provided duration, overriding values if the data already existed
// It returns true if the value existed before this call. It also operates on the locker so the call is concurrent safe
func (tcc *timeCacheCore) put(key string, value interface{}, duration time.Duration) (bool, error) {
func (tcc *timeCacheCore) put(key string, value interface{}, duration time.Duration) error {
if len(key) == 0 {
return false, storage.ErrEmptyKey
return storage.ErrEmptyKey
}

tcc.Lock()
defer tcc.Unlock()

_, found := tcc.data[key]

tcc.data[key] = &entry{
timestamp: time.Now(),
span: duration,
value: value,
}
return found, nil
return nil
}

// hasOrAdd will add the key, value and provided duration, if the key is not found
Expand All @@ -89,7 +87,7 @@ func (tcc *timeCacheCore) hasOrAdd(key string, value interface{}, duration time.

_, found := tcc.data[key]
if found {
return found, false, nil
return true, false, nil
}

tcc.data[key] = &entry{
Expand Down
2 changes: 1 addition & 1 deletion storage/timecache/timeCacheCore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestTimeCacheCore_ConcurrentOperations(t *testing.T) {
case 4:
tcc.clear()
case 5:
_, err := tcc.put(fmt.Sprintf("key%d", idx), fmt.Sprintf("valuey%d", idx), time.Second)
err := tcc.put(fmt.Sprintf("key%d", idx), fmt.Sprintf("valuey%d", idx), time.Second)
assert.Nil(t, err)
case 6:
_, _, err := tcc.hasOrAdd(fmt.Sprintf("key%d", idx), fmt.Sprintf("valuey%d", idx), time.Second)
Expand Down
4 changes: 1 addition & 3 deletions storage/timecache/timeCacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (tc *timeCacher) Clear() {

// Put adds a value to the cache. It will always return false since the eviction did not occur
func (tc *timeCacher) Put(key []byte, value interface{}, _ int) (evicted bool) {
_, err := tc.timeCache.put(string(key), value, tc.timeCache.defaultSpan)
err := tc.timeCache.put(string(key), value, tc.timeCache.defaultSpan)
if err != nil {
log.Error("mapTimeCacher.Put", "error", key)
}
Expand Down Expand Up @@ -119,8 +119,6 @@ func (tc *timeCacher) HasOrAdd(key []byte, value interface{}, _ int) (has, added
has, added, err = tc.timeCache.hasOrAdd(string(key), value, tc.timeCache.defaultSpan)
if err != nil {
log.Error("mapTimeCacher.HasOrAdd", "error", key)

return
}

return
Expand Down

0 comments on commit e4dc56e

Please sign in to comment.