Skip to content

Commit

Permalink
Make sure that cached result always refresh the cache for a zero refr…
Browse files Browse the repository at this point in the history
…esh period
  • Loading branch information
sjanel committed May 5, 2024
1 parent 8783fc2 commit f926fb4
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/tech/include/cachedresult.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ class CachedResultWithArgs : public CachedResultBase<typename ClockT::duration>
TKey key(std::forward<Args &&>(funcArgs)...);
auto [it, isInserted] = _data.try_emplace(key, flattenTuple, key, nowTime);
if (!isInserted && this->_state != State::kForceCache &&
this->_refreshPeriod < nowTime - it->second.lastUpdatedTs) {
// less or equal to make sure value is always refreshed for a zero refresh period
this->_refreshPeriod <= nowTime - it->second.lastUpdatedTs) {
it->second = Value(flattenTuple, std::move(key), nowTime);
}
return it->second.result;
Expand Down
2 changes: 1 addition & 1 deletion src/tech/test/cachedresult_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ TEST_F(CachedResultTest, SetInCache) {
cachedResult.set(42, nowTime, 3, 4); // timestamp too old, should not be set
EXPECT_EQ(cachedResult.get(3, 4), 7);

cachedResult.set(42, nowTime + 2 * kCacheExpireTime, 3, 4); // should be set
cachedResult.set(42, SteadyClock::now(), 3, 4); // should be set
EXPECT_EQ(cachedResult.get(3, 4), 42);
}

Expand Down

0 comments on commit f926fb4

Please sign in to comment.