From 6e4d5fbe3a1ed3eba68cf9c484f62259d7890243 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 5 Jan 2016 21:24:14 +0000 Subject: Add support explicitly clearing all items from the cache --- libadhocutil/cache.h | 2 ++ libadhocutil/cache.impl.h | 8 ++++++++ libadhocutil/unittests/testCache.cpp | 2 ++ 3 files changed, 12 insertions(+) diff --git a/libadhocutil/cache.h b/libadhocutil/cache.h index 72ad879..a394e92 100644 --- a/libadhocutil/cache.h +++ b/libadhocutil/cache.h @@ -95,6 +95,8 @@ class DLL_PUBLIC Cache { /** Explicitly remove an item from the cache. * @param k Cache key to remove. */ void remove(const K & k); + /** Explicitly remove ALL items from the cache. */ + void clear(); private: void DLL_PRIVATE prune() const; diff --git a/libadhocutil/cache.impl.h b/libadhocutil/cache.impl.h index c07d9c0..0a2b0b3 100644 --- a/libadhocutil/cache.impl.h +++ b/libadhocutil/cache.impl.h @@ -125,6 +125,14 @@ Cache::remove(const K & k) cached.template get().erase(k); } +template +void +Cache::clear() +{ + Lock(lock); + cached.clear(); +} + template void Cache::prune() const diff --git a/libadhocutil/unittests/testCache.cpp b/libadhocutil/unittests/testCache.cpp index 1f41836..914d282 100644 --- a/libadhocutil/unittests/testCache.cpp +++ b/libadhocutil/unittests/testCache.cpp @@ -83,6 +83,8 @@ BOOST_AUTO_TEST_CASE( multivalues ) BOOST_REQUIRE(!tc.get("key1")); BOOST_REQUIRE_EQUAL(2, *tc.get("key2")); BOOST_REQUIRE_EQUAL(3, *tc.get("key3")); + tc.clear(); + BOOST_REQUIRE_EQUAL(0, tc.size()); } BOOST_AUTO_TEST_CASE( expired ) -- cgit v1.2.3