diff options
Diffstat (limited to 'src/cache.rs')
-rw-r--r-- | src/cache.rs | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/src/cache.rs b/src/cache.rs index c0e160f..67c7a01 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,10 +1,13 @@ use anyhow::Result; -use redb::{Database, TableDefinition}; +use redb::{Database, Durability, TableDefinition}; use std::{ - fs::{File, create_dir_all}, - io::{Read, Write}, + fs::create_dir_all, path::{Path, PathBuf}, }; +use tokio::{ + fs::File, + io::{AsyncReadExt, AsyncWriteExt}, +}; pub enum Cache { Directory(PathBuf), @@ -31,13 +34,13 @@ impl Cache { Ok(Self::Redb(db)) } - pub fn get(&self, path: &str) -> Result<Option<Vec<u8>>> { + pub async fn get(&self, path: &str) -> Result<Option<Vec<u8>>> { match self { Cache::Directory(cachedir) => { let cachepath = cachedir.join(path); if cachepath.exists() { let mut buf = Vec::new(); - File::open(cachepath)?.read_to_end(&mut buf)?; + File::open(cachepath).await?.read_to_end(&mut buf).await?; Ok(Some(buf.into())) } else { Ok(None) @@ -55,15 +58,17 @@ impl Cache { } } } - pub fn insert(&self, path: &str, data: &[u8]) -> Result<()> { + pub async fn insert(&self, path: &str, data: &[u8]) -> Result<()> { match self { Cache::Directory(cachedir) => { let cachepath = cachedir.join(path); - File::create(cachepath)?.write_all(data)?; + File::create(cachepath).await?.write_all(data).await?; Ok(()) } Cache::Redb(database) => { - let txn = database.begin_write()?; + let mut txn = database.begin_write()?; + txn.set_durability(Durability::Eventual); + txn.set_quick_repair(true); let mut table = txn.open_table(T_DOWNLOAD)?; table.insert(path, data)?; drop(table); |