Return Arc<RedisServer>

This commit is contained in:
Temirkhan Myrzamadi 2020-07-04 21:02:07 +06:00
parent 82ade822cf
commit 56dadfbb34
3 changed files with 16 additions and 22 deletions

View file

@ -9,7 +9,6 @@ mod transitions;
use states::*; use states::*;
use transitions::*; use transitions::*;
use std::sync::Arc;
use teloxide::{ use teloxide::{
dispatching::dialogue::{serializer::Bincode, RedisStorage, Storage}, dispatching::dialogue::{serializer::Bincode, RedisStorage, Storage},
prelude::*, prelude::*,
@ -54,15 +53,13 @@ async fn run() {
.await .await
.expect("Something is wrong with the bot!") .expect("Something is wrong with the bot!")
}, },
Arc::new( // You can also choose serializer::JSON or serializer::CBOR
// You can also choose serializer::JSON or serializer::CBOR // All serializers but JSON require enabling feature
// All serializers but JSON require enabling feature // "serializer-<name>", e. g. "serializer-cbor"
// "serializer-<name>", e. g. "serializer-cbor" // or "serializer-bincode"
// or "serializer-bincode" RedisStorage::open("redis://127.0.0.1:6379", Bincode)
RedisStorage::open("redis://127.0.0.1:6379", Bincode) .await
.await .unwrap(),
.unwrap(),
),
)) ))
.dispatch() .dispatch()
.await; .await;

View file

@ -35,13 +35,13 @@ impl<S> RedisStorage<S> {
pub async fn open( pub async fn open(
url: impl IntoConnectionInfo, url: impl IntoConnectionInfo,
serializer: S, serializer: S,
) -> Result<Self, RedisStorageError<Infallible>> { ) -> Result<Arc<Self>, RedisStorageError<Infallible>> {
Ok(Self { Ok(Arc::new(Self {
conn: Mutex::new( conn: Mutex::new(
redis::Client::open(url)?.get_async_connection().await?, redis::Client::open(url)?.get_async_connection().await?,
), ),
serializer, serializer,
}) }))
} }
} }

View file

@ -10,25 +10,22 @@ use teloxide::dispatching::dialogue::{
#[tokio::test] #[tokio::test]
async fn test_redis_json() { async fn test_redis_json() {
let storage = Arc::new( let storage =
RedisStorage::open("redis://127.0.0.1:7777", JSON).await.unwrap(), RedisStorage::open("redis://127.0.0.1:7777", JSON).await.unwrap();
);
test_redis(storage).await; test_redis(storage).await;
} }
#[tokio::test] #[tokio::test]
async fn test_redis_bincode() { async fn test_redis_bincode() {
let storage = Arc::new( let storage =
RedisStorage::open("redis://127.0.0.1:7778", Bincode).await.unwrap(), RedisStorage::open("redis://127.0.0.1:7778", Bincode).await.unwrap();
);
test_redis(storage).await; test_redis(storage).await;
} }
#[tokio::test] #[tokio::test]
async fn test_redis_cbor() { async fn test_redis_cbor() {
let storage = Arc::new( let storage =
RedisStorage::open("redis://127.0.0.1:7779", CBOR).await.unwrap(), RedisStorage::open("redis://127.0.0.1:7779", CBOR).await.unwrap();
);
test_redis(storage).await; test_redis(storage).await;
} }