Skip to content

Commit

Permalink
issue-25: Add Cacheable
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyErmilov committed Sep 16, 2021
1 parent 99f061b commit 81aab31
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 61 deletions.
2 changes: 1 addition & 1 deletion examples/examples/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use axum::response::Html;
async fn main() {
let app = Router::new()
.route(
"/",
"/:user_id/index.html",
get(handler.layer(CacheLayer::new())),
);

Expand Down
2 changes: 2 additions & 0 deletions hitbox-axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
51 changes: 51 additions & 0 deletions hitbox-axum/src/cacheable.rs
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:"))
}
}
61 changes: 2 additions & 59 deletions hitbox-axum/src/layer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use tower_service::Service;
use std::task::{Poll, Context};
use tower_layer::Layer;
use std::fmt;
use hitbox::{Cacheable, CacheError};
use serde::Serialize;
use crate::service::CacheService;

#[derive(Debug, Clone)]
pub struct CacheLayer {}
Expand All @@ -18,59 +14,6 @@ impl<S> Layer<S> for CacheLayer {
type Service = CacheService<S>;

fn layer(&self, service: S) -> Self::Service {
CacheService { service }
CacheService::new(service)
}
}

#[derive(Clone)]
pub struct CacheService<S> {
service: S,
}

impl<S, Request> Service<Request> for CacheService<S>
where
S: Service<Request>,
// Request: fmt::Debug + Serialize,
Request: fmt::Debug,
{
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, request: Request) -> Self::Future {
// let wrapper = Wrapper(request);
// let cache_key = wrapper.cache_key();
// println!("request = {:?}", request);
// println!("cache_key = {:?}", cache_key);
// self.service.call(wrapper.into_inner())
// let a = serde_json::to_string(&request);
// dbg!(&a);
self.service.call(request)
}
}

// #[derive(Serialize, Clone)]
// pub struct Wrapper<Request>(Request);
//
// impl<Request> Wrapper<Request> {
// pub fn into_inner(self) -> Request {
// self.0
// }
// }
//
// impl<Request> Cacheable for Wrapper<Request>
// where
// Request: Serialize
// {
// fn cache_key(&self) -> Result<String, CacheError> {
// Ok(serde_json::to_string(&self).unwrap())
// }
//
// fn cache_key_prefix(&self) -> String {
// todo!()
// }
// }
5 changes: 4 additions & 1 deletion hitbox-axum/src/lib.rs
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;
38 changes: 38 additions & 0 deletions hitbox-axum/src/service.rs
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())
}
}

0 comments on commit 81aab31

Please sign in to comment.