Only cache a service if we're asked to
This commit is contained in:
parent
88c5a1b1a0
commit
cdf823a94d
|
|
@ -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"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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,18 +37,21 @@ impl FuelTank {
|
||||||
U: Serialize,
|
U: Serialize,
|
||||||
U: DeserializeOwned, U: Pump<T>
|
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!");
|
debug!("Filling tank!");
|
||||||
let mut cache = {
|
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);
|
debug!("Cache path: {:?}", &path);
|
||||||
fs::OpenOptions::new()
|
fs::OpenOptions::new()
|
||||||
.read(true)
|
.read(true)
|
||||||
.write(true)
|
|
||||||
.create(true)
|
.create(true)
|
||||||
.open(path)
|
.open(&path)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
error!("Error creating cache file: {}", e);
|
error!("Error creating cache file: {}", e);
|
||||||
ManifoldError::from("CACHE ERROR")
|
ManifoldError::from("CACHE ERROR")
|
||||||
|
|
@ -59,7 +62,7 @@ impl FuelTank {
|
||||||
|
|
||||||
let json: Result<U, serde_json::Error> = serde_json::from_reader(&mut cache);
|
let json: Result<U, serde_json::Error> = serde_json::from_reader(&mut cache);
|
||||||
debug!("Json: {:?}", &json);
|
debug!("Json: {:?}", &json);
|
||||||
let mut fuel = match json {
|
match json {
|
||||||
Ok(contents) => {
|
Ok(contents) => {
|
||||||
match contents.len() {
|
match contents.len() {
|
||||||
0 => self.emit().await?,
|
0 => self.emit().await?,
|
||||||
|
|
@ -67,18 +70,37 @@ impl FuelTank {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => 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);
|
||||||
|
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
|
cache
|
||||||
.set_len(0)
|
.set_len(0)
|
||||||
.and_then(|_| cache.seek(SeekFrom::Start(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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue