diff --git a/crates/tower-sessions-kv-store/src/lib.rs b/crates/tower-sessions-kv-store/src/lib.rs index 8285f6f..2ba17c3 100644 --- a/crates/tower-sessions-kv-store/src/lib.rs +++ b/crates/tower-sessions-kv-store/src/lib.rs @@ -1,6 +1,7 @@ //! A key-value store backend for the tower-sessions crate. use std::{ + borrow::Borrow, fmt, sync::{Arc, LazyLock}, }; @@ -39,16 +40,16 @@ fn session_id_to_key(id: &Id) -> Key { #[async_trait::async_trait] impl SessionStore for TowerSessionsKvStore { async fn save(&self, session_record: &Record) -> Result<(), Error> { - let mut txn = - self.kv.begin_pessimistic_transaction().await.map_err(|e| { - Error::Backend(format!("Failed to start pessimistic transaction: {e}")) - })?; - let key = session_id_to_key(&session_record.id); let value = Value::serialize(session_record).map_err(|e| { Error::Encode(format!("Failed to serialize session record: {e}")) })?; + let mut txn = + self.kv.begin_pessimistic_transaction().await.map_err(|e| { + Error::Backend(format!("Failed to start pessimistic transaction: {e}")) + })?; + let mut txn = { if let Err(e) = txn.put(&key, value).await.map_err(|e| { Error::Backend(format!("Failed to put session record: {e}")) @@ -73,6 +74,10 @@ impl SessionStore for TowerSessionsKvStore { Ok(()) } + async fn create(&self, session_record: &mut Record) -> Result<(), Error> { + self.save(session_record.borrow()).await + } + async fn load(&self, session_id: &Id) -> Result, Error> { let mut txn = self.kv.begin_optimistic_transaction().await.map_err(|e| {