Skip to content

Commit a571788

Browse files
committed
feat(cache): use bytes::Bytes for reads
1 parent a565ee9 commit a571788

File tree

4 files changed

+9
-6
lines changed

4 files changed

+9
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ md5 = "0.7.0"
1919
rand = {version = "0.8.3", features = ["small_rng", "getrandom"], default-features = false}
2020
bson = {version = "1.2.2", features = ["u2i"]}
2121
serde = {version = "1.0.125", features = ["derive"]}
22-
tokio = {version = "1.5.0", features = ["rt", "fs", "io-util"]}
22+
tokio = {version = "1.5.0", features = ["fs", "io-util"]}
23+
bytes = "1.0.1"
2324

2425
[dev-dependencies]
2526
tokio = {version = "1.5.0", features = ["full"]}

src/cache.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{ForcepError, MetaDb, Metadata, Result};
2+
use bytes::Bytes;
23
use std::io;
34
use std::path;
45
use tokio::fs as afs;
@@ -132,10 +133,10 @@ impl Cache {
132133
/// # cache.write(b"MY_KEY", b"Hello World").await.unwrap();
133134
///
134135
/// let value = cache.read(b"MY_KEY").await.unwrap();
135-
/// assert_eq!(&value, b"Hello World");
136+
/// assert_eq!(value.as_ref(), b"Hello World");
136137
/// # }
137138
/// ```
138-
pub async fn read<K: AsRef<[u8]>>(&self, key: K) -> Result<Vec<u8>> {
139+
pub async fn read<K: AsRef<[u8]>>(&self, key: K) -> Result<Bytes> {
139140
use tokio::io::AsyncReadExt;
140141

141142
let file = {
@@ -159,7 +160,7 @@ impl Cache {
159160
.read_to_end(&mut buf)
160161
.await
161162
.map_err(ForcepError::Io)?;
162-
Ok(buf)
163+
Ok(Bytes::from(buf))
163164
}
164165

165166
/// Writes an entry with the specified key to the cache database. This will replace the
@@ -350,7 +351,7 @@ mod test {
350351

351352
cache.write(&b"CACHE_KEY", &b"Hello World").await.unwrap();
352353
let data = cache.read(&b"CACHE_KEY").await.unwrap();
353-
assert_eq!(&data, &b"Hello World");
354+
assert_eq!(data.as_ref(), b"Hello World");
354355
cache.remove(&b"CACHE_KEY").await.unwrap();
355356
}
356357

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
//!
4343
//! cache.write(b"MY_KEY", b"Hello World").await.unwrap();
4444
//! let data = cache.read(b"MY_KEY").await.unwrap();
45-
//! assert_eq!(&data, b"Hello World");
45+
//! assert_eq!(data.as_ref(), b"Hello World");
4646
//! # }
4747
//! ```
4848

0 commit comments

Comments
 (0)