diff --git a/include/cache.hpp b/include/cache.hpp index 26e385d..df32690 100644 --- a/include/cache.hpp +++ b/include/cache.hpp @@ -73,7 +73,8 @@ class fixed_sized_cache * \param[in] key Key value to use * \param[in] value Value to assign to the given key */ - void Put(const Key &key, const Value &value) noexcept + template + WrappedValue Put(const Key &key, T &&value) noexcept { operation_guard lock{safe_op}; auto elem_it = FindElem(key); @@ -88,12 +89,12 @@ class fixed_sized_cache Erase(disp_candidate_key); } - Insert(key, value); + return Insert(key, std::forward(value)); } else { // update previous value - Update(key, value); + return Update(key, std::forward(value)); } } @@ -204,10 +205,13 @@ class fixed_sized_cache } protected: - void Insert(const Key &key, const Value &value) + template + WrappedValue Insert(const Key &key, T &&value) { cache_policy.Insert(key); - cache_items_map.emplace(std::make_pair(key, std::make_shared(value))); + auto ptr = std::make_shared(std::forward(value)); + cache_items_map.emplace(std::make_pair(key, ptr)); + return ptr; } void Erase(const_iterator elem) @@ -224,10 +228,13 @@ class fixed_sized_cache Erase(elem_it); } - void Update(const Key &key, const Value &value) + template + WrappedValue Update(const Key &key, T &&value) { cache_policy.Touch(key); - cache_items_map[key] = std::make_shared(value); + auto ptr = std::make_shared(std::forward(value));; + cache_items_map[key] = ptr; + return ptr; } const_iterator FindElem(const Key &key) const diff --git a/test/fifo_cache_tests.cpp b/test/fifo_cache_tests.cpp index 656c40e..94af06f 100644 --- a/test/fifo_cache_tests.cpp +++ b/test/fifo_cache_tests.cpp @@ -20,8 +20,11 @@ TEST(FIFOCache, Simple_Test) { fifo_cache_t fc(2); - fc.Put(1, 10); - fc.Put(2, 20); + auto val1 = fc.Put(1, 10); + auto val2 = fc.Put(2, 20); + + EXPECT_EQ(*val1, 10); + EXPECT_EQ(*val2, 20); EXPECT_EQ(fc.Size(), 2); EXPECT_EQ(*fc.Get(1), 10); diff --git a/test/lfu_cache_tests.cpp b/test/lfu_cache_tests.cpp index cb1f065..2600790 100644 --- a/test/lfu_cache_tests.cpp +++ b/test/lfu_cache_tests.cpp @@ -23,10 +23,13 @@ TEST(LFUCache, Simple_Test) constexpr size_t THIRD_FREQ = 8; lfu_cache_t cache(3); - cache.Put("A", 1); - cache.Put("B", 2); + auto val1 = cache.Put("A", 1); + auto val2 = cache.Put("B", 2); cache.Put("C", 3); + EXPECT_EQ(*val1, 1); + EXPECT_EQ(*val2, 2); + for (size_t i = 0; i < FIRST_FREQ; ++i) { EXPECT_EQ(*cache.Get("B"), 2); diff --git a/test/lru_cache_tests.cpp b/test/lru_cache_tests.cpp index 1cd674a..7742700 100644 --- a/test/lru_cache_tests.cpp +++ b/test/lru_cache_tests.cpp @@ -19,7 +19,8 @@ TEST(CacheTest, SimplePut) { lru_cache_t cache(1); - cache.Put("test", 666); + auto val = cache.Put("test", 666); + EXPECT_EQ(*val, 666); EXPECT_EQ(*cache.Get("test"), 666); } diff --git a/test/nopolicy_cache_tests.cpp b/test/nopolicy_cache_tests.cpp index b802034..75f417f 100644 --- a/test/nopolicy_cache_tests.cpp +++ b/test/nopolicy_cache_tests.cpp @@ -15,6 +15,20 @@ TEST(NoPolicyCache, Add_one_element) ASSERT_EQ(*cache.Get("Hello"), 1); } +TEST(NoPolicyCache, Put_returns_shared_ptr) +{ + constexpr std::size_t cache_size = 1; + caches::fixed_sized_cache cache(cache_size); + + auto val = cache.Put("Hello", 1); + ASSERT_EQ(*val, 1); + ASSERT_EQ(*cache.Get("Hello"), 1); + + // update existing element + auto val_updated = cache.Put("Hello", 2); + ASSERT_EQ(*val_updated, 2); +} + TEST(NoPolicyCache, Add_delete_add_one_element) { constexpr std::size_t cache_size = 1;