diff options
-rw-r--r-- | libadhocutil/cache.h | 3 | ||||
-rw-r--r-- | libadhocutil/cache.impl.h | 8 | ||||
-rw-r--r-- | libadhocutil/unittests/testCache.cpp | 20 |
3 files changed, 31 insertions, 0 deletions
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 @@ -119,6 +119,14 @@ Cache<T, K>::size() const template<typename T, typename K> void +Cache<T, K>::remove(const K & k) +{ + Lock(lock); + cached.template get<byKey>().erase(k); +} + +template<typename T, typename K> +void Cache<T, K>::prune() const { auto now = time(NULL); 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 ) |