docs: add basic examples

This commit is contained in:
Kat Marchán 2019-10-15 13:08:02 -04:00
parent 180a031b52
commit 4259a39fdf
No known key found for this signature in database
GPG Key ID: AEB529C08A3C7E9E
2 changed files with 37 additions and 1 deletions

View File

@ -4,6 +4,18 @@ A Rust port of [`cacache` for Node.js](https://npm.im/cacache).
A high-performance, concurrent, content-addressable disk cache. A high-performance, concurrent, content-addressable disk cache.
## Example
```rust
use cacache;
use tempfile;
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().to_owned();
cacache::put::data(&dir, "key", b"my-data").unwrap();
let data = cacache::get::read(&dir, "key").unwrap();
assert_eq!(data, b"my-data");
```
## Install ## Install
Using [`cargo-edit`](https://crates.io/crates/cargo-edit) Using [`cargo-edit`](https://crates.io/crates/cargo-edit)

View File

@ -1,7 +1,31 @@
//! cacache is a Rust library for managing local key and content address //! cacache is a Rust library for managing local key and content address
//! caches. It's really fast, really good at concurrency, and it will never //! caches. It's really fast, really good at concurrency, and it will never
//! give you corrupted data, even if cache files get corrupted or manipulated. //! give you corrupted data, even if cache files get corrupted or manipulated.
//!
//! ## Examples
//! ```
//! use cacache;
//! # use tempfile;
//! # let tmp = tempfile::tempdir().unwrap();
//! let dir = tmp.path().to_owned();
//! cacache::put::data(&dir, "key", b"my-data").unwrap();
//! let data = cacache::get::read(&dir, "key").unwrap();
//! assert_eq!(data, b"my-data");
//! ```
//!
//! You can also use the equivalent async APIs using async/await!
//! ```
//! # use tempfile;
//! use cacache;
//! # use async_std::task;
//! # let tmp = tempfile::tempdir().unwrap();
//! let dir = tmp.path().to_owned();
//! # task::block_on(async {
//! cacache::async_put::data(&dir, "key", b"my-async-data").await.unwrap();
//! let data = cacache::async_get::read(&dir, "key").await.unwrap();
//! assert_eq!(data, b"my-async-data");
//! # })
//! ```
#![warn(missing_docs, missing_doc_code_examples)] #![warn(missing_docs, missing_doc_code_examples)]
pub use serde_json::Value; pub use serde_json::Value;