Facing problem with using Memcache globally (&mut)

using lazy_static I do this

    static ref CLIENT: Box<Pool<MemcacheConnectionManager>> = Box::new(
        block_on(MemcacheClient::init(CoreConfig::init(
            "config/app/config.yaml",
        )))
        .client
        .unwrap()
    );
    static ref CONN: RwLock<PooledConnection<'static, MemcacheConnectionManager>> =
        RwLock::new(*Box::new(block_on((unsafe { CLIENT }).get()).unwrap()));

and eventually try to do this:

     CONN.read()
            .await
            .borrow_mut()
            .get_multi(&cache_keys_ref)

I'm getting the following error:

cannot borrow data in dereference of tokio::sync::RwLockReadGuard<'_, PooledConnection<'_, MemcacheConnectionManager>> as mutable
trait DerefMut is required to modify through a dereference, but it is not implemented for tokio::sync::RwLockReadGuard<'_, PooledConnection<'_, MemcacheConnectionManager>>rustcE0596

I have searched a lot of approaches but in rust such pattern is not allowed. I wish to proceed with such approach where we keep the connection open for better latency numbers. At the moment only memcache-async (ascii) is viable but facing the connection issue. Any help would be appreciated.

Where is the method get_multi() defined? The similarly-named get() takes &self, so what are you attempting to do with borrow_mut()?

I'm trying to get multiple keys, more of definition over here:

I also see the .get() is defined with &mut self and not &self

Hmm, I can't seem to find how that method would be available from your PooledConnection. In any case, you're storing CONN in a RwLock, so you'll want to use CONN.write() instead of CONN.read() to obtain a mutable guard.

1 Like

That worked. Thank you @LegionMammal978