|
| 1 | +//! # async-redis-session |
| 2 | +//! ```rust |
| 3 | +//! use async_redis_session::RedisSessionStore; |
| 4 | +//! use async_session::{Session, SessionStore}; |
| 5 | +//! |
| 6 | +//! # fn main() -> async_session::Result { async_std::task::block_on(async { |
| 7 | +//! let store = RedisSessionStore::new("redis://127.0.0.1/")?; |
| 8 | +//! |
| 9 | +//! let session = Session::new(); |
| 10 | +//! session.insert("key".into(), "value".into()); |
| 11 | +//! |
| 12 | +//! let cookie_value = store.store_session(session).await.unwrap(); |
| 13 | +//! let session = store.load_session(cookie_value).await.unwrap(); |
| 14 | +//! assert_eq!(session.get("key").unwrap(), "value"); |
| 15 | +//! # Ok(()) }) } |
| 16 | +//! ``` |
| 17 | +
|
| 18 | +#![forbid(unsafe_code, future_incompatible)] |
| 19 | +#![deny( |
| 20 | + missing_debug_implementations, |
| 21 | + nonstandard_style, |
| 22 | + missing_docs, |
| 23 | + unreachable_pub, |
| 24 | + missing_copy_implementations, |
| 25 | + unused_qualifications |
| 26 | +)] |
| 27 | + |
1 | 28 | use async_session::{async_trait, serde_json, Result, Session, SessionStore};
|
2 | 29 | use redis::{aio::Connection, AsyncCommands, Client, IntoConnectionInfo, RedisResult};
|
3 | 30 |
|
| 31 | +/// # RedisSessionStore |
4 | 32 | #[derive(Clone, Debug)]
|
5 | 33 | pub struct RedisSessionStore {
|
6 | 34 | client: Client,
|
7 | 35 | prefix: Option<String>,
|
8 | 36 | }
|
9 | 37 |
|
10 | 38 | impl RedisSessionStore {
|
| 39 | + /// creates a redis store from an existing [`redis::Client`] |
| 40 | + /// ```rust |
| 41 | + /// # use async_redis_session::RedisSessionStore; |
| 42 | + /// let client = redis::Client::open("redis://127.0.0.1").unwrap(); |
| 43 | + /// let store = RedisSessionStore::from_client(client); |
| 44 | + /// ``` |
11 | 45 | pub fn from_client(client: Client) -> Self {
|
12 | 46 | Self {
|
13 | 47 | client,
|
14 | 48 | prefix: None,
|
15 | 49 | }
|
16 | 50 | }
|
17 | 51 |
|
| 52 | + /// creates a redis store from a [`redis::IntoConnectionInfo`] |
| 53 | + /// such as a [`String`], [`&str`](str), or [`Url`](../url/struct.Url.html) |
| 54 | + /// ```rust |
| 55 | + /// # use async_redis_session::RedisSessionStore; |
| 56 | + /// let store = RedisSessionStore::new("redis://127.0.0.1").unwrap(); |
| 57 | + /// ``` |
18 | 58 | pub fn new(connection_info: impl IntoConnectionInfo) -> RedisResult<Self> {
|
19 | 59 | Ok(Self::from_client(Client::open(connection_info)?))
|
20 | 60 | }
|
21 | 61 |
|
22 |
| - pub fn with_prefix(mut self, prefix: String) -> Self { |
23 |
| - self.prefix = Some(prefix); |
| 62 | + /// sets a key prefix for this session store |
| 63 | + /// |
| 64 | + /// ```rust |
| 65 | + /// # use async_redis_session::RedisSessionStore; |
| 66 | + /// let store = RedisSessionStore::new("redis://127.0.0.1").unwrap() |
| 67 | + /// .with_prefix("async-sessions/"); |
| 68 | + /// ``` |
| 69 | + /// ```rust |
| 70 | + /// # use async_redis_session::RedisSessionStore; |
| 71 | + /// let client = redis::Client::open("redis://127.0.0.1").unwrap(); |
| 72 | + /// let store = RedisSessionStore::from_client(client) |
| 73 | + /// .with_prefix("async-sessions/"); |
| 74 | + /// ``` |
| 75 | + pub fn with_prefix(mut self, prefix: impl AsRef<str>) -> Self { |
| 76 | + self.prefix = Some(prefix.as_ref().to_owned()); |
24 | 77 | self
|
25 | 78 | }
|
26 | 79 |
|
|
0 commit comments