Have been interested in using Redis Multiplexed with Actix Web but am a bit confused in the implementation.
Is a mutex required here or is it thread safe in the way a connection pool is, and I'm missing something? I noticed the only way I could actually get things to work with the compiler with a Mutex, was with the get_tokio_multiplexed_connection...() from a client that I passed into the handler, but that doesn't really seem to make sense. Hopefully someone can point me in the right direction because there is not much documentation online about it
When you call get_multiplexed_tokio_connection you get a MultiplexedConnection struct, which can be cloned to send requests concurrently. You don't need any other synchronization primitive on top of it.
Okay thanks for the response, but when passing the connection to a handler like
App()
.app_data(conn)
.service(service)
...
It is wrapped in web::Data which I can only manage to dereference to an arc, and the compiler complains at the following or anything like it since cache needs to be mutable as well.
The thing is you can just use get_tokio_multiplexed() on the underlying client through web::Data<> which is why I wondered if I was thinking about this wrong. Currently have it wrapped in a Mutex in order to access the mutable multiplexed connection.
Rather than using the multiplexed connection directly I personally prefer using a connection pool that handles everything for me. You can take a look at deadpool-redis.
Then you can use it like this with Actix web:
use deadpool_redis::{Config, Pool, Runtime};
let cfg = Config::from_url(host_url);
let pool = cfg.create_pool(Some(Runtime::Tokio1)).unwrap();
App::new().app_data(web::Data::new(pool))...
Yeah I considered using a pool (I prefer bb8), but this is just for a small app I'm going to host at home and was hoping to try out multiplexing's performance and get some experience with it
Data is not the right extractor for your use-case. You want to clone the multiplexed connection for each request, not an Arc that wraps one single multiplexed connection instance. You could use Arc::unwrap_or_clone to get a clone of the inner MultiplexedConnection, but using Actix's ThinData extractor in favour of Data would be the better solution to achieve the same.