|
| 1 | +# http-cache-ureq |
| 2 | + |
| 3 | +[](https://github.com/06chaynes/http-cache/actions/workflows/http-cache-ureq.yml) |
| 4 | +[](https://crates.io/crates/http-cache-ureq) |
| 5 | +[](https://docs.rs/http-cache-ureq) |
| 6 | +[](https://app.codecov.io/gh/06chaynes/http-cache) |
| 7 | + |
| 8 | + |
| 9 | +<img class="logo" align="right" src="https://raw.githubusercontent.com/06chaynes/http-cache/main/.assets/images/http-cache_logo_bluegreen.svg" height="150px" alt="the http-cache logo"> |
| 10 | + |
| 11 | +A caching middleware that follows HTTP caching rules, |
| 12 | +thanks to [http-cache-semantics](https://github.com/kornelski/rusty-http-cache-semantics). |
| 13 | +By default, it uses [cacache](https://github.com/zkat/cacache-rs) as the backend cache manager. |
| 14 | +Provides a simple caching wrapper around [ureq](https://github.com/algesten/ureq). |
| 15 | + |
| 16 | +## Minimum Supported Rust Version (MSRV) |
| 17 | + |
| 18 | +1.82.0 |
| 19 | + |
| 20 | +## Install |
| 21 | + |
| 22 | +With [cargo add](https://github.com/killercup/cargo-edit#Installation) installed : |
| 23 | + |
| 24 | +```sh |
| 25 | +cargo add http-cache-ureq |
| 26 | +``` |
| 27 | + |
| 28 | +## Example |
| 29 | + |
| 30 | +```rust |
| 31 | +use http_cache_ureq::{CACacheManager, CachedAgent}; |
| 32 | + |
| 33 | +#[smol_macros::main] |
| 34 | +async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { |
| 35 | + let client = CachedAgent::builder() |
| 36 | + .cache_manager(CACacheManager::default()) |
| 37 | + .build()?; |
| 38 | + |
| 39 | + let response = client |
| 40 | + .get("https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching") |
| 41 | + .call() |
| 42 | + .await?; |
| 43 | + |
| 44 | + println!("Status: {}", response.status()); |
| 45 | + Ok(()) |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +## Basic Usage |
| 50 | + |
| 51 | +The `CachedAgent` wraps ureq's functionality while providing transparent HTTP caching: |
| 52 | + |
| 53 | +```rust |
| 54 | +use http_cache_ureq::{CACacheManager, CachedAgent}; |
| 55 | + |
| 56 | +// Create a cached agent with default settings |
| 57 | +let client = CachedAgent::builder() |
| 58 | + .cache_manager(CACacheManager::default()) |
| 59 | + .build()?; |
| 60 | + |
| 61 | +// Use it just like a regular ureq agent |
| 62 | +let response = client.get("https://httpbin.org/json").call().await?; |
| 63 | +``` |
| 64 | + |
| 65 | +## Features |
| 66 | + |
| 67 | +The following features are available. By default `manager-cacache` is enabled. |
| 68 | + |
| 69 | +- `manager-cacache` (default): enable [cacache](https://github.com/zkat/cacache-rs), a high-performance disk cache, backend manager. |
| 70 | +- `manager-moka` (disabled): enable [moka](https://github.com/moka-rs/moka), a high-performance in-memory cache, backend manager. |
| 71 | +- `json` (disabled): enable JSON support via ureq's json feature. |
| 72 | +- `rate-limiting` (disabled): enable rate limiting functionality. |
| 73 | + |
| 74 | +## Documentation |
| 75 | + |
| 76 | +- [API Docs](https://docs.rs/http-cache-ureq) |
| 77 | + |
| 78 | +## License |
| 79 | + |
| 80 | +Licensed under either of |
| 81 | + |
| 82 | +- Apache License, Version 2.0 |
| 83 | + ([LICENSE-APACHE](https://github.com/06chaynes/http-cache/blob/main/LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>) |
| 84 | +- MIT license |
| 85 | + ([LICENSE-MIT](https://github.com/06chaynes/http-cache/blob/main/LICENSE-MIT) or <http://opensource.org/licenses/MIT>) |
| 86 | + |
| 87 | +at your option. |
| 88 | + |
| 89 | +## Contribution |
| 90 | + |
| 91 | +Unless you explicitly state otherwise, any contribution intentionally submitted |
| 92 | +for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 93 | +dual licensed as above, without any additional terms or conditions. |
0 commit comments