Skip to content
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
14 changes: 3 additions & 11 deletions cpp/src/parquet/encryption/two_level_cache_with_expiration.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,32 +103,24 @@ class TwoLevelCacheWithExpiration {
if (external_cache_entry == cache_.end() ||
external_cache_entry->second.IsExpired()) {
cache_.insert({access_token, internal::ExpiringCacheMapEntry<V>(
std::shared_ptr<ConcurrentMap<std::string, V>>(
new ConcurrentMap<std::string, V>()),
std::make_shared<ConcurrentMap<std::string, V>>(),
cache_entry_lifetime_seconds)});
}

return cache_[access_token].cached_item();
}

void CheckCacheForExpiredTokens(double cache_cleanup_period_seconds) {
void CheckCacheForExpiredTokens(double cache_cleanup_period_seconds = 0.0) {
auto lock = mutex_.Lock();

const auto now = internal::CurrentTimePoint();
if (now > (last_cache_cleanup_timestamp_ +
std::chrono::duration<double>(cache_cleanup_period_seconds))) {
RemoveExpiredEntriesNoMutex();
last_cache_cleanup_timestamp_ =
now + std::chrono::duration<double>(cache_cleanup_period_seconds);
last_cache_cleanup_timestamp_ = now;
}
}

void RemoveExpiredEntriesFromCache() {
auto lock = mutex_.Lock();

RemoveExpiredEntriesNoMutex();
}

void Remove(const std::string& access_token) {
auto lock = mutex_.Lock();
cache_.erase(access_token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ TEST_F(TwoLevelCacheWithExpirationTest, RemoveExpiration) {
// lifetime2 will not be expired
SleepFor(0.3);
// now clear expired items from the cache
cache_.RemoveExpiredEntriesFromCache();
cache_.CheckCacheForExpiredTokens();

// lifetime1 (with 2 items) is expired and has been removed from the cache.
// Now the cache create a new object which has no item.
Expand Down Expand Up @@ -114,6 +114,14 @@ TEST_F(TwoLevelCacheWithExpirationTest, CleanupPeriodOk) {
// lifetime2 is not expired and still contains 2 items.
auto lifetime2 = cache_.GetOrCreateInternalCache("lifetime2", 3);
ASSERT_EQ(lifetime2->size(), 2);

// The further process is added to test whether the timestamp is set correctly.
// CheckCacheForExpiredTokens() should be called at least twice to verify the
// correctness.
SleepFor(0.3);
cache_.CheckCacheForExpiredTokens(0.2);
lifetime2 = cache_.GetOrCreateInternalCache("lifetime2", 0.5);
ASSERT_EQ(lifetime2->size(), 0);
}

TEST_F(TwoLevelCacheWithExpirationTest, RemoveByToken) {
Expand Down
Loading