pub async fn write<K: AsRef<[u8]>, V: AsRef<[u8]>>(
        &self,
        key: K,
        value: V,
    ) -> Result<Metadata> {
        use tokio::io::AsyncWriteExt;
        let key = key.as_ref();
        let value = value.as_ref();

        let (tmp, tmp_path) = tempfile(&self.opts.path).await?;
        // write all data to a temporary file
        {
            let mut writer = tokio::io::BufWriter::with_capacity(self.opts.wbuff_sz, tmp);
            writer.write_all(value).await.map_err(ForcepError::Io)?;
            writer.flush().await.map_err(ForcepError::Io)?;
        }

        // move the temporary file to the final destination
        let final_path = self.path_from_key(key);
        if let Some(parent) = final_path.parent() {
            afs::create_dir_all(parent).await.map_err(ForcepError::Io)?;
        }
        afs::rename(&tmp_path, &final_path)
            .await
            .map_err(ForcepError::Io)?;

        if !self.mem.is_nil() {
            self.mem.put(key, Bytes::from(Vec::from(value)));
        }
        self.meta.insert_metadata_for(key, value)
    }
/// Writes an entry with the specified key to the cache database. This will replace the
/// previous entry if it exists, otherwise it will store a completely new one.
///
/// # Examples
///
/// ```rust
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() {
/// use forceps::Cache;
///
/// let cache = Cache::new("./cache")
///     .build()
///     .await
///     .unwrap();
///
/// cache.write(b"MY_KEY", b"Hello World").await.unwrap();
/// # }
/// ```
Writes an entry with the specified key to the cache database. This will replace the
previous entry if it exists, otherwise it will store a completely new one.
Writes an entry with the specified key to the cache database . This will replace the previous entry if it exists otherwise it will store a completely new one .
pub async fn write<K: AsRef<[u8]>, V: AsRef<[u8]>>(
        &self,
        key: K,
        value: V,
    ) -> Result<Metadata> {
        use tokio::io::AsyncWriteExt;
        let key = key.as_ref();
        let value = value.as_ref();
        let (tmp, tmp_path) = tempfile(&self.opts.path).await?;
        // write all data to a temporary file
        {
            let mut writer = tokio::io::BufWriter::with_capacity(self.opts.wbuff_sz, tmp);
            writer.write_all(value).await.map_err(ForcepError::Io)?;
            writer.flush().await.map_err(ForcepError::Io)?;
        }
        // move the temporary file to the final destination
        let final_path = self.path_from_key(key);
        if let Some(parent) = final_path.parent() {
            afs::create_dir_all(parent).await.map_err(ForcepError::Io)?;
        }
        afs::rename(&tmp_path, &final_path)
            .await
            .map_err(ForcepError::Io)?;
        if !self.mem.is_nil() {
            self.mem.put(key, Bytes::from(Vec::from(value)));
        }
        self.meta.insert_metadata_for(key, value)
    }
pub async fn write < K : AsRef < [ u8 ] > , V : AsRef < [ u8 ] > > ( & self , key : K , value : V , ) -> Result < Metadata > { use tokio :: io :: AsyncWriteExt ; let key = key . as_ref ( ) ; let value = value . as_ref ( ) ; let ( tmp , tmp_path ) = tempfile ( & self . opts . path ) . await ? ; // write all data to a temporary file { let mut writer = tokio :: io :: BufWriter :: with_capacity ( self . opts . wbuff_sz , tmp ) ; writer . write_all ( value ) . await . map_err ( ForcepError :: Io ) ? ; writer . flush ( ) . await . map_err ( ForcepError :: Io ) ? ; } // move the temporary file to the final destination let final_path = self . path_from_key ( key ) ; if let Some ( parent ) = final_path . parent ( ) { afs :: create_dir_all ( parent ) . await . map_err ( ForcepError :: Io ) ? ; } afs :: rename ( & tmp_path , & final_path ) . await . map_err ( ForcepError :: Io ) ? ; if ! self . mem . is_nil ( ) { self . mem . put ( key , Bytes :: from ( Vec :: from ( value ) ) ) ; } self . meta . insert_metadata_for ( key , value ) }
Writes an entry with the specified key to the cache database.
Writes an entry with the specified key to the cache database .
&self
key
K
value
V
&self
key
K
value
V
examples
None
#[tokio::main(flavor = "current_thread")]
None
async fn main() {
use forceps::Cache.

let cache = Cache::new("./cache")
.build()
.await
.unwrap().

cache.write(b"MY_KEY", b"Hello World").await.unwrap().
use forceps :: Cache . let cache = Cache :: new ( " . / cache " ) . build () . await . unwrap () . cache . write ( b " MY_KEY " b " Hello World " ) . await . unwrap () .
}
None