Skip to content

Commit 2ee2550

Browse files
committed
update mysql implementation to support optional async-std feature
1 parent ea9042b commit 2ee2550

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/mysql.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use async_session::{async_trait, chrono::Utc, log, serde_json, Result, Session, SessionStore};
2-
use async_std::task;
32
use sqlx::{pool::PoolConnection, Executor, MySql, MySqlPool};
4-
use std::time::Duration;
53

64
/// sqlx mysql session store for async-sessions
75
///
@@ -14,6 +12,7 @@ use std::time::Duration;
1412
/// let store = MySqlSessionStore::new(&std::env::var("MYSQL_TEST_DB_URL").unwrap()).await?;
1513
/// store.migrate().await?;
1614
/// # store.clear_store().await?;
15+
/// # #[cfg(feature = "async_std")]
1716
/// store.spawn_cleanup_task(Duration::from_secs(60 * 60));
1817
///
1918
/// let mut session = Session::new();
@@ -178,7 +177,9 @@ impl MySqlSessionStore {
178177
}
179178

180179
/// Spawns an async_std::task that clears out stale (expired)
181-
/// sessions on a periodic basis.
180+
/// sessions on a periodic basis. Only available with the
181+
/// async_std feature enabled.
182+
///
182183
/// ```rust,no_run
183184
/// # use async_sqlx_session::MySqlSessionStore;
184185
/// # use async_session::{Result, SessionStore, Session};
@@ -197,7 +198,8 @@ impl MySqlSessionStore {
197198
/// # join_handle.cancel().await;
198199
/// # Ok(()) }) }
199200
/// ```
200-
pub fn spawn_cleanup_task(&self, period: Duration) -> task::JoinHandle<()> {
201+
#[cfg(feature = "async_std")]
202+
pub fn spawn_cleanup_task(&self, period: std::time::Duration) -> task::JoinHandle<()> {
201203
let store = self.clone();
202204
task::spawn(async move {
203205
loop {
@@ -228,7 +230,7 @@ impl MySqlSessionStore {
228230
/// ```
229231
pub async fn cleanup(&self) -> sqlx::Result<()> {
230232
let mut connection = self.connection().await?;
231-
sqlx::query(&self.substitute_table_name("DELETE FROM %%TABLE_NAME%% WHERE expires < $1"))
233+
sqlx::query(&self.substitute_table_name("DELETE FROM %%TABLE_NAME%% WHERE expires < ?"))
232234
.bind(Utc::now())
233235
.execute(&mut connection)
234236
.await?;
@@ -330,6 +332,7 @@ impl SessionStore for MySqlSessionStore {
330332
mod tests {
331333
use super::*;
332334
use async_session::chrono::DateTime;
335+
use std::time::Duration;
333336

334337
async fn test_store() -> MySqlSessionStore {
335338
let store = MySqlSessionStore::new(&std::env::var("MYSQL_TEST_DB_URL").unwrap())
@@ -462,7 +465,7 @@ mod tests {
462465

463466
assert!(!loaded_session.is_expired());
464467

465-
task::sleep(Duration::from_secs(1)).await;
468+
async_std::task::sleep(Duration::from_secs(1)).await;
466469
assert_eq!(None, store.load_session(cookie_value).await?);
467470

468471
Ok(())

0 commit comments

Comments
 (0)