Skip to content

Commit

Permalink
Add Moka session caching
Browse files Browse the repository at this point in the history
Signed-off-by: Elizabeth Myers <[email protected]>
  • Loading branch information
Elizafox committed Mar 18, 2024
1 parent da08457 commit 095d70b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ tracing-subscriber = { version = "0.3.18", features = ["local-time", "parking_lo
tower = { version = "0.4.13", features = ["timeout", "tokio"] }
tower-http = { version = "0.5.2", features = ["fs", "normalize-path", "timeout", "tokio"] }
tower-sessions = { version = "0.11.1", features = ["axum-core"] }
tower-sessions-moka-store = "0.11.0"
tower-sessions-redis-store = "0.11.0"
url = "2.5.0"
validator = { version = "0.17.0", features = ["derive"] }
9 changes: 6 additions & 3 deletions src/web/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ use time::Duration;
use tokio::task::JoinHandle;
use tower::ServiceBuilder;
use tower_http::{normalize_path::NormalizePathLayer, timeout::TimeoutLayer};
use tower_sessions::{Expiry, SessionManagerLayer};
use tower_sessions::{CachingSessionStore, Expiry, SessionManagerLayer};
use tower_sessions_moka_store::MokaStore;
use tower_sessions_redis_store::{fred::prelude::*, RedisStore};
use tracing::info;

Expand Down Expand Up @@ -87,8 +88,10 @@ impl App {

// Begin serving
pub(crate) async fn serve(self) -> Result<(), Box<dyn std::error::Error>> {
let session_store = RedisStore::new(self.redis_pool);
let session_layer = SessionManagerLayer::new(session_store)
let redis_store = RedisStore::new(self.redis_pool);
let moka_store = MokaStore::new(Some(100));
let caching_store = CachingSessionStore::new(moka_store, redis_store);
let session_layer = SessionManagerLayer::new(caching_store)
.with_expiry(Expiry::OnInactivity(Duration::days(1)));

let backend = Backend::new(self.state.db.clone());
Expand Down

0 comments on commit 095d70b

Please sign in to comment.