11use async_session:: { async_trait, chrono:: Utc , log, serde_json, Result , Session , SessionStore } ;
2- use async_std:: task;
32use sqlx:: { pool:: PoolConnection , sqlite:: SqlitePool , Sqlite } ;
4- use std:: time:: Duration ;
53
64/// sqlx sqlite session store for async-sessions
75///
@@ -11,8 +9,9 @@ use std::time::Duration;
119/// use std::time::Duration;
1210///
1311/// # fn main() -> async_session::Result { async_std::task::block_on(async {
14- /// let store = SqliteSessionStore::new("sqlite:%3Amemory :").await?;
12+ /// let store = SqliteSessionStore::new("sqlite::memory :").await?;
1513/// store.migrate().await?;
14+ /// # #[cfg(feature = "async_std")]
1615/// store.spawn_cleanup_task(Duration::from_secs(60 * 60));
1716///
1817/// let mut session = Session::new();
@@ -39,7 +38,7 @@ impl SqliteSessionStore {
3938 /// # use async_sqlx_session::SqliteSessionStore;
4039 /// # use async_session::Result;
4140 /// # fn main() -> Result { async_std::task::block_on(async {
42- /// let pool = sqlx::SqlitePool::connect("sqlite:%3Amemory :").await.unwrap();
41+ /// let pool = sqlx::SqlitePool::connect("sqlite::memory :").await.unwrap();
4342 /// let store = SqliteSessionStore::from_client(pool)
4443 /// .with_table_name("custom_table_name");
4544 /// store.migrate().await;
@@ -66,7 +65,7 @@ impl SqliteSessionStore {
6665 /// # use async_sqlx_session::SqliteSessionStore;
6766 /// # use async_session::Result;
6867 /// # fn main() -> Result { async_std::task::block_on(async {
69- /// let store = SqliteSessionStore::new("sqlite:%3Amemory :").await?;
68+ /// let store = SqliteSessionStore::new("sqlite::memory :").await?;
7069 /// store.migrate().await;
7170 /// # Ok(()) }) }
7271 /// ```
@@ -85,7 +84,7 @@ impl SqliteSessionStore {
8584 /// # use async_sqlx_session::SqliteSessionStore;
8685 /// # use async_session::Result;
8786 /// # fn main() -> Result { async_std::task::block_on(async {
88- /// let store = SqliteSessionStore::new_with_table_name("sqlite:%3Amemory :", "custom_table_name").await?;
87+ /// let store = SqliteSessionStore::new_with_table_name("sqlite::memory :", "custom_table_name").await?;
8988 /// store.migrate().await;
9089 /// # Ok(()) }) }
9190 /// ```
@@ -99,7 +98,7 @@ impl SqliteSessionStore {
9998 /// # use async_sqlx_session::SqliteSessionStore;
10099 /// # use async_session::Result;
101100 /// # fn main() -> Result { async_std::task::block_on(async {
102- /// let store = SqliteSessionStore::new("sqlite:%3Amemory :").await?
101+ /// let store = SqliteSessionStore::new("sqlite::memory :").await?
103102 /// .with_table_name("custom_name");
104103 /// store.migrate().await;
105104 /// # Ok(()) }) }
@@ -109,7 +108,7 @@ impl SqliteSessionStore {
109108 /// # use async_sqlx_session::SqliteSessionStore;
110109 /// # use async_session::Result;
111110 /// # fn main() -> Result { async_std::task::block_on(async {
112- /// let store = SqliteSessionStore::new("sqlite:%3Amemory :").await?
111+ /// let store = SqliteSessionStore::new("sqlite::memory :").await?
113112 /// .with_table_name("johnny (); drop users;");
114113 /// # Ok(()) }) }
115114 /// ```
@@ -139,7 +138,7 @@ impl SqliteSessionStore {
139138 /// # use async_sqlx_session::SqliteSessionStore;
140139 /// # use async_session::{Result, SessionStore, Session};
141140 /// # fn main() -> Result { async_std::task::block_on(async {
142- /// let store = SqliteSessionStore::new("sqlite:%3Amemory :").await?;
141+ /// let store = SqliteSessionStore::new("sqlite::memory :").await?;
143142 /// assert!(store.count().await.is_err());
144143 /// store.migrate().await?;
145144 /// store.store_session(Session::new()).await?;
@@ -177,13 +176,15 @@ impl SqliteSessionStore {
177176 }
178177
179178 /// Spawns an async_std::task that clears out stale (expired)
180- /// sessions on a periodic basis.
179+ /// sessions on a periodic basis. Only available with the
180+ /// async_std feature enabled.
181+ ///
181182 /// ```rust,no_run
182183 /// # use async_sqlx_session::SqliteSessionStore;
183184 /// # use async_session::{Result, SessionStore, Session};
184185 /// # use std::time::Duration;
185186 /// # fn main() -> Result { async_std::task::block_on(async {
186- /// let store = SqliteSessionStore::new("sqlite:%3Amemory :").await?;
187+ /// let store = SqliteSessionStore::new("sqlite::memory :").await?;
187188 /// store.migrate().await?;
188189 /// # let join_handle =
189190 /// store.spawn_cleanup_task(Duration::from_secs(1));
@@ -196,11 +197,15 @@ impl SqliteSessionStore {
196197 /// # join_handle.cancel().await;
197198 /// # Ok(()) }) }
198199 /// ```
199- pub fn spawn_cleanup_task ( & self , period : Duration ) -> task:: JoinHandle < ( ) > {
200+ #[ cfg( feature = "async_std" ) ]
201+ pub fn spawn_cleanup_task (
202+ & self ,
203+ period : std:: time:: Duration ,
204+ ) -> async_std:: task:: JoinHandle < ( ) > {
200205 let store = self . clone ( ) ;
201- task:: spawn ( async move {
206+ async_std :: task:: spawn ( async move {
202207 loop {
203- task:: sleep ( period) . await ;
208+ async_std :: task:: sleep ( period) . await ;
204209 if let Err ( error) = store. cleanup ( ) . await {
205210 log:: error!( "cleanup error: {}" , error) ;
206211 }
@@ -214,7 +219,7 @@ impl SqliteSessionStore {
214219 /// # use async_sqlx_session::SqliteSessionStore;
215220 /// # use async_session::{chrono::{Utc,Duration}, Result, SessionStore, Session};
216221 /// # fn main() -> Result { async_std::task::block_on(async {
217- /// let store = SqliteSessionStore::new("sqlite:%3Amemory :").await?;
222+ /// let store = SqliteSessionStore::new("sqlite::memory :").await?;
218223 /// store.migrate().await?;
219224 /// let mut session = Session::new();
220225 /// session.set_expiry(Utc::now() - Duration::seconds(5));
@@ -247,7 +252,7 @@ impl SqliteSessionStore {
247252 /// # use async_session::{Result, SessionStore, Session};
248253 /// # use std::time::Duration;
249254 /// # fn main() -> Result { async_std::task::block_on(async {
250- /// let store = SqliteSessionStore::new("sqlite:%3Amemory :").await?;
255+ /// let store = SqliteSessionStore::new("sqlite::memory :").await?;
251256 /// store.migrate().await?;
252257 /// assert_eq!(store.count().await?, 0);
253258 /// store.store_session(Session::new()).await?;
@@ -342,9 +347,10 @@ impl SessionStore for SqliteSessionStore {
342347#[ cfg( test) ]
343348mod tests {
344349 use super :: * ;
350+ use std:: time:: Duration ;
345351
346352 async fn test_store ( ) -> SqliteSessionStore {
347- let store = SqliteSessionStore :: new ( "sqlite:%3Amemory :" )
353+ let store = SqliteSessionStore :: new ( "sqlite::memory :" )
348354 . await
349355 . expect ( "building a sqlite :memory: SqliteSessionStore" ) ;
350356 store
@@ -468,7 +474,7 @@ mod tests {
468474
469475 assert ! ( !loaded_session. is_expired( ) ) ;
470476
471- task:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
477+ async_std :: task:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
472478 assert_eq ! ( None , store. load_session( cookie_value) . await ?) ;
473479
474480 Ok ( ( ) )
0 commit comments