diff --git a/cpp/src/parquet/encryption/two_level_cache_with_expiration.h b/cpp/src/parquet/encryption/two_level_cache_with_expiration.h index 76c2b827700..283ebd97b71 100644 --- a/cpp/src/parquet/encryption/two_level_cache_with_expiration.h +++ b/cpp/src/parquet/encryption/two_level_cache_with_expiration.h @@ -103,32 +103,24 @@ class TwoLevelCacheWithExpiration { if (external_cache_entry == cache_.end() || external_cache_entry->second.IsExpired()) { cache_.insert({access_token, internal::ExpiringCacheMapEntry( - std::shared_ptr>( - new ConcurrentMap()), + std::make_shared>(), 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(cache_cleanup_period_seconds))) { RemoveExpiredEntriesNoMutex(); - last_cache_cleanup_timestamp_ = - now + std::chrono::duration(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); diff --git a/cpp/src/parquet/encryption/two_level_cache_with_expiration_test.cc b/cpp/src/parquet/encryption/two_level_cache_with_expiration_test.cc index d8f2c625514..75c31a907c0 100644 --- a/cpp/src/parquet/encryption/two_level_cache_with_expiration_test.cc +++ b/cpp/src/parquet/encryption/two_level_cache_with_expiration_test.cc @@ -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. @@ -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) {