Add TimedCache for controlled cache expiration #17557
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
This PR introduces the
TimedCache
class, which adds functionality for managing a cache where the entire cache expires after a set interval, and to be manually cleared.This
TimedCache
is intended to be used in scenario where the check caches data for multiple check runs but wants to periodically clear the cache and re-build after sometime. It's mainly to avoid a cache to grow indefinitely.Motivation
The
TimedCache
will be first used by postgres query metrics where we need to cache thepg_stat_statements
as baseline and keep adding new queryid to the cache as new normalized queries appear. However, to avoid the baseline cache grow overtime, we will check if the cache is expired and rebuild the baseline periodically.Additional Notes
The
TimedCache
is different from cachetoolsTTLCache
. InTTLCache
, every key has a TTL and expires independently.TimedCache
has TTL on the entire cache but does not automatically clear the cache. This is to avoid losing cached values in the middle of the process. Developer should check if cache is expired at the beginning of the process and rebuild whenever needed.Review checklist (to be filled by reviewers)
qa/skip-qa
label if the PR doesn't need to be tested during QA.backport/<branch-name>
label to the PR and it will automatically open a backport PR once this one is merged