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

Improve TTL caching and handling #230

Merged
merged 3 commits into from
Aug 3, 2023
Merged
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
22 changes: 15 additions & 7 deletions spotify_player/src/client/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ pub async fn start_player_event_watchers(
// An event is categorized as "high-frequency" when
// - we want to handle it "immediately" to prevent users from experiencing a noticable delay
let refresh_duration = std::time::Duration::from_millis(200); // frequency = 5Hz

// This timer is used to avoid making multiple `GetContext` requests in a short period of time.
let mut last_request_timer = std::time::Instant::now();

loop {
tokio::time::sleep(refresh_duration).await;

Expand Down Expand Up @@ -175,17 +179,21 @@ pub async fn start_player_event_watchers(
*page_state = None;
}
}
}

// request new context's data if not found in memory
if let Some(id) = id {
if !state.data.read().caches.context.contains_key(&id.uri()) {
client_pub
.send(ClientRequest::GetContext(id.clone()))
.unwrap_or_default();
}
// request new context's data if not found in memory
if let Some(id) = id {
if !state.data.read().caches.context.contains_key(&id.uri())
&& last_request_timer.elapsed() >= std::time::Duration::from_secs(1)
{
last_request_timer = std::time::Instant::now();
client_pub
.send(ClientRequest::GetContext(id.clone()))
.unwrap_or_default();
}
}
}

#[cfg(feature = "lyric-finder")]
PageState::Lyric {
track,
Expand Down
4 changes: 2 additions & 2 deletions spotify_player/src/state/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use super::model::*;

pub type DataReadGuard<'a> = parking_lot::RwLockReadGuard<'a, AppData>;

// cache duration, which is default to be 1h
// cache duration, which is default to be 3h
pub static CACHE_DURATION: Lazy<std::time::Duration> =
Lazy::new(|| std::time::Duration::from_secs(60 * 60));
Lazy::new(|| std::time::Duration::from_secs(60 * 60 * 3));

#[derive(Default)]
/// the application's data
Expand Down
Loading