2020-01-30 00:31:25 +00:00
|
|
|
# actix-redis
|
2018-01-22 23:16:24 -08:00
|
|
|
|
2021-03-21 09:38:29 +00:00
|
|
|
> Redis integration for Actix and session store for Actix Web.
|
2020-01-30 00:31:25 +00:00
|
|
|
|
2021-03-21 09:38:29 +00:00
|
|
|
[](https://crates.io/crates/actix-redis)
|
2021-12-12 19:12:46 +00:00
|
|
|
[](https://docs.rs/actix-redis/0.10.0-beta.4)
|
2021-03-21 09:38:29 +00:00
|
|
|

|
2021-12-12 19:12:46 +00:00
|
|
|
[](https://deps.rs/crate/actix-redis/0.10.0-beta.4)
|
2018-01-22 23:13:18 -08:00
|
|
|
|
2021-03-21 09:38:29 +00:00
|
|
|
## Documentation & Resources
|
2018-01-22 23:13:18 -08:00
|
|
|
|
2021-04-09 16:39:50 +03:00
|
|
|
- [API Documentation](https://docs.rs/actix-redis)
|
2021-03-21 09:38:29 +00:00
|
|
|
- [Example Project](https://github.com/actix/examples/tree/HEAD/session/redis-session)
|
2021-10-19 01:48:41 +01:00
|
|
|
- Minimum Supported Rust Version (MSRV): 1.52
|
2018-01-22 23:13:18 -08:00
|
|
|
|
2021-03-21 09:38:29 +00:00
|
|
|
## Redis Session Backend
|
2017-12-29 01:10:27 -08:00
|
|
|
|
|
|
|
Use redis as session storage.
|
|
|
|
|
|
|
|
You need to pass an address of the redis server and random value to the
|
2020-05-15 14:14:43 +03:00
|
|
|
constructor of `RedisSession`. This is private key for cookie session,
|
2017-12-29 01:10:27 -08:00
|
|
|
When this value is changed, all session data is lost.
|
|
|
|
|
|
|
|
Note that whatever you write into your session is visible by the user (but not modifiable).
|
|
|
|
|
|
|
|
Constructor panics if key length is less than 32 bytes.
|
|
|
|
|
2018-05-08 10:12:57 -07:00
|
|
|
```rust
|
2021-04-09 16:39:50 +03:00
|
|
|
use actix_web::{App, HttpServer, middleware::Logger};
|
|
|
|
use actix_web::web::{resource, get}
|
2020-05-15 14:14:43 +03:00
|
|
|
use actix_redis::RedisSession;
|
2017-12-29 01:10:27 -08:00
|
|
|
|
2021-04-09 16:39:50 +03:00
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
HttpServer::new(move || App::new()
|
2019-12-20 22:11:38 +06:00
|
|
|
// cookie session middleware
|
2021-04-09 16:39:50 +03:00
|
|
|
.wrap(RedisSession::new("127.0.0.1:6379", &[0; 32]))
|
|
|
|
// enable logger
|
|
|
|
.wrap(Logger::default())
|
2019-12-20 22:11:38 +06:00
|
|
|
// register simple route, handle all methods
|
2021-04-09 16:39:50 +03:00
|
|
|
.service(resource("/").route(get().to(index)))
|
2019-12-20 22:11:38 +06:00
|
|
|
)
|
2021-04-09 16:39:50 +03:00
|
|
|
.bind("127.0.0.1:8080")?
|
|
|
|
.run()
|
2019-12-20 22:11:38 +06:00
|
|
|
.await
|
2017-12-29 01:10:27 -08:00
|
|
|
}
|
|
|
|
```
|