Skip to content

Commit 49d1c43

Browse files
committed
Skip internal keys' lookup in LRU
for the DefaultCache. Relates-to: DATASDK-71 Signed-off-by: sopov <[email protected]>
1 parent 478cb38 commit 49d1c43

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ OperationOutcomeEmpty DefaultCacheImpl::GetFromDiskCache(
881881

882882
if (expiry > 0 || protected_keys_.IsProtected(key)) {
883883
// Entry didn't expire yet, we can still use it
884-
if (!PromoteKeyLru(key)) {
884+
if (!IsInternalKey(key) && !PromoteKeyLru(key)) {
885885
// If not found in LRU or not protected no need to look in disk cache
886886
// either.
887887
OLP_SDK_LOG_DEBUG_F(kLogTag,

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,79 @@ TEST_F(DefaultCacheImplTest, LruCacheEvictionWithProtected) {
990990
}
991991
}
992992

993+
// SCOPED_TRACE("Expiry protected cache");
994+
// const std::string key1{"somekey1"};
995+
// const std::string key2{"somekey2"};
996+
// const std::string data_string{"this is key's data"};
997+
// constexpr auto expiry = 2;
998+
// std::vector<unsigned char> binary_data = {1, 2, 3};
999+
// const auto data_ptr =
1000+
// std::make_shared<std::vector<unsigned char>>(binary_data);
1001+
//
1002+
// cache::CacheSettings settings1;
1003+
// settings1.disk_path_mutable = cache_path_;
1004+
// DefaultCacheImplHelper cache1(settings1);
1005+
// cache1.Open();
1006+
// cache1.Clear();
1007+
//
1008+
// cache1.Put(key1, data_ptr, expiry);
1009+
// cache1.Put(key2, data_string, [=]() { return data_string; }, expiry);
1010+
// cache1.Close();
1011+
//
1012+
// cache::CacheSettings settings2;
1013+
// settings2.disk_path_protected = cache_path_;
1014+
// DefaultCacheImplHelper cache2(settings2);
1015+
// cache2.Open();
1016+
//
1017+
// const auto value = cache2.Get(key1);
1018+
// const auto value2 =
1019+
// cache2.Get(key2, [](const std::string& value) { return value; });
1020+
//
1021+
// EXPECT_FALSE(value.get() == nullptr);
1022+
// EXPECT_EQ(value2.type(), typeid(std::string));
1023+
// auto str = boost::any_cast<std::string>(value2);
1024+
// EXPECT_EQ(str, data_string);
1025+
//
1026+
// std::this_thread::sleep_for(std::chrono::seconds(3));
1027+
//
1028+
// EXPECT_TRUE(cache2.Get(key1).get() == nullptr);
1029+
// EXPECT_TRUE(
1030+
// cache2.Get(key2, [](const std::string& value) { return value;
1031+
// }).empty());
1032+
// cache2.Close();
1033+
1034+
TEST_F(DefaultCacheImplTest, InternalKeysBypassLru) {
1035+
{
1036+
SCOPED_TRACE("Protect and release keys, which suppose to be evicted");
1037+
1038+
const auto internal_key{"internal::protected::protected_data"};
1039+
cache::CacheSettings settings;
1040+
settings.disk_path_mutable = cache_path_;
1041+
settings.max_disk_storage = 2u * 1024u * 1024u;
1042+
{
1043+
DefaultCacheImplHelper cache(settings);
1044+
ASSERT_EQ(olp::cache::DefaultCache::Success, cache.Open());
1045+
cache.Clear();
1046+
const std::string key{"somekey"};
1047+
std::string data_string{"this is key's data"};
1048+
1049+
constexpr auto expiry = 2;
1050+
EXPECT_TRUE(cache.Put(key, data_string,
1051+
[data_string]() { return data_string; }, expiry));
1052+
ASSERT_TRUE(cache.Protect({key}));
1053+
}
1054+
1055+
settings.disk_path_mutable = cache_path_;
1056+
settings.disk_path_protected.reset();
1057+
1058+
DefaultCacheImplHelper cache(settings);
1059+
ASSERT_TRUE(cache.Open());
1060+
// no keys was evicted
1061+
EXPECT_TRUE(cache.Get(internal_key));
1062+
cache.Clear();
1063+
}
1064+
}
1065+
9931066
TEST_F(DefaultCacheImplTest, ReadOnlyPartitionForProtectedCache) {
9941067
SCOPED_TRACE("Read only partition protected cache");
9951068
const std::string key{"somekey"};

0 commit comments

Comments
 (0)