Skip to content

[Question/Request] Async functions #27

@hffmnn

Description

@hffmnn

Thanks a lot for this crate, works really great.

I would have one use case, that currently isn't supported: When trying to apply the macro to an async function, it doesn't compile (because the returned Future is not clonable).

A typical use case would be caching of network requests.

Currently, I have a workaround for this, but it feels rather complicated or indirect:

  1. Use a blocking client in the memoized function to do the network request
  2. call the memoized function from an async function via tokios task::spawn_blocking as described here
use memoize::memoize;
use std::{collections::HashMap, time::Duration};
use tokio::task;

#[memoize(Capacity: 5, TimeToLive: Duration::from_secs(3600))]
fn hello(query: String) -> Result<String, ()> {
    let response = reqwest::blocking::get(format!("https://some-endpoint.com/?{query}"));
    // do things with the response...
    // return 
    Ok(something_i_need.into())
}

async fn cached(query: String) -> Result<String, ()> {
    let res = task::spawn_blocking(move || hello(query)).await;
    if let Ok(res) = res {
        return res;
    }
    Err(())
}

Because I have no idea about macros I wonder if it would be possible to add a async_memoize version that calls the async (memoized) function and how much work that would be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions