Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a total cached message cap, alongside the per-channel cap #2848

Draft
wants to merge 1 commit into
base: current
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use std::collections::{HashSet, VecDeque};
use std::hash::Hash;
use std::sync::atomic::AtomicUsize;
#[cfg(feature = "temp_cache")]
use std::sync::Arc;
#[cfg(feature = "temp_cache")]
Expand Down Expand Up @@ -216,6 +217,10 @@ pub struct Cache {
/// cache. When a maximum number of messages are in a channel's cache, we can pop the front and
/// remove that ID from the cache.
pub(crate) message_queue: DashMap<ChannelId, VecDeque<MessageId>, BuildHasher>,
/// Total number of messages stored across all channels.
///
/// Must not exceed [`Settings::max_messages_total`]
pub(crate) total_messages: Option<AtomicUsize>,

// Miscellanous fixed-size data
// ---
Expand Down Expand Up @@ -282,6 +287,7 @@ impl Cache {

messages: DashMap::default(),
message_queue: DashMap::default(),
total_messages: settings.max_messages_total.map(|_| AtomicUsize::new(0)),

shard_data: RwLock::new(CachedShardData {
total: 1,
Expand Down
8 changes: 8 additions & 0 deletions src/cache/settings.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::num::NonZeroUsize;
use std::time::Duration;

/// Settings for the cache.
Expand All @@ -20,6 +21,12 @@ pub struct Settings {
///
/// Defaults to 0.
pub max_messages: usize,
/// The maximum number of messages to store in the message cache, in total.
///
/// See [`Self::max_messages`] to disable the cache entirely.
///
/// Defaults to None, meaning no limit set.
pub max_messages_total: Option<NonZeroUsize>,
/// How long temporarily-cached data should be stored before being thrown out.
///
/// Defaults to one hour.
Expand All @@ -42,6 +49,7 @@ impl Default for Settings {
fn default() -> Self {
Self {
max_messages: 0,
max_messages_total: None,
time_to_live: Duration::from_secs(60 * 60),
cache_guilds: true,
cache_channels: true,
Expand Down
Loading