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

Async disk cache #187

Open
bschreck opened this issue Mar 7, 2024 · 4 comments
Open

Async disk cache #187

bschreck opened this issue Mar 7, 2024 · 4 comments

Comments

@bschreck
Copy link

bschreck commented Mar 7, 2024

I saw #145 was only merged recently but an async version would be really useful for me

@omid
Copy link
Contributor

omid commented Mar 7, 2024

Disk cache for async functions is there already if you mean that:
https://github.com/jaemk/cached/blob/master/tests/cached.rs#L1298

If you mean async disk access, nope AFAICS

@bschreck
Copy link
Author

bschreck commented Mar 8, 2024

Hmm what am I doing wrong here? The macro works, but I need to specify custom keys. So the following breaks and does not attempt to use the IOCachedAsync trait:

#[io_cached(
   type = "DiskCache<String, Result<String, TrimItClientError>>",
   map_error = r##"|e| TrimItClientError::DiskError(format!("{:?}", e))"##,
   create = r##" {
        DiskCache::new("uploaded_videos_from_disk_cache")
            .build()
            .expect("error building disk cache")
   } "##,
   convert = r#"{ format!("{}-{}-{}", &project_name.unwrap_or(""), base_url, _email) }"#
)]
async fn uploaded_videos_from_disk_cache(client: &Client, project_name: Option<&str>, base_url: &str, _email: &str) -> Result<String, TrimItClientError> {
    uploaded_videos(client, project_name, base_url, _email).await
}

But I can use:

#[io_cached(
    disk = true,
    time = 30,
    map_error = r##"|e| TrimItClientError::DiskError(format!("{:?}", e))"##,
)]
``` if I don't need to change the default keys.

I get two errors with the first snippet: ```error[E0277]: `Result<std::option::Option<Result<std::string::String, TrimItClientError>>, DiskCacheError>` is not a future
   --> src/modal_client.rs:208:1
    |
208 | /  #[io_cached(
209 | |     type = "DiskCache<String, Result<String, TrimItClientError>>",
210 | |     map_error = r##"|e| TrimItClientError::DiskError(format!("{:?}", e))"##,
211 | |     create = r##" {
...   |
217 | |     convert = r#"{ format!("{}-{}-{}", &project_name.unwrap_or(""), base_url, _email) }"#
218 | |  )]
    | | __^-
    | ||__|
    |  |  `Result<std::option::Option<Result<std::string::String, TrimItClientError>>, DiskCacheError>` is not a future
219 |  | async fn uploaded_videos_from_disk_cache(client: &Client, project_name: Option<&str>, base_u...
    |  |_ help: remove the `.await`
    |
    = help: the trait `Future` is not implemented for `Result<std::option::Option<Result<std::string::String, TrimItClientError>>, DiskCacheError>`, which is required by `Result<std::option::Option<Result<std::string::String, TrimItClientError>>, DiskCacheError>: IntoFuture`
    = note: Result<std::option::Option<Result<std::string::String, TrimItClientError>>, DiskCacheError> must be a future or must implement `IntoFuture` to be awaited
    = note: required for `Result<std::option::Option<Result<std::string::String, TrimItClientError>>, DiskCacheError>` to implement `IntoFuture`

error[E0308]: mismatched types
   --> src/modal_client.rs:208:1
    |
208 | / #[io_cached(
209 | |    type = "DiskCache<String, Result<String, TrimItClientError>>",
210 | |    map_error = r##"|e| TrimItClientError::DiskError(format!("{:?}", e))"##,
211 | |    create = r##" {
...   |
217 | |    convert = r#"{ format!("{}-{}-{}", &project_name.unwrap_or(""), base_url, _email) }"#
218 | | )]
    | |  ^
    | |  |
    | |__expected `Result<String, ...>`, found `String`
    |    arguments to this method are incorrect
    |
    = note: expected enum `Result<std::string::String, TrimItClientError>`
             found struct `std::string::String`
help: the return type of this call is `std::string::String` due to the type of the argument passed
   --> src/modal_client.rs:208:1
    |
208 | / #[io_cached(
209 | |    type = "DiskCache<String, Result<String, TrimItClientError>>",
210 | |    map_error = r##"|e| TrimItClientError::DiskError(format!("{:?}", e))"##,
211 | |    create = r##" {
...   |
217 | |    convert = r#"{ format!("{}-{}-{}", &project_name.unwrap_or(""), base_url, _email) }"#
218 | | )]
    | |__^ this argument influences the return type of `cache_set`
note: method defined here
   --> /Users/ben/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cached-0.49.2/src/lib.rs:404:8
    |
404 |     fn cache_set(&self, k: K, v: V) -> Result<Option<V>, Self::Error>;
    |        ^^^^^^^^^
    = note: this error originates in the attribute macro `io_cached` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: unused import: `cached::IOCachedAsync`
  --> src/modal_client.rs:12:5
   |
12 | use cached::IOCachedAsync;
   |     ^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

@bschreck
Copy link
Author

bschreck commented Mar 8, 2024

Ah had to change the type to just be DiskCache<String, String> without the Result<> (even though my function returns a Result)

   disk = true,
   type = "DiskCache<String, String>",
   map_error = r##"|e| TrimItClientError::DiskError(format!("{:?}", e))"##,
   create = r##" {
        DiskCache::new("uploaded_videos_from_disk_cache")
            .build()
            .expect("error building disk cache")
   } "##,
   convert = r#"{ format!("{}-{}-{}", &project_name.unwrap_or(""), base_url, _email) }"#
)]
async fn uploaded_videos_from_disk_cache(client: &Client, project_name: Option<&str>, base_url: &str, _email: &str) -> Result<String, TrimItClientError> {
    uploaded_videos(client, project_name, base_url, _email).await
}```

@omid
Copy link
Contributor

omid commented Apr 16, 2024

@bschreck so if it's solved, let's close this

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

No branches or pull requests

2 participants