Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please add caching example #627

Open
bangbaew opened this issue Jul 20, 2023 · 4 comments
Open

Please add caching example #627

bangbaew opened this issue Jul 20, 2023 · 4 comments
Assignees
Labels
new-example Request for a new example

Comments

@bangbaew
Copy link

bangbaew commented Jul 20, 2023

I want to wrap an in-memory cache middleware for all GET routes that returns 200, the cache key should be based on the full request URL and can be customized, If I send "Cache-Control": "no-cache" in the request headers, Actix should process the request and overwrite the new response in the cache key, and I want the ability to delete the cache key in case of successful data update, so I can get the new data again in the next request.

Thanks.

@robjtede robjtede added the new-example Request for a new example label Jul 20, 2023
@bangbaew
Copy link
Author

I've tried this:

use actix_http::header::CONTENT_TYPE;
use actix_session::SessionExt;
use actix_web::{
    body::{self, MessageBody},
    dev::{ServiceRequest, ServiceResponse},
    http::{
        header::{HeaderValue, CACHE_CONTROL},
        Method, StatusCode,
    },
    Error, HttpResponse,
};

use actix_web_lab::middleware::Next;

pub async fn cache_middleware(
    req: ServiceRequest,
    next: Next<impl MessageBody>,
) -> Result<ServiceResponse<impl MessageBody>, Error> {
    let key = format!("{}{}", req.path(), req.query_string());
    let session = req.get_session();

    if let Ok(cached_response) = session.get::<String>(&key) {
        //println!("{:?}", cached_response);
        if cached_response.is_some() {
            let res = HttpResponse::new(StatusCode::OK).set_body(cached_response.unwrap());
            let mut res = ServiceResponse::new(req.request().to_owned(), res);

            res.headers_mut()
                .append(CONTENT_TYPE, HeaderValue::from_static("application/json"));
            res.headers_mut()
                .append(CACHE_CONTROL, HeaderValue::from_static("max-age=86400"));

            return Ok(res);
        }
    }
    // Call the next service
    let res = next.call(req).await?;

    // deconstruct response into parts
    let (req, res) = res.into_parts();
    let (res, body) = res.into_parts();

    // Convert body to Bytes
    let body = body::to_bytes(body).await.ok().unwrap();
    // Use bytes directly for caching instead of converting to a String
    let res_body_enc = std::str::from_utf8(&body).unwrap();

    let res = res.set_body(res_body_enc.to_owned());
    let mut res = ServiceResponse::new(req.to_owned(), res);

    println!("{}, {}", req.method(), res.status());
    if req.method() == Method::GET && StatusCode::is_success(&res.status()) {
        println!("caching");
        res.headers_mut()
            .append(CACHE_CONTROL, HeaderValue::from_static("max-age=86400"));
        if let Err(e) = session.insert(key, res_body_enc) {
            println!("cache insert error: {}", e);
        }
    } else {
        println!("not caching");
    }
    Ok(res)
}

it's working fine but i'm not sure if it can be optimized to improve efficiency and performance

@robjtede
Copy link
Member

PR welcome where comments on specifics can be made.

@bangbaew
Copy link
Author

bangbaew commented Jul 23, 2023

PR welcome where comments on specifics can be made.

Ok, but I’ll have to fix the problem when redis is not running, it will always return status 500 when session.get() or session.set() is called, the error catch block is not working.

and I’ll have to implement header Cache-Control: no-cache directive in case I want to skip cache.

@bangbaew
Copy link
Author

PR welcome where comments on specifics can be made.

Ready for review #630

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-example Request for a new example
Projects
None yet
Development

No branches or pull requests

2 participants