Skip to content

Commit dbd0c51

Browse files
committed
refactor(predicate): improve Config struct and add TTL setter method
Signed-off-by: doxxx93 <[email protected]>
1 parent 036cca8 commit dbd0c51

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

kube-runtime/src/utils/predicate.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,20 @@ where
120120
pub struct Config {
121121
/// Time-to-live for cache entries
122122
///
123-
/// Entries older than this duration will be evicted from the cache.
123+
/// Entries not seen for at least this long will be evicted from the cache.
124124
/// Default is 1 hour.
125-
pub ttl: Duration,
125+
ttl: Duration,
126+
}
127+
128+
impl Config {
129+
/// Set the time-to-live for cache entries
130+
///
131+
/// Entries not seen for at least this long will be evicted from the cache.
132+
#[must_use]
133+
pub fn ttl(mut self, ttl: Duration) -> Self {
134+
self.ttl = ttl;
135+
self
136+
}
126137
}
127138

128139
impl Default for Config {
@@ -199,7 +210,7 @@ where
199210
// Check if the predicate value changed or entry doesn't exist
200211
let changed = me.cache.get(&key).map(|entry| entry.hash) != Some(val);
201212

202-
// Update cache with new hash and timestamp
213+
// Upsert the cache entry with new hash and timestamp
203214
me.cache.insert(key, CacheEntry {
204215
hash: val,
205216
last_seen: now,
@@ -402,9 +413,7 @@ pub(crate) mod tests {
402413
};
403414

404415
// Use a very short TTL for testing
405-
let config = Config {
406-
ttl: Duration::from_millis(50),
407-
};
416+
let config = Config::default().ttl(Duration::from_millis(50));
408417

409418
// Use a channel so we can send items with delays
410419
let (mut tx, rx) = mpsc::unbounded();

0 commit comments

Comments
 (0)