Skip to content

Skip internal keys' lookup in LRU for the DefaultCache. #1615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ OperationOutcomeEmpty DefaultCacheImpl::GetFromDiskCache(

if (expiry > 0 || protected_keys_.IsProtected(key)) {
// Entry didn't expire yet, we can still use it
if (!PromoteKeyLru(key)) {
if (!IsInternalKey(key) && !PromoteKeyLru(key)) {
// If not found in LRU or not protected no need to look in disk cache
// either.
OLP_SDK_LOG_DEBUG_F(kLogTag,
Expand Down
38 changes: 38 additions & 0 deletions olp-cpp-sdk-core/tests/cache/DefaultCacheImplTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,44 @@ TEST_F(DefaultCacheImplTest, LruCacheEvictionWithProtected) {
}
}

TEST_F(DefaultCacheImplTest, InternalKeysBypassLru) {
{
SCOPED_TRACE("Protect and release keys, which suppose to be evicted");

const auto internal_key{"internal::protected::protected_data"};
const auto data_string{"this is key's data"};
cache::CacheSettings settings;
settings.disk_path_mutable = cache_path_;
{
settings.eviction_policy = cache::EvictionPolicy::kNone;
DefaultCacheImplHelper cache(settings);
ASSERT_EQ(olp::cache::DefaultCache::Success, cache.Open());
cache.Clear();

constexpr auto expiry = 2;
EXPECT_TRUE(cache.Put(internal_key, data_string,
[data_string]() { return data_string; }, expiry));
}

settings.disk_path_mutable = cache_path_;
settings.disk_path_protected.reset();
settings.eviction_policy = cache::EvictionPolicy::kLeastRecentlyUsed;

DefaultCacheImplHelper cache(settings);
ASSERT_EQ(olp::cache::DefaultCache::Success, cache.Open());

auto stored_dats = cache.Get(internal_key);
ASSERT_TRUE(stored_dats);

std::string stored_string(
reinterpret_cast<const char*>(stored_dats->data()),
stored_dats->size());
EXPECT_EQ(stored_string, data_string);

cache.Clear();
}
}

TEST_F(DefaultCacheImplTest, ReadOnlyPartitionForProtectedCache) {
SCOPED_TRACE("Read only partition protected cache");
const std::string key{"somekey"};
Expand Down
Loading