1
1
use async_session:: { async_trait, chrono:: Utc , log, serde_json, Result , Session , SessionStore } ;
2
- use async_std:: task;
3
2
use sqlx:: { pool:: PoolConnection , Executor , MySql , MySqlPool } ;
4
- use std:: time:: Duration ;
5
3
6
4
/// sqlx mysql session store for async-sessions
7
5
///
@@ -14,6 +12,7 @@ use std::time::Duration;
14
12
/// let store = MySqlSessionStore::new(&std::env::var("MYSQL_TEST_DB_URL").unwrap()).await?;
15
13
/// store.migrate().await?;
16
14
/// # store.clear_store().await?;
15
+ /// # #[cfg(feature = "async_std")]
17
16
/// store.spawn_cleanup_task(Duration::from_secs(60 * 60));
18
17
///
19
18
/// let mut session = Session::new();
@@ -178,7 +177,9 @@ impl MySqlSessionStore {
178
177
}
179
178
180
179
/// 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
+ ///
182
183
/// ```rust,no_run
183
184
/// # use async_sqlx_session::MySqlSessionStore;
184
185
/// # use async_session::{Result, SessionStore, Session};
@@ -197,7 +198,8 @@ impl MySqlSessionStore {
197
198
/// # join_handle.cancel().await;
198
199
/// # Ok(()) }) }
199
200
/// ```
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 < ( ) > {
201
203
let store = self . clone ( ) ;
202
204
task:: spawn ( async move {
203
205
loop {
@@ -228,7 +230,7 @@ impl MySqlSessionStore {
228
230
/// ```
229
231
pub async fn cleanup ( & self ) -> sqlx:: Result < ( ) > {
230
232
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 < ? " ) )
232
234
. bind ( Utc :: now ( ) )
233
235
. execute ( & mut connection)
234
236
. await ?;
@@ -330,6 +332,7 @@ impl SessionStore for MySqlSessionStore {
330
332
mod tests {
331
333
use super :: * ;
332
334
use async_session:: chrono:: DateTime ;
335
+ use std:: time:: Duration ;
333
336
334
337
async fn test_store ( ) -> MySqlSessionStore {
335
338
let store = MySqlSessionStore :: new ( & std:: env:: var ( "MYSQL_TEST_DB_URL" ) . unwrap ( ) )
@@ -462,7 +465,7 @@ mod tests {
462
465
463
466
assert ! ( !loaded_session. is_expired( ) ) ;
464
467
465
- task:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
468
+ async_std :: task:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
466
469
assert_eq ! ( None , store. load_session( cookie_value) . await ?) ;
467
470
468
471
Ok ( ( ) )
0 commit comments