Only cache a service if we're asked to
This commit is contained in:
parent
88c5a1b1a0
commit
cdf823a94d
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "manifold"
|
||||
version = "3.1.0"
|
||||
version = "3.1.1"
|
||||
authors = ["Lucy Bladen <admin@lbladen.uk>"]
|
||||
edition = "2021"
|
||||
|
||||
|
|
|
|||
|
|
@ -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,18 +37,21 @@ impl FuelTank {
|
|||
U: Serialize,
|
||||
U: DeserializeOwned, U: Pump<T>
|
||||
{
|
||||
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));
|
||||
|
||||
let mut fuel = match self.cache_mode {
|
||||
TankMode::Cache => {
|
||||
// Read cache only if we have one
|
||||
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)
|
||||
.open(&path)
|
||||
.map_err(|e| {
|
||||
error!("Error creating cache file: {}", e);
|
||||
ManifoldError::from("CACHE ERROR")
|
||||
|
|
@ -59,7 +62,7 @@ impl FuelTank {
|
|||
|
||||
let json: Result<U, serde_json::Error> = serde_json::from_reader(&mut cache);
|
||||
debug!("Json: {:?}", &json);
|
||||
let mut fuel = match json {
|
||||
match json {
|
||||
Ok(contents) => {
|
||||
match contents.len() {
|
||||
0 => self.emit().await?,
|
||||
|
|
@ -67,18 +70,37 @@ impl FuelTank {
|
|||
}
|
||||
}
|
||||
_ => 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);
|
||||
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)?;
|
||||
}
|
||||
|
||||
return Ok(single)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue