Skip to content

Commit 5568cb1

Browse files
Fix the DefaultCache::Put behavior (#602)
Fix the DefaultCache::Put behavior when the value does not fit in-memory cache When the value does not fit the in-memory cache, it still cached on disk. Resolves: OLPSUP-9206 Signed-off-by: Mykhailo Kuchma <[email protected]>
1 parent fec9f7f commit 5568cb1

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

olp-cpp-sdk-core/src/cache/DefaultCache.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,11 @@ bool DefaultCache::Put(const std::string& key, const boost::any& value,
134134

135135
auto encodedItem = encoder();
136136
if (memory_cache_) {
137-
if (!memory_cache_->Put(key, value, expiry, encodedItem.size()))
138-
return false;
137+
if (!memory_cache_->Put(key, value, expiry, encodedItem.size())) {
138+
OLP_SDK_LOG_WARNING_F(kLogTag,
139+
"Failed to store value in memory cache %s, size %d",
140+
key.c_str(), static_cast<int>(encodedItem.size()));
141+
}
139142
}
140143

141144
if (mutable_cache_) {
@@ -162,7 +165,9 @@ bool DefaultCache::Put(const std::string& key,
162165

163166
if (memory_cache_) {
164167
if (!memory_cache_->Put(key, value, expiry, value->size())) {
165-
return false;
168+
OLP_SDK_LOG_WARNING_F(kLogTag,
169+
"Failed to store value in memory cache %s, size %d",
170+
key.c_str(), static_cast<int>(value->size()));
166171
}
167172
}
168173

olp-cpp-sdk-core/tests/cache/DefaultCacheTest.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,30 @@ TEST(DefaultCacheTest, AlreadyInUsePath) {
343343
olp::cache::DefaultCache cache2(settings);
344344
ASSERT_EQ(olp::cache::DefaultCache::OpenDiskPathFailure, cache2.Open());
345345
}
346+
347+
TEST(DefaultCacheTest, ValueGreaterThanMemCacheLimit) {
348+
using namespace olp::cache;
349+
350+
const std::string content_key = "test_key";
351+
const std::string content =
352+
"a very long string that does not fit into the in memory cache";
353+
354+
olp::cache::CacheSettings settings;
355+
settings.max_memory_cache_size = 10;
356+
settings.disk_path_mutable = olp::utils::Dir::TempDirectory() + "/mutable";
357+
358+
DefaultCache cache(settings);
359+
EXPECT_EQ(cache.Open(), DefaultCache::StorageOpenResult::Success);
360+
361+
auto input_buffer = std::make_shared<std::vector<unsigned char>>(
362+
std::begin(content), std::end(content));
363+
EXPECT_TRUE(cache.Put(content_key, input_buffer, 15));
364+
365+
auto output_buffer = cache.Get(content_key);
366+
ASSERT_TRUE(output_buffer != nullptr);
367+
368+
EXPECT_TRUE(std::equal(output_buffer->begin(), output_buffer->end(),
369+
content.begin()));
370+
371+
cache.Close();
372+
}

0 commit comments

Comments
 (0)