From af7b05e4f6116f21c3d43a5558e87681215cab44 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 5 Jan 2016 21:21:31 +0000 Subject: Add support explicitly removing an item from the cache --- libadhocutil/cache.h | 3 +++ libadhocutil/cache.impl.h | 8 ++++++++ libadhocutil/unittests/testCache.cpp | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/libadhocutil/cache.h b/libadhocutil/cache.h index 06d4d5a..72ad879 100644 --- a/libadhocutil/cache.h +++ b/libadhocutil/cache.h @@ -92,6 +92,9 @@ class DLL_PUBLIC Cache { * determine or estimate the amount of memory used by items in the cache without further * knowledge of the items themselves. */ size_t size() const; + /** Explicitly remove an item from the cache. + * @param k Cache key to remove. */ + void remove(const K & k); private: void DLL_PRIVATE prune() const; diff --git a/libadhocutil/cache.impl.h b/libadhocutil/cache.impl.h index 7b0340c..c07d9c0 100644 --- a/libadhocutil/cache.impl.h +++ b/libadhocutil/cache.impl.h @@ -117,6 +117,14 @@ Cache::size() const return cached.size(); } +template +void +Cache::remove(const K & k) +{ + Lock(lock); + cached.template get().erase(k); +} + template void Cache::prune() const diff --git a/libadhocutil/unittests/testCache.cpp b/libadhocutil/unittests/testCache.cpp index 650e3c7..1f41836 100644 --- a/libadhocutil/unittests/testCache.cpp +++ b/libadhocutil/unittests/testCache.cpp @@ -63,6 +63,26 @@ BOOST_AUTO_TEST_CASE( hit ) BOOST_REQUIRE_EQUAL(vu, tc.getItem("key")->validUntil); BOOST_REQUIRE_EQUAL("key", tc.getItem("key")->key); BOOST_REQUIRE_EQUAL(1, tc.size()); + tc.remove("key"); + BOOST_REQUIRE_EQUAL(0, tc.size()); +} + +BOOST_AUTO_TEST_CASE( multivalues ) +{ + TestCache tc; + auto vu = time(NULL) + 5; + tc.add("key1", 1, vu); + tc.add("key2", 2, vu); + tc.add("key3", 3, vu); + BOOST_REQUIRE_EQUAL(3, tc.size()); + BOOST_REQUIRE_EQUAL(1, *tc.get("key1")); + BOOST_REQUIRE_EQUAL(2, *tc.get("key2")); + BOOST_REQUIRE_EQUAL(3, *tc.get("key3")); + tc.remove("key1"); + BOOST_REQUIRE_EQUAL(2, tc.size()); + BOOST_REQUIRE(!tc.get("key1")); + BOOST_REQUIRE_EQUAL(2, *tc.get("key2")); + BOOST_REQUIRE_EQUAL(3, *tc.get("key3")); } BOOST_AUTO_TEST_CASE( expired ) -- cgit v1.2.3