-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99f061b
commit 81aab31
Showing
6 changed files
with
98 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,5 @@ tower-layer = "0.3.1" | |
tower-service = "0.3.1" | ||
serde = "1" | ||
serde_json = "1" | ||
axum = "0.2" | ||
sha2 = "0.9" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use axum::http::Request; | ||
use hitbox::cache::Cacheable; | ||
use hitbox::CacheError; | ||
|
||
pub struct Wrapper<T> { | ||
pub request: Request<T>, | ||
} | ||
|
||
impl<T> Wrapper<T> { | ||
pub fn into_inner(self) -> Request<T> { | ||
self.request | ||
} | ||
} | ||
|
||
impl<T> Cacheable for Wrapper<T> { | ||
fn cache_key(&self) -> Result<String, CacheError> { | ||
let path = self.request.uri().path(); | ||
let method = self.request.method(); | ||
let query = self.request.uri().query().unwrap_or_default(); | ||
Ok(format!("{}:{}:{}", path, method, query)) | ||
} | ||
|
||
fn cache_key_prefix(&self) -> String { | ||
todo!() | ||
} | ||
|
||
fn cache_ttl(&self) -> u32 { | ||
todo!() | ||
} | ||
|
||
fn cache_stale_ttl(&self) -> u32 { | ||
todo!() | ||
} | ||
|
||
fn cache_version(&self) -> u32 { | ||
todo!() | ||
} | ||
} | ||
|
||
mod tests { | ||
use axum::http::Request; | ||
use crate::Wrapper; | ||
use hitbox::cache::Cacheable; | ||
|
||
#[test] | ||
fn test_cache_key() { | ||
let request = Request::new(String::from("Hello world")); | ||
let wrapper = Wrapper { request }; | ||
assert_eq!(wrapper.cache_key().unwrap(), String::from("/:GET:")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
mod layer; | ||
mod service; | ||
mod cacheable; | ||
|
||
pub use layer::CacheLayer; | ||
pub use layer::CacheLayer; | ||
pub use cacheable::Wrapper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use tower_service::Service; | ||
use axum::http::{Request, Response}; | ||
use std::task::{Poll, Context}; | ||
use crate::Wrapper; | ||
use hitbox::{Cacheable, CacheError}; | ||
|
||
#[derive(Clone)] | ||
pub struct CacheService<S> { | ||
service: S, | ||
} | ||
|
||
impl<S> CacheService<S> { | ||
pub fn new(service: S) -> CacheService<S> { | ||
CacheService { service } | ||
} | ||
} | ||
|
||
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for CacheService<S> | ||
where | ||
S: Service<Request<ReqBody>, Response = Response<ResBody>> + Clone + Send + 'static, | ||
S::Future: Send + 'static, | ||
ReqBody: Send + 'static, | ||
ResBody: Send + 'static, | ||
{ | ||
type Response = S::Response; | ||
type Error = S::Error; | ||
type Future = S::Future; | ||
|
||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.service.poll_ready(cx) | ||
} | ||
|
||
fn call(&mut self, mut request: Request<ReqBody>) -> Self::Future { | ||
let wrapper = Wrapper { request }; | ||
// let cache_key = wrapper.cache_key(); | ||
self.service.call(wrapper.into_inner()) | ||
} | ||
} |