Async disk cache
bschreck opened this issue · comments
I saw #145 was only merged recently but an async version would be really useful for me
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
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
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
}```