1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
use crate::middleware::session::SessionBackend; use bb8_redis::{bb8::Pool, redis::AsyncCommands, RedisConnectionManager}; use futures::{future::BoxFuture, FutureExt}; use log::error; pub struct RedisBackend { ttl: usize, pool: Pool<RedisConnectionManager>, } impl RedisBackend { pub fn new(ttl: usize, pool: Pool<RedisConnectionManager>) -> Self { Self { ttl, pool } } pub fn pool(pool: Pool<RedisConnectionManager>) -> Self { Self::new(60 * 60 * 24 * 7, pool) } } impl SessionBackend for RedisBackend { fn persist_session<'a>(&'a self, id: &'a str, content: &'a [u8]) -> BoxFuture<'a, bool> { let content = Vec::from(content); let ttl = self.ttl; async move { if let Ok(mut conn) = self.pool.get().await { let conn = conn.as_mut().unwrap(); if let Err(e) = conn.set_ex::<_, _, String>(id, content, ttl).await { error!("Failed to run redis command, {}", e); } else { return true; } } else { error!("Failed to get redis connection from pool"); } false } .boxed() } fn read_session<'a>(&'a self, id: &'a str) -> BoxFuture<'a, Option<Vec<u8>>> { async move { if let Ok(mut conn) = self.pool.get().await { let conn = conn.as_mut().unwrap(); match conn.get(id).await { Ok(value) => return Some(value), Err(e) => error!("Failed to run redis command, {}", e) } } else { error!("Failed to get redis connection from pool"); } None } .boxed() } fn drop_session<'a>(&'a self, id: &'a str) -> BoxFuture<'a, ()> { async move { if let Ok(mut conn) = self.pool.get().await { let conn = conn.as_mut().unwrap(); if let Err(e) = conn.del::<_, String>(id).await { error!("Failed to run redis command, {}", e); } } else { error!("Failed to get redis connection from pool"); } } .boxed() } }