【问题标题】:How to cache or memoize data in actix-web route?如何在 actix-web 路由中缓存或记忆数据?
【发布时间】:2021-01-06 11:24:37
【问题描述】:

我有一个我正在实现的 API,我有一个需要调用的昂贵函数。我想记住这个函数或使用键值缓存来查找以前的结果。我也会考虑使用Mutex 或类似的结构,但我想避免使用 Redis 或 Memcached 等外部​​结构。根据我的阅读,Mutex 的缓存性能很差。如何使用 HashMap 之类的键值存储或在 actix_web 异步路由中存储函数?现在,我正在尝试一个简单的HashMap,但我得到了这个错误:can't borrow data in an Arc as mutable

async fn index(
    kv: web::Data<HashMap<&str, i64>>,
) -> Result<HttpResponse> {
    dbg!(kv);   
    kv.insert("four", 4);
    Ok(HttpResponse::Ok().json(kv)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let tuples: Vec<(&str, i64)> = vec![("one", 1), ("two", 2), ("three", 3)];
    let mut m = tuples.into_iter().collect::<HashMap<&str, i64>>();
    let mut kv = web::Data::new(m);
    // move is necessary to give closure below ownership of counter1
    HttpServer::new(move || {
            App::new()
                .app_data(kv.clone())
                .route("/", web::get().to(index))
        })
        .bind("127.0.0.1:8080")?
        .run()
        .await
}

【问题讨论】:

    标签: rust rust-actix actix-web


    【解决方案1】:

    看来你可以使用cached crate。

    将逻辑移至另一个函数。类似以下代码的工作:

    use actix_web::{web, App, HttpServer, HttpResponse};
    use cached::proc_macro::cached;
    
    async fn index() -> HttpResponse {
        dbg!("in index");
        let number = get_number(2);
        HttpResponse::Ok().body(number.to_string())
    }
    
    #[cached]
    fn get_number(n: i32) -> i32 {
        dbg!("in get_number");
        n * 2
    }
    
    #[actix_web::main]
    async fn main() -> std::io::Result<()> {
        HttpServer::new(move || {
                App::new()
                    .route("/", web::get().to(index))
            })
            .bind("127.0.0.1:8080")?
            .run()
            .await
    }
    

    【讨论】:

    • 谢谢,是的,我看到了,但无法正常工作。
    猜你喜欢
    • 1970-01-01
    • 2021-11-24
    • 2014-04-11
    • 2023-02-24
    • 2020-06-01
    • 2016-06-27
    • 1970-01-01
    • 2016-05-19
    • 2020-06-04
    相关资源
    最近更新 更多