From a22207f3b35d75d70bb80b03caa6a1b14aa67f09 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Wed, 28 Dec 2022 18:21:32 -0500 Subject: [PATCH] Added delete entry. --- src/morethantext/mod.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index 76fb103..b7a4a2e 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -1,7 +1,7 @@ pub mod error; use async_std::{ - fs::{create_dir, read, write}, + fs::{create_dir, read, remove_file, write}, path::Path, sync::{Arc, Mutex}, task::{sleep, spawn}, @@ -198,6 +198,15 @@ impl MoreThanText { cache.insert(id.to_string(), data); 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)] @@ -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 fn remove_older() { let mtt = MTT::new().await;