Skip to content

Commit

Permalink
Truncate cache if max_messages is set to a lower value at runtime (s…
Browse files Browse the repository at this point in the history
  • Loading branch information
ivinjabraham authored and GnomedDev committed Nov 15, 2024
1 parent 03c59f8 commit 9023e72
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,18 @@ impl Cache {
///
/// By default, no messages will be cached.
pub fn set_max_messages(&self, max: usize) {
// Check to see if cache has to be truncated
if max < self.settings.read().max_messages {
for mut entry in self.messages.iter_mut() {
let message_queue = entry.value_mut();
let queue_len = message_queue.len();

if queue_len > max {
message_queue.drain(..queue_len - max);
}
}
}

self.settings.write().max_messages = max;
}

Expand Down

0 comments on commit 9023e72

Please sign in to comment.