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 , sqlite:: SqlitePool , Sqlite } ;
4
- use std:: time:: Duration ;
5
3
6
4
/// sqlx sqlite session store for async-sessions
7
5
///
@@ -11,8 +9,9 @@ use std::time::Duration;
11
9
/// use std::time::Duration;
12
10
///
13
11
/// # 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?;
15
13
/// store.migrate().await?;
14
+ /// # #[cfg(feature = "async_std")]
16
15
/// store.spawn_cleanup_task(Duration::from_secs(60 * 60));
17
16
///
18
17
/// let mut session = Session::new();
@@ -39,7 +38,7 @@ impl SqliteSessionStore {
39
38
/// # use async_sqlx_session::SqliteSessionStore;
40
39
/// # use async_session::Result;
41
40
/// # 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();
43
42
/// let store = SqliteSessionStore::from_client(pool)
44
43
/// .with_table_name("custom_table_name");
45
44
/// store.migrate().await;
@@ -66,7 +65,7 @@ impl SqliteSessionStore {
66
65
/// # use async_sqlx_session::SqliteSessionStore;
67
66
/// # use async_session::Result;
68
67
/// # 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?;
70
69
/// store.migrate().await;
71
70
/// # Ok(()) }) }
72
71
/// ```
@@ -85,7 +84,7 @@ impl SqliteSessionStore {
85
84
/// # use async_sqlx_session::SqliteSessionStore;
86
85
/// # use async_session::Result;
87
86
/// # 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?;
89
88
/// store.migrate().await;
90
89
/// # Ok(()) }) }
91
90
/// ```
@@ -99,7 +98,7 @@ impl SqliteSessionStore {
99
98
/// # use async_sqlx_session::SqliteSessionStore;
100
99
/// # use async_session::Result;
101
100
/// # 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?
103
102
/// .with_table_name("custom_name");
104
103
/// store.migrate().await;
105
104
/// # Ok(()) }) }
@@ -109,7 +108,7 @@ impl SqliteSessionStore {
109
108
/// # use async_sqlx_session::SqliteSessionStore;
110
109
/// # use async_session::Result;
111
110
/// # 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?
113
112
/// .with_table_name("johnny (); drop users;");
114
113
/// # Ok(()) }) }
115
114
/// ```
@@ -139,7 +138,7 @@ impl SqliteSessionStore {
139
138
/// # use async_sqlx_session::SqliteSessionStore;
140
139
/// # use async_session::{Result, SessionStore, Session};
141
140
/// # 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?;
143
142
/// assert!(store.count().await.is_err());
144
143
/// store.migrate().await?;
145
144
/// store.store_session(Session::new()).await?;
@@ -177,13 +176,15 @@ impl SqliteSessionStore {
177
176
}
178
177
179
178
/// 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
+ ///
181
182
/// ```rust,no_run
182
183
/// # use async_sqlx_session::SqliteSessionStore;
183
184
/// # use async_session::{Result, SessionStore, Session};
184
185
/// # use std::time::Duration;
185
186
/// # 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?;
187
188
/// store.migrate().await?;
188
189
/// # let join_handle =
189
190
/// store.spawn_cleanup_task(Duration::from_secs(1));
@@ -196,11 +197,15 @@ impl SqliteSessionStore {
196
197
/// # join_handle.cancel().await;
197
198
/// # Ok(()) }) }
198
199
/// ```
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 < ( ) > {
200
205
let store = self . clone ( ) ;
201
- task:: spawn ( async move {
206
+ async_std :: task:: spawn ( async move {
202
207
loop {
203
- task:: sleep ( period) . await ;
208
+ async_std :: task:: sleep ( period) . await ;
204
209
if let Err ( error) = store. cleanup ( ) . await {
205
210
log:: error!( "cleanup error: {}" , error) ;
206
211
}
@@ -214,7 +219,7 @@ impl SqliteSessionStore {
214
219
/// # use async_sqlx_session::SqliteSessionStore;
215
220
/// # use async_session::{chrono::{Utc,Duration}, Result, SessionStore, Session};
216
221
/// # 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?;
218
223
/// store.migrate().await?;
219
224
/// let mut session = Session::new();
220
225
/// session.set_expiry(Utc::now() - Duration::seconds(5));
@@ -247,7 +252,7 @@ impl SqliteSessionStore {
247
252
/// # use async_session::{Result, SessionStore, Session};
248
253
/// # use std::time::Duration;
249
254
/// # 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?;
251
256
/// store.migrate().await?;
252
257
/// assert_eq!(store.count().await?, 0);
253
258
/// store.store_session(Session::new()).await?;
@@ -342,9 +347,10 @@ impl SessionStore for SqliteSessionStore {
342
347
#[ cfg( test) ]
343
348
mod tests {
344
349
use super :: * ;
350
+ use std:: time:: Duration ;
345
351
346
352
async fn test_store ( ) -> SqliteSessionStore {
347
- let store = SqliteSessionStore :: new ( "sqlite:%3Amemory :" )
353
+ let store = SqliteSessionStore :: new ( "sqlite::memory :" )
348
354
. await
349
355
. expect ( "building a sqlite :memory: SqliteSessionStore" ) ;
350
356
store
@@ -468,7 +474,7 @@ mod tests {
468
474
469
475
assert ! ( !loaded_session. is_expired( ) ) ;
470
476
471
- task:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
477
+ async_std :: task:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
472
478
assert_eq ! ( None , store. load_session( cookie_value) . await ?) ;
473
479
474
480
Ok ( ( ) )
0 commit comments