-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache retrieved flakes on disk (#101)
- Update dioxus-std PR - nix_rs: Avoid serde untagged on exposed types
- Loading branch information
Showing
6 changed files
with
143 additions
and
56 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//! A database of [Flake] intended to be cached in dioxus [Signal] and persisted to disk. | ||
//! | ||
//! This is purposefully dumb right now, but we might revisit this in future based on actual performance. | ||
use dioxus::prelude::Scope; | ||
use serde::{Deserialize, Serialize}; | ||
use std::{collections::HashMap, time::SystemTime}; | ||
|
||
use dioxus_signals::Signal; | ||
use dioxus_std::storage::new_storage; | ||
use dioxus_std::storage::LocalStorage; | ||
|
||
use crate::app::state::FlakeUrl; | ||
use nix_rs::flake::Flake; | ||
|
||
/// A database of [Flake] intended to be cached in dioxus [Signal] and persisted to disk. | ||
/// | ||
/// Contains the "last fetched" time and the [Flake] itself. | ||
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)] | ||
pub struct FlakeCache(HashMap<FlakeUrl, Option<(SystemTime, Flake)>>); | ||
|
||
impl FlakeCache { | ||
/// Create a new [Signal] for [FlakeCache] from [LocalStorage]. | ||
pub fn new_signal(cx: Scope) -> Signal<FlakeCache> { | ||
new_storage::<LocalStorage, _>(cx, "flake_cache".to_string(), || { | ||
tracing::warn!("📦 No flake cache found"); | ||
let init = FlakeUrl::suggestions() | ||
.into_iter() | ||
.map(|url| (url, None)) | ||
.collect(); | ||
FlakeCache(init) | ||
}) | ||
} | ||
|
||
/// Look up a [Flake] by [FlakeUrl] in the cache. | ||
pub fn get(&self, k: &FlakeUrl) -> Option<Flake> { | ||
let (t, flake) = self.0.get(k).and_then(|v| v.as_ref().cloned())?; | ||
tracing::info!("Cache hit for {} (updated: {:?})", k, t); | ||
Some(flake) | ||
} | ||
|
||
/// Update the cache with a new [Flake]. | ||
pub fn update(&mut self, k: FlakeUrl, flake: Flake) { | ||
tracing::info!("Caching flake [{}]", &k); | ||
self.0.insert(k, Some((SystemTime::now(), flake))); | ||
} | ||
|
||
/// Recently updated flakes, along with any unavailable flakes in cache. | ||
pub fn recent_flakes(&self) -> Vec<FlakeUrl> { | ||
let mut pairs: Vec<_> = self | ||
.0 | ||
.iter() | ||
.filter_map(|(k, v)| v.as_ref().map(|(t, _)| (k, t))) | ||
.collect(); | ||
|
||
// Sort by the timestamp in descending order. | ||
pairs.sort_unstable_by(|a, b| b.1.cmp(a.1)); | ||
|
||
pairs.into_iter().map(|(k, _)| k.clone()).collect() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters