Added delete entry.

This commit is contained in:
Jeff Baskin 2022-12-28 18:21:32 -05:00
parent 6229671212
commit a22207f3b3

View File

@ -1,7 +1,7 @@
pub mod error; pub mod error;
use async_std::{ use async_std::{
fs::{create_dir, read, write}, fs::{create_dir, read, remove_file, write},
path::Path, path::Path,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
task::{sleep, spawn}, task::{sleep, spawn},
@ -198,6 +198,15 @@ impl MoreThanText {
cache.insert(id.to_string(), data); cache.insert(id.to_string(), data);
Ok(()) Ok(())
} }
async fn delete_entry(&self, id: &str) {
let mut cache = self.cache.lock().await;
cache.remove(id);
match remove_file(Path::new(&self.filename(id))).await {
Ok(_) => (),
Err(_) => (),
}
}
} }
#[cfg(test)] #[cfg(test)]
@ -429,6 +438,27 @@ mod cache {
} }
} }
#[async_std::test]
async fn remove_entry() {
let mtt = MTT::new().await;
let id = mtt
.db
.add_entry(CacheType::Raw("delete".to_string()))
.await
.unwrap();
mtt.db.delete_entry(&id).await;
match mtt.db.get_entry(&id).await {
Ok(_) => assert!(false, "Entry should be removed from cache."),
Err(_) => (),
};
}
#[async_std::test]
async fn remove_missing_entry() {
let mtt = MTT::new().await;
mtt.db.delete_entry("missing").await;
}
#[async_std::test] #[async_std::test]
async fn remove_older() { async fn remove_older() {
let mtt = MTT::new().await; let mtt = MTT::new().await;