1
- use async_session:: { async_trait, base64, serde_json, Session , SessionStore } ;
2
- use redis:: { AsyncCommands , Client , RedisError } ;
3
- use std:: time:: Duration ;
1
+ use async_session:: { async_trait, serde_json, Result , Session , SessionStore } ;
2
+ use redis:: { aio:: Connection , AsyncCommands , Client , IntoConnectionInfo , RedisResult } ;
4
3
5
4
#[ derive( Clone , Debug ) ]
6
5
pub struct RedisSessionStore {
7
6
client : Client ,
8
- ttl : Duration ,
9
7
prefix : Option < String > ,
10
8
}
11
9
12
10
impl RedisSessionStore {
13
11
pub fn from_client ( client : Client ) -> Self {
14
12
Self {
15
13
client,
16
- ttl : Duration :: from_secs ( 86400 ) ,
17
14
prefix : None ,
18
15
}
19
16
}
20
17
21
- pub fn new ( connection_info : impl redis :: IntoConnectionInfo ) -> Result < Self , RedisError > {
18
+ pub fn new ( connection_info : impl IntoConnectionInfo ) -> RedisResult < Self > {
22
19
Ok ( Self :: from_client ( Client :: open ( connection_info) ?) )
23
20
}
24
21
@@ -27,11 +24,6 @@ impl RedisSessionStore {
27
24
self
28
25
}
29
26
30
- pub fn with_ttl ( mut self , ttl : Duration ) -> Self {
31
- self . ttl = ttl;
32
- self
33
- }
34
-
35
27
fn prefix_key ( & self , key : impl AsRef < str > ) -> String {
36
28
if let Some ( ref prefix) = self . prefix {
37
29
format ! ( "{}{}" , prefix, key. as_ref( ) )
@@ -40,84 +32,50 @@ impl RedisSessionStore {
40
32
}
41
33
}
42
34
43
- async fn connection ( & self ) -> redis :: RedisResult < redis :: aio :: Connection > {
35
+ async fn connection ( & self ) -> RedisResult < Connection > {
44
36
self . client . get_async_std_connection ( ) . await
45
37
}
46
38
}
47
39
48
40
#[ async_trait]
49
41
impl SessionStore for RedisSessionStore {
50
- type Error = Error ;
51
-
52
42
async fn load_session ( & self , cookie_value : String ) -> Option < Session > {
53
43
let id = Session :: id_from_cookie_value ( & cookie_value) . ok ( ) ?;
54
44
let mut connection = self . connection ( ) . await . ok ( ) ?;
55
- match connection. get :: < _ , Option < String > > ( id) . await . ok ( ) ? {
45
+ let record: Option < String > = connection. get ( id) . await . ok ( ) ?;
46
+ match record {
56
47
Some ( value) => serde_json:: from_str ( & value) . ok ( ) ?,
57
48
None => None ,
58
49
}
59
50
}
60
51
61
- async fn store_session ( & self , mut session : Session ) -> Option < String > {
52
+ async fn store_session ( & self , session : Session ) -> Option < String > {
62
53
let id = session. id ( ) ;
63
54
let string = serde_json:: to_string ( & session) . ok ( ) ?;
64
55
65
56
let mut connection = self . connection ( ) . await . ok ( ) ?;
66
- connection
67
- . set_ex :: < _ , _ , ( ) > ( id, string, self . ttl . as_secs ( ) as usize )
68
- . await
69
- . ok ( ) ?;
70
57
71
- session. take_cookie_value ( )
58
+ match session. expires_in ( ) {
59
+ None => connection. set ( id, string) . await . ok ( ) ?,
60
+
61
+ Some ( expiry) => connection
62
+ . set_ex ( id, string, expiry. as_secs ( ) as usize )
63
+ . await
64
+ . ok ( ) ?,
65
+ } ;
66
+
67
+ session. into_cookie_value ( )
72
68
}
73
69
74
- async fn destroy_session ( & self , session : Session ) -> Result < ( ) , Self :: Error > {
75
- self . connection ( )
76
- . await ?
77
- . del :: < _ , ( ) > ( self . prefix_key ( session. id ( ) . to_string ( ) ) )
78
- . await ?;
70
+ async fn destroy_session ( & self , session : Session ) -> Result {
71
+ let mut connection = self . connection ( ) . await ?;
72
+ let key = self . prefix_key ( session. id ( ) . to_string ( ) ) ;
73
+ connection. del ( key) . await ?;
79
74
Ok ( ( ) )
80
75
}
81
76
82
- async fn clear_store ( & self ) -> Result < ( ) , Self :: Error > {
77
+ async fn clear_store ( & self ) -> Result {
83
78
self . connection ( ) . await ?. del ( self . prefix_key ( "*" ) ) . await ?;
84
79
Ok ( ( ) )
85
80
}
86
81
}
87
-
88
- #[ derive( Debug ) ]
89
- pub enum Error {
90
- RedisError ( RedisError ) ,
91
- SerdeError ( serde_json:: Error ) ,
92
- Base64Error ( base64:: DecodeError ) ,
93
- }
94
-
95
- impl std:: fmt:: Display for Error {
96
- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
97
- match self {
98
- Error :: RedisError ( e) => e. fmt ( f) ,
99
- Error :: SerdeError ( e) => e. fmt ( f) ,
100
- Error :: Base64Error ( e) => e. fmt ( f) ,
101
- }
102
- }
103
- }
104
-
105
- impl From < serde_json:: Error > for Error {
106
- fn from ( e : serde_json:: Error ) -> Self {
107
- Self :: SerdeError ( e)
108
- }
109
- }
110
-
111
- impl From < base64:: DecodeError > for Error {
112
- fn from ( e : base64:: DecodeError ) -> Self {
113
- Self :: Base64Error ( e)
114
- }
115
- }
116
-
117
- impl From < RedisError > for Error {
118
- fn from ( e : RedisError ) -> Self {
119
- Self :: RedisError ( e)
120
- }
121
- }
122
-
123
- impl std:: error:: Error for Error { }
0 commit comments