Skip to content

Commit 73eb1ea

Browse files
committed
added alternate moka manager mokadeser
1 parent e12ce75 commit 73eb1ea

File tree

7 files changed

+411
-1
lines changed

7 files changed

+411
-1
lines changed
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: http-cache-mokadeser
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
workflow_dispatch:
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
concurrency:
13+
group: ${{ github.ref }}-http-cache-mokadeser
14+
cancel-in-progress: true
15+
16+
defaults:
17+
run:
18+
working-directory: ./http-cache-mokadeser
19+
20+
jobs:
21+
fmt:
22+
name: Check formatting
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v4
26+
- uses: dtolnay/rust-toolchain@stable
27+
with:
28+
components: "rustfmt"
29+
- run: cargo fmt -- --check
30+
31+
test:
32+
name: Test stable on ${{ matrix.os }}
33+
needs: [fmt]
34+
strategy:
35+
matrix:
36+
os:
37+
- ubuntu-latest
38+
- windows-latest
39+
- macOS-latest
40+
runs-on: ${{ matrix.os }}
41+
steps:
42+
- uses: actions/checkout@v4
43+
- uses: dtolnay/rust-toolchain@stable
44+
- run: |
45+
cargo test --all-targets --all-features
46+
47+
clippy:
48+
name: Check clippy
49+
needs: [fmt, test]
50+
runs-on: ubuntu-latest
51+
steps:
52+
- uses: actions/checkout@v4
53+
- uses: dtolnay/rust-toolchain@stable
54+
with:
55+
components: "clippy"
56+
- run: |
57+
cargo clippy --lib --tests --all-targets --all-features -- -D warnings
58+
59+
docs:
60+
name: Build docs
61+
needs: [fmt, test]
62+
runs-on: ubuntu-latest
63+
steps:
64+
- uses: actions/checkout@v4
65+
- uses: dtolnay/rust-toolchain@nightly
66+
env:
67+
RUSTFLAGS: --cfg docsrs
68+
RUSTDOCFLAGS: --cfg docsrs -Dwarnings
69+
- run: cargo doc --no-deps --document-private-items

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ members = [
55
"http-cache-reqwest",
66
"http-cache-surf",
77
"http-cache-quickcache",
8-
"http-cache-darkbird"
8+
"http-cache-darkbird",
9+
"http-cache-mokadeser"
910
]

http-cache-mokadeser/CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
# Changelog
3+
4+
## [0.1.0] - 2023-09-26
5+
6+
### Added
7+
8+
- Initial release

http-cache-mokadeser/Cargo.toml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[package]
2+
name = "http-cache-mokadeser"
3+
version = "0.1.0"
4+
description = "http-cache manager implementation for moka stored deserialized"
5+
authors = ["Christian Haynes <[email protected]>", "Kat Marchán <[email protected]>"]
6+
repository = "https://github.com/06chaynes/http-cache"
7+
homepage = "https://http-cache.rs"
8+
license = "MIT OR Apache-2.0"
9+
readme = "README.md"
10+
keywords = ["cache", "http", "manager", "quick-cache"]
11+
categories = [
12+
"caching",
13+
"web-programming::http-client"
14+
]
15+
edition = "2021"
16+
rust-version = "1.66.1"
17+
18+
[dependencies]
19+
async-trait = "0.1.72"
20+
http-cache-semantics = "1.0.1"
21+
url = { version = "2.4.0", features = ["serde"] }
22+
moka = { version = "0.12.0", features = ["future"]}
23+
24+
[dependencies.http-cache]
25+
path = "../http-cache"
26+
version = "0.15.0"
27+
default-features = false
28+
features = ["bincode"]
29+
30+
[dev-dependencies]
31+
http = "0.2.9"
32+
reqwest = { version = "0.11.18", default-features = false }
33+
reqwest-middleware = "0.2.2"
34+
tokio = { version = "1.29.1", features = [ "macros", "rt", "rt-multi-thread" ] }
35+
wiremock = "0.5.19"
36+
37+
[dev-dependencies.http-cache-reqwest]
38+
path = "../http-cache-reqwest"
39+
40+
[package.metadata.docs.rs]
41+
all-features = true
42+
rustdoc-args = ["--cfg", "docsrs"]

