Skip to content

Commit

Permalink
feat(storage): add clear method
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemeowx2 committed Nov 21, 2023
1 parent c097531 commit 20c550a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ pub trait Storage: Send + Sync {
async fn set(&self, key: &str, value: &str) -> Result<()>;
async fn remove(&self, key: &str) -> Result<()>;
async fn keys(&self) -> Result<Vec<StorageKey>>;
async fn clear(&self) -> Result<()>;
}
16 changes: 16 additions & 0 deletions src/storage/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl Storage for FileStorage {
let index = self.get_index().await?;
Ok(index.index.get(key).map(|item| item.updated_at))
}

async fn get(&self, key: &str) -> Result<Option<StorageItem>> {
let _ = self.lock.read().await;
let index = self.get_index().await?;
Expand Down Expand Up @@ -165,6 +166,7 @@ impl Storage for FileStorage {

Ok(())
}

async fn keys(&self) -> Result<Vec<StorageKey>> {
let _ = self.lock.read().await;
let index = self.get_index().await?;
Expand Down Expand Up @@ -195,4 +197,18 @@ impl Storage for FileStorage {

Ok(())
}

async fn clear(&self) -> Result<()> {
let _ = self.lock.write().await;
let index = self.get_index().await?;
for item in index.index.values() {
remove_file(self.storage_dir.join(&item.content)).await.ok();
}
self.set_index(Index {
version: 0,
index: HashMap::new(),
})
.await?;
Ok(())
}
}
8 changes: 8 additions & 0 deletions src/storage/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ impl Storage for MemoryCache {
async fn get_updated_at(&self, key: &str) -> Result<Option<SystemTime>> {
Ok(self.cache.read().get(key).map(|item| item.updated_at))
}

async fn get(&self, key: &str) -> Result<Option<StorageItem>> {
Ok(self.cache.read().get(key).cloned())
}

async fn set(&self, key: &str, value: &str) -> Result<()> {
self.cache.write().insert(
key.to_string(),
Expand All @@ -36,6 +38,7 @@ impl Storage for MemoryCache {
);
Ok(())
}

async fn keys(&self) -> Result<Vec<StorageKey>> {
Ok(self
.cache
Expand All @@ -52,4 +55,9 @@ impl Storage for MemoryCache {
self.cache.write().remove(key);
Ok(())
}

async fn clear(&self) -> Result<()> {
self.cache.write().clear();
Ok(())
}
}

0 comments on commit 20c550a

Please sign in to comment.