Only cache a service if we're asked to

This commit is contained in:
Xyon 2023-09-01 15:16:01 +01:00
parent 88c5a1b1a0
commit cdf823a94d
Signed by: xyon
GPG Key ID: DD18155D6B18078D
2 changed files with 56 additions and 34 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "manifold" name = "manifold"
version = "3.1.0" version = "3.1.1"
authors = ["Lucy Bladen <admin@lbladen.uk>"] authors = ["Lucy Bladen <admin@lbladen.uk>"]
edition = "2021" edition = "2021"

View File

@ -25,7 +25,7 @@ pub struct FuelTank {
cache_mode: TankMode, cache_mode: TankMode,
} }
#[derive(Copy, Clone, Debug, Serialize, Deserialize)] #[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
pub enum TankMode { pub enum TankMode {
Cache, Cache,
NoCache, NoCache,
@ -37,48 +37,70 @@ impl FuelTank {
U: Serialize, U: Serialize,
U: DeserializeOwned, U: Pump<T> U: DeserializeOwned, U: Pump<T>
{ {
debug!("Filling tank!"); let path = PathBuf::new()
let mut cache = { .join(home_dir().ok_or(ManifoldError::from("No home dir"))?)
let mut path = PathBuf::new(); .join(".local")
path.push(home_dir().ok_or(ManifoldError::from("No home dir"))?); .join(format!("manifold-{}-cache-{}.json", self.cache_name.clone().unwrap(), CACHE_VERSION));
path.push(".local");
path.push(format!("manifold-{}-cache-{}.json", self.cache_name.clone().unwrap(), CACHE_VERSION));
debug!("Cache path: {:?}", &path);
fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(path)
.map_err(|e| {
error!("Error creating cache file: {}", e);
ManifoldError::from("CACHE ERROR")
})?
};
debug!("Cache: {:?}", &cache); let mut fuel = match self.cache_mode {
TankMode::Cache => {
// Read cache only if we have one
debug!("Filling tank!");
let mut cache = {
debug!("Cache path: {:?}", &path);
fs::OpenOptions::new()
.read(true)
.create(true)
.open(&path)
.map_err(|e| {
error!("Error creating cache file: {}", e);
ManifoldError::from("CACHE ERROR")
})?
};
let json: Result<U, serde_json::Error> = serde_json::from_reader(&mut cache); debug!("Cache: {:?}", &cache);
debug!("Json: {:?}", &json);
let mut fuel = match json { let json: Result<U, serde_json::Error> = serde_json::from_reader(&mut cache);
Ok(contents) => { debug!("Json: {:?}", &json);
match contents.len() { match json {
0 => self.emit().await?, Ok(contents) => {
_ => contents, match contents.len() {
0 => self.emit().await?,
_ => contents,
}
}
_ => self.emit().await?,
} }
} },
_ => self.emit().await?, TankMode::NoCache => {
self.emit().await?
},
}; };
debug!("Got tips: {:?}", &fuel); debug!("Got tips: {:?}", &fuel);
let single = fuel.pump().ok_or(ManifoldError::from("CACHE ERROR"))?; let single = fuel.pump().ok_or(ManifoldError::from("CACHE ERROR"))?;
debug!("Got single tip: {:?}", &single); debug!("Got single tip: {:?}", &single);
cache if self.cache_mode == TankMode::Cache {
.set_len(0) // Write cache only if we have one
.and_then(|_| cache.seek(SeekFrom::Start(0)))?; let mut cache = {
debug!("Cache path: {:?}", &path);
fs::OpenOptions::new()
.write(true)
.create(true)
.open(&path)
.map_err(|e| {
error!("Error creating cache file: {}", e);
ManifoldError::from("CACHE ERROR")
})?
};
cache
.set_len(0)
.and_then(|_| cache.seek(SeekFrom::Start(0)))?;
debug!("Writing cache"); debug!("Writing cache");
serde_json::to_writer(cache, &fuel)?; serde_json::to_writer(cache, &fuel)?;
}
return Ok(single) return Ok(single)
} }