Hey, I have implemented an async cache and the cache requires the user to create it with a loader function. The loader function should return a Sync Future:
impl<K, V, E> LoadingCache<K, V, E>
where
K: Eq + Hash + Send + Sync + 'static,
V: Send + Sync + 'static,
{
pub fn new<FUser, Fut, Q>(loader: FUser, expire_after_write: Duration) -> Self
where
K: Borrow<Q>,
FUser: Fn(&Q) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<V, E>> + Send + Sync + 'static, // <- Here
{
Self::with_capacity(loader, expire_after_write, 0)
}
pub fn with_capacity<FUser, Fut, Q>(
loader: FUser,
expire_after_write: Duration,
capacity: usize,
) -> Self
where
K: Borrow<Q>,
FUser: Fn(&Q) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<V, E>> + Send + Sync + 'static,
{
LoadingCache {
map: Arc::new(DashMap::with_capacity(capacity)),
loader: Arc::new(move |k| Box::pin((loader)(k.as_ref().borrow()))),
expire_after_write,
stats: Arc::new(Default::default()),
}
}
}
When trying to use it with redis-rs, I am creating the loader as such:
let loader = |_| get_custom_domains(&mut conn);
Ok(LoadingCache::new(loader, Duration::from_secs(10 * 60)))
where:
pub async fn get_custom_domains(redis_conn: &mut Connection) -> Result<HashSet<String>> {
let redis_resp = redis_conn.smembers("custom-domains").await;
match redis_resp {
Ok(data) => Ok(data),
Err(e) => Err(anyhow!("failed to get custom domains fomr redis: {}", e)),
}
}
But this fails to compile:
error: future cannot be shared between threads safely
--> src/cache.rs:19:8
|
19 | Ok(LoadingCache::new(loader, Duration::from_secs(10 * 60)))
| ^^^^^^^^^^^^^^^^^ future returned by `get_custom_domains` is not `Sync`
|
::: src/ttl_cache.rs:45:5
|
45 | / pub fn new<FUser, Fut, Q>(loader: FUser, expire_after_write: Duration) -> Self
46 | | where
47 | | K: Borrow<Q>,
48 | | FUser: Fn(&Q) -> Fut + Send + Sync + 'static,
49 | | Fut: Future<Output = Result<V, E>> + Send + Sync + 'static,
| |___________________________________________________________________- required by `LoadingCache::<K, V, E>::new`
|
= help: the trait `Sync` is not implemented for `dyn Future<Output = Result<HashSet<std::string::String>, RedisError>> + Send`
note: future is not `Sync` as it awaits another future which is not `Sync`
--> src/redis.rs:35:22
|
35 | let redis_resp = redis_conn.smembers("custom-domains").await;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ await occurs here on type `Pin<Box<dyn Future<Output = Result<HashSet<std::string::String>, RedisError>> + Send>>`, which is not `Sync`
- Is this because inherently the
redis-rsAPI returns a!Syncfuture? Maybe this is related to the API taking a&mut Connection?Or is it something else causing this? - Can this be worked around by creating a proper loader closure?