jaemk / cached

Rust cache structures and easy function memoization
MIT License
1.57k stars 95 forks source link

Errors seen while using AsyncRedisCache #136

Open rajesh-blueshift opened 1 year ago

rajesh-blueshift commented 1 year ago

Hi, I am a newbie to rust, I am trying to create a module based on this the example given for AsyncRedisCache

use cached::proc_macro::io_cached;
use cached::AsyncRedisCache;
use thiserror::Error;

#[derive(Error, Debug, PartialEq, Clone)]
enum ExampleError {
    #[error("error with redis cache `{0}`")]
    RedisError(String),
}

/// Cache the results of an async function in redis. Cache
/// keys will be prefixed with `cache_redis_prefix`.
/// A `map_error` closure must be specified to convert any
/// redis cache errors into the same type of error returned
/// by your function. All `io_cached` functions must return `Result`s.
#[io_cached(
    map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##,
    type = "AsyncRedisCache<u64, String>",
    create = r##" {
        AsyncRedisCache::new("cached_redis_prefix", 1)
            .set_refresh(true)
            .build()
            .await
            .expect("error building example redis cache")
    } "##
)]
async fn async_cached_sleep_secs(secs: u64) -> Result<String, ExampleError> {
    std::thread::sleep(std::time::Duration::from_secs(secs));
    Ok(secs.to_string())
}

However, I get following errors:

`*const AsyncRedisCache<u64, std::string::String>` cannot be sent between threads safely
the trait `std::marker::Send` is not implemented for `*const AsyncRedisCache<u64, std::string::String>`
required because of the requirements on the impl of `std::marker::Send` for `Cell<*const AsyncRedisCache<u64, std::string::String>>`
required because of the requirements on the impl of `std::marker::Sync` for `spin::once::Once<AsyncOnce<AsyncRedisCache<u64, std::string::String>>>`
shared static variables must have a type that implements `Sync`

Am I missing something?