-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
180 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use color_eyre::eyre::Result; | ||
use serde::{Deserialize, Serialize}; | ||
use std::{ | ||
collections::HashMap, | ||
fs, | ||
io::{self, Write}, | ||
path::PathBuf, | ||
time::SystemTime, | ||
}; | ||
|
||
static ONE_DAY_IN_SECONDS: u64 = 24 * 60 * 60; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct Entry { | ||
timestamp: SystemTime, | ||
data: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Default)] | ||
pub struct Cache { | ||
path: PathBuf, | ||
entries: HashMap<String, Entry>, | ||
refresh: bool, | ||
} | ||
|
||
impl Cache { | ||
pub fn new(path: PathBuf, refresh: bool) -> Self { | ||
let entries = match fs::read_to_string(&path) { | ||
Ok(contents) => serde_json::from_str(&contents).unwrap_or_default(), | ||
Err(_) => { | ||
fs::create_dir_all(path.parent().unwrap()).unwrap(); | ||
HashMap::new() | ||
} | ||
}; | ||
|
||
Cache { | ||
path, | ||
entries, | ||
refresh, | ||
} | ||
} | ||
|
||
pub fn get(&self, key: &str) -> Option<&String> { | ||
if self.refresh { | ||
return None; | ||
} | ||
match self.entries.get(key) { | ||
Some(entry) => { | ||
let diff = SystemTime::now() | ||
.duration_since(entry.timestamp) | ||
.unwrap() | ||
.as_secs(); | ||
if diff < ONE_DAY_IN_SECONDS { | ||
Some(&entry.data) | ||
} else { | ||
None | ||
} | ||
} | ||
None => None, | ||
} | ||
} | ||
|
||
pub fn get_or<F>(&mut self, key: &str, fetch: F) -> Result<String> | ||
where | ||
F: FnOnce() -> Result<String>, | ||
{ | ||
if let Some(data) = self.get(key) { | ||
return Ok(data.clone()); | ||
} | ||
let value = fetch()?; | ||
self.save(key, value.clone())?; | ||
Ok(value) | ||
} | ||
|
||
pub fn save(&mut self, key: &str, value: String) -> Result<String> { | ||
self.entries.insert( | ||
key.to_string(), | ||
Entry { | ||
timestamp: SystemTime::now(), | ||
data: value.clone(), | ||
}, | ||
); | ||
self.save_to_file()?; | ||
Ok(value) | ||
} | ||
|
||
fn save_to_file(&self) -> io::Result<()> { | ||
let mut file = fs::File::create(&self.path)?; | ||
file.write_all(serde_json::to_string(&self.entries)?.as_bytes()) | ||
} | ||
} |
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
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