Skip to content

Commit

Permalink
Add clear() to ut_map cache (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickhalfasleep authored Jun 1, 2021
1 parent a3589b0 commit 84db0f0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions inc/cappuccino/ut_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,19 @@ class ut_map
return m_keyed_elements.size();
}

/**
* Removes all elements from the cache (which are destroyed), leaving the container size 0.
*/
auto clear() -> void
{
if (!empty())
{
std::lock_guard guard{m_lock};
m_keyed_elements.clear();
m_ttl_list.clear();
}
}

private:
struct keyed_element;
struct ttl_element;
Expand Down
21 changes: 21 additions & 0 deletions test/test_ut_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,24 @@ TEST_CASE("ut_map Insert only long running test.")
REQUIRE(blocked > inserted);
REQUIRE(elapsed >= std::chrono::milliseconds{200});
}

TEST_CASE("ut_map clear cache.")
{
ut_map<uint64_t, std::string> cache{50ms};

REQUIRE(cache.empty());
REQUIRE(cache.insert(1, "test"));
REQUIRE(cache.insert(2, "more tests"));
REQUIRE(cache.insert(8, "yet more tests"));
cache.clear();
REQUIRE(cache.empty());
REQUIRE(cache.insert(2, "more tests"));
REQUIRE(cache.insert(6, "surprise! more tests!"));
auto surprise = cache.find(6);
REQUIRE(surprise.has_value());
REQUIRE(surprise.value() == "surprise! more tests!");
REQUIRE(cache.size() == 2);
cache.clear();
REQUIRE(cache.empty());
REQUIRE(cache.size() == 0);
}

0 comments on commit 84db0f0

Please sign in to comment.