summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2016-01-05 21:21:31 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2016-01-05 21:21:31 +0000
commitaf7b05e4f6116f21c3d43a5558e87681215cab44 (patch)
treef2e38f2d1f97c67a17d80fbe1d4d485e0d0711ce
parentTest with a class that has suitable semantics instead of a POD type (diff)
downloadlibadhocutil-af7b05e4f6116f21c3d43a5558e87681215cab44.tar.bz2
libadhocutil-af7b05e4f6116f21c3d43a5558e87681215cab44.tar.xz
libadhocutil-af7b05e4f6116f21c3d43a5558e87681215cab44.zip
Add support explicitly removing an item from the cache
-rw-r--r--libadhocutil/cache.h3
-rw-r--r--libadhocutil/cache.impl.h8
-rw-r--r--libadhocutil/unittests/testCache.cpp20
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 )