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]
name = "manifold"
version = "3.1.0"
version = "3.1.1"
authors = ["Lucy Bladen <admin@lbladen.uk>"]
edition = "2021"

View File

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