pub async fn read<K: AsRef<[u8]>>(&self, key: K) -> Result<Bytes> {
        use tokio::io::AsyncReadExt;
        let k = key.as_ref();

        // look in the memory cache to see if it's there and return if it is
        if let Some(val) = self.mem.get(k) {
            return self.track_access_for(k).map(|_| val);
        }

        let file = {
            let path = self.path_from_key(k);
            afs::OpenOptions::new()
                .read(true)
                .open(&path)
                .await
                .map_err(|e| match e.kind() {
                    io::ErrorKind::NotFound => ForcepError::NotFound,
                    _ => ForcepError::Io(e),
                })?
        };

        // create a new buffer based on the estimated size of the file
        let size_guess = file.metadata().await.map(|x| x.len()).unwrap_or(0);
        let mut buf = Vec::with_capacity(size_guess as usize);

        // read the entire file to the buffer
        tokio::io::BufReader::with_capacity(self.opts.rbuff_sz, file)
            .read_to_end(&mut buf)
            .await
            .map_err(ForcepError::Io)?;

        self.track_access_for(k)?;
        let bytes = Bytes::from(buf);
        self.mem.put(k, Bytes::clone(&bytes));
        Ok(bytes)
    }
/// Reads an entry from the database, returning a vector of bytes that represent the entry.
///
/// # Not Found
///
/// If the entry is not found, then it will return
/// `Err(`[`Error::NotFound`](ForcepError::NotFound)`)`.
///
/// # Metadata
///
/// This function will *not* perform a metadata read or write **unless** the `track_access`
/// build option is set. If the option is set, then it will perform a blocking read/write to
/// write new values to track the last access time and the total hits.
///
/// # 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();
///
/// let value = cache.read(b"MY_KEY").await.unwrap();
/// assert_eq!(value.as_ref(), b"Hello World");
/// # }
/// ```
Reads an entry from the database, returning a vector of bytes that represent the entry.
Reads an entry from the database returning a vector of bytes that represent the entry .
pub async fn read<K: AsRef<[u8]>>(&self, key: K) -> Result<Bytes> {
        use tokio::io::AsyncReadExt;
        let k = key.as_ref();
        // look in the memory cache to see if it's there and return if it is
        if let Some(val) = self.mem.get(k) {
            return self.track_access_for(k).map(|_| val);
        }
        let file = {
            let path = self.path_from_key(k);
            afs::OpenOptions::new()
                .read(true)
                .open(&path)
                .await
                .map_err(|e| match e.kind() {
                    io::ErrorKind::NotFound => ForcepError::NotFound,
                    _ => ForcepError::Io(e),
                })?
        };
        // create a new buffer based on the estimated size of the file
        let size_guess = file.metadata().await.map(|x| x.len()).unwrap_or(0);
        let mut buf = Vec::with_capacity(size_guess as usize);
        // read the entire file to the buffer
        tokio::io::BufReader::with_capacity(self.opts.rbuff_sz, file)
            .read_to_end(&mut buf)
            .await
            .map_err(ForcepError::Io)?;
        self.track_access_for(k)?;
        let bytes = Bytes::from(buf);
        self.mem.put(k, Bytes::clone(&bytes));
        Ok(bytes)
    }
pub async fn read < K : AsRef < [ u8 ] > > ( & self , key : K ) -> Result < Bytes > { use tokio :: io :: AsyncReadExt ; let k = key . as_ref ( ) ; // look in the memory cache to see if it's there and return if it is if let Some ( val ) = self . mem . get ( k ) { return self . track_access_for ( k ) . map ( | _ | val ) ; } let file = { let path = self . path_from_key ( k ) ; afs :: OpenOptions :: new ( ) . read ( true ) . open ( & path ) . await . map_err ( | e | match e . kind ( ) { io :: ErrorKind :: NotFound => ForcepError :: NotFound , _ => ForcepError :: Io ( e ) , } ) ? } ; // create a new buffer based on the estimated size of the file let size_guess = file . metadata ( ) . await . map ( | x | x . len ( ) ) . unwrap_or ( 0 ) ; let mut buf = Vec :: with_capacity ( size_guess as usize ) ; // read the entire file to the buffer tokio :: io :: BufReader :: with_capacity ( self . opts . rbuff_sz , file ) . read_to_end ( & mut buf ) . await . map_err ( ForcepError :: Io ) ? ; self . track_access_for ( k ) ? ; let bytes = Bytes :: from ( buf ) ; self . mem . put ( k , Bytes :: clone ( & bytes ) ) ; Ok ( bytes ) }
Reads an entry from the database, returning a vector of bytes that represent the entry.
Reads an entry from the database returning a vector of bytes that represent the entry .
&self
key
K
&self
key
K
not found
If the entry is not found, then it will return
`Err(`[`Error::NotFound`](ForcepError::NotFound)`)`.
If the entry is not found then it will return ` Err ( ` [ ` Error :: NotFound ` ] ( ForcepError :: NotFound ) ` ) ` .
metadata
This function will *not* perform a metadata read or write **unless** the `track_access`
build option is set. If the option is set, then it will perform a blocking read/write to
write new values to track the last access time and the total hits.
This function will * not * perform a metadata read or write ** unless ** the ` track_access ` build option is set . If the option is set then it will perform a blocking read / write to write new values to track the last access time and the total hits .
examples
None
#[tokio::main(flavor = "current_thread")]
None
async fn main() {
use forceps::Cache.

let cache = Cache::new("./cache")
.build()
.await
.unwrap().
use forceps :: Cache . let cache = Cache :: new ( " . / cache " ) . build () . await . unwrap () .
cache.write(b"my_key", b"hello world").await.unwrap();
let value = cache.read(b"MY_KEY").await.unwrap();
assert_eq!(value.as_ref(), b"Hello World").
let value = cache . read ( b " MY_KEY " ) . await . unwrap () ; assert_eq! ( value . as_ref () b " Hello World " ) .
}
None