http-cache-mokadeser/README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# http-cache-mokadeser
2+
3+
[![CI](https://img.shields.io/github/actions/workflow/status/06chaynes/http-cache/http-cache-mokadeser.yml?label=CI&style=for-the-badge)](https://github.com/06chaynes/http-cache/actions/workflows/http-cache-mokadeser.yml)
4+
[![Crates.io](https://img.shields.io/crates/v/http-cache-mokadeser?style=for-the-badge)](https://crates.io/crates/http-cache-mokadeser)
5+
[![Docs.rs](https://img.shields.io/docsrs/http-cache-mokadeser?style=for-the-badge)](https://docs.rs/http-cache-mokadeser)
6+
[![Codecov](https://img.shields.io/codecov/c/github/06chaynes/http-cache?style=for-the-badge)](https://app.codecov.io/gh/06chaynes/http-cache)
7+
![Crates.io](https://img.shields.io/crates/l/http-cache-mokadeser?style=for-the-badge)
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+
An http-cache manager implementation for [moka](https://github.com/moka-rs/moka).
12+
13+
## Minimum Supported Rust Version (MSRV)
14+
15+
1.66.1
16+
17+
## Install
18+
19+
With [cargo add](https://github.com/killercup/cargo-edit#Installation) installed :
20+
21+
```sh
22+
cargo add http-cache-mokadeser
23+
```
24+
25+
## Example
26+
27+
```rust
28+
use http_cache_mokadeser::MokaManager;
29+
use http_cache_surf::{Cache, CacheMode, HttpCache, HttpCacheOptions};
30+
31+
#[async_std::main]
32+
async fn main() -> surf::Result<()> {
33+
let req = surf::get("https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching");
34+
surf::client()
35+
.with(Cache(HttpCache {
36+
mode: CacheMode::Default,
37+
manager: MokaManager::default(),
38+
options: HttpCacheOptions::default(),
39+
}))
40+
.send(req)
41+
.await?;
42+
Ok(())
43+
}
44+
```
45+
46+
## Documentation
47+
48+
- [API Docs](https://docs.rs/http-cache-mokadeser)
49+
50+
## License
51+
52+
Licensed under either of
53+
54+
- Apache License, Version 2.0
55+
([LICENSE-APACHE](https://github.com/06chaynes/http-cache/blob/main/LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
56+
- MIT license
57+
([LICENSE-MIT](https://github.com/06chaynes/http-cache/blob/main/LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
58+
59+
at your option.
60+
61+
## Contribution
62+
63+
Unless you explicitly state otherwise, any contribution intentionally submitted
64+
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
65+
dual licensed as above, without any additional terms or conditions.

http-cache-mokadeser/src/lib.rs

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use http_cache::{CacheManager, HttpResponse, Result};
2+
3+
use std::{fmt, sync::Arc};
4+
5+
use http_cache_semantics::CachePolicy;
6+
use moka::future::Cache;
7+
8+
#[derive(Clone)]
9+
pub struct MokaManager {
10+
pub cache: Arc<Cache<String, Store>>,
11+
}
12+
13+
impl fmt::Debug for MokaManager {
14+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15+
f.debug_struct("MokaManager").finish_non_exhaustive()
16+
}
17+
}
18+
19+
impl Default for MokaManager {
20+
fn default() -> Self {
21+
Self::new(Cache::new(42))
22+
}
23+
}
24+
25+
#[derive(Clone, Debug)]
26+
pub struct Store {
27+
response: HttpResponse,
28+
policy: CachePolicy,
29+
}
30+
31+
impl MokaManager {
32+
pub fn new(cache: Cache<String, Store>) -> Self {
33+
Self { cache: Arc::new(cache) }
34+
}
35+
pub async fn clear(&self) -> Result<()> {
36+
self.cache.invalidate_all();
37+
self.cache.run_pending_tasks().await;
38+
Ok(())
39+
}
40+
}
41+
42+
#[async_trait::async_trait]
43+
impl CacheManager for MokaManager {
44+
async fn get(
45+
&self,
46+
cache_key: &str,
47+
) -> Result<Option<(HttpResponse, CachePolicy)>> {
48+
let store: Store = match self.cache.get(cache_key).await {
49+
Some(d) => d,
50+
None => return Ok(None),
51+
};
52+
Ok(Some((store.response, store.policy)))
53+
}
54+
55+
async fn put(
56+
&self,
57+
cache_key: String,
58+
response: HttpResponse,
59+
policy: CachePolicy,
60+
) -> Result<HttpResponse> {
61+
let store = Store { response: response.clone(), policy };
62+
self.cache.insert(cache_key, store).await;
63+
self.cache.run_pending_tasks().await;
64+
Ok(response)
65+
}
66+
67+
async fn delete(&self, cache_key: &str) -> Result<()> {
68+
self.cache.invalidate(cache_key).await;
69+
self.cache.run_pending_tasks().await;
70+
Ok(())
71+
}
72+
}
73+
74+
#[cfg(test)]
75+
mod test;

0 commit comments

Comments
 (0)