Skip to content

Commit 906bc58

Browse files
committed
updates for latest async-session interface
1 parent d8017cc commit 906bc58

File tree

1 file changed

+22
-64
lines changed

1 file changed

+22
-64
lines changed

src/lib.rs

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
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};
43

54
#[derive(Clone, Debug)]
65
pub struct RedisSessionStore {
76
client: Client,
8-
ttl: Duration,
97
prefix: Option<String>,
108
}
119

1210
impl RedisSessionStore {
1311
pub fn from_client(client: Client) -> Self {
1412
Self {
1513
client,
16-
ttl: Duration::from_secs(86400),
1714
prefix: None,
1815
}
1916
}
2017

21-
pub fn new(connection_info: impl redis::IntoConnectionInfo) -> Result<Self, RedisError> {
18+
pub fn new(connection_info: impl IntoConnectionInfo) -> RedisResult<Self> {
2219
Ok(Self::from_client(Client::open(connection_info)?))
2320
}
2421

@@ -27,11 +24,6 @@ impl RedisSessionStore {
2724
self
2825
}
2926

30-
pub fn with_ttl(mut self, ttl: Duration) -> Self {
31-
self.ttl = ttl;
32-
self
33-
}
34-
3527
fn prefix_key(&self, key: impl AsRef<str>) -> String {
3628
if let Some(ref prefix) = self.prefix {
3729
format!("{}{}", prefix, key.as_ref())
@@ -40,84 +32,50 @@ impl RedisSessionStore {
4032
}
4133
}
4234

43-
async fn connection(&self) -> redis::RedisResult<redis::aio::Connection> {
35+
async fn connection(&self) -> RedisResult<Connection> {
4436
self.client.get_async_std_connection().await
4537
}
4638
}
4739

4840
#[async_trait]
4941
impl SessionStore for RedisSessionStore {
50-
type Error = Error;
51-
5242
async fn load_session(&self, cookie_value: String) -> Option<Session> {
5343
let id = Session::id_from_cookie_value(&cookie_value).ok()?;
5444
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 {
5647
Some(value) => serde_json::from_str(&value).ok()?,
5748
None => None,
5849
}
5950
}
6051

61-
async fn store_session(&self, mut session: Session) -> Option<String> {
52+
async fn store_session(&self, session: Session) -> Option<String> {
6253
let id = session.id();
6354
let string = serde_json::to_string(&session).ok()?;
6455

6556
let mut connection = self.connection().await.ok()?;
66-
connection
67-
.set_ex::<_, _, ()>(id, string, self.ttl.as_secs() as usize)
68-
.await
69-
.ok()?;
7057

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()
7268
}
7369

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?;
7974
Ok(())
8075
}
8176

82-
async fn clear_store(&self) -> Result<(), Self::Error> {
77+
async fn clear_store(&self) -> Result {
8378
self.connection().await?.del(self.prefix_key("*")).await?;
8479
Ok(())
8580
}
8681
}
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

Comments
 (0)