1
0
mirror of https://github.com/actix/examples synced 2025-02-17 07:23:29 +01:00

update redis example to v0.22

This commit is contained in:
Rob Ede 2022-10-16 19:14:40 +01:00
parent 99d0afde28
commit fa323545e9
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
4 changed files with 87 additions and 215 deletions

205
Cargo.lock generated
View File

@ -29,30 +29,6 @@ dependencies = [
"ureq", "ureq",
] ]
[[package]]
name = "actix"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3720d0064a0ce5c0de7bd93bdb0a6caebab2a9b5668746145d7b3b0c5da02914"
dependencies = [
"actix-rt",
"actix_derive",
"bitflags",
"bytes 1.2.1",
"crossbeam-channel",
"futures-core",
"futures-sink",
"futures-task",
"futures-util",
"log",
"once_cell",
"parking_lot 0.11.2",
"pin-project-lite 0.2.9",
"smallvec",
"tokio 1.21.0",
"tokio-util 0.6.10",
]
[[package]] [[package]]
name = "actix" name = "actix"
version = "0.13.0" version = "0.13.0"
@ -83,7 +59,7 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e089b5c4d0cf02d72a9eca0b6c80aebff1ccd3d1e99c83d1f7b9baeff39dc5e9" checksum = "e089b5c4d0cf02d72a9eca0b6c80aebff1ccd3d1e99c83d1f7b9baeff39dc5e9"
dependencies = [ dependencies = [
"actix 0.13.0", "actix",
"ahash 0.7.6", "ahash 0.7.6",
"log", "log",
] ]
@ -272,48 +248,22 @@ dependencies = [
"prost", "prost",
] ]
[[package]]
name = "actix-redis"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8e10d7a6b71f5bfbc47ccbff55d89387e15087fd9583bdfdc47e9f0c1d2a9a8"
dependencies = [
"actix 0.12.0",
"actix-rt",
"actix-service",
"actix-session 0.5.0",
"actix-tls",
"actix-web",
"backoff 0.2.1",
"derive_more",
"futures-core",
"log",
"rand 0.8.5",
"redis 0.19.0",
"redis-async 0.8.1",
"serde",
"serde_json",
"time 0.3.14",
"tokio 1.21.0",
"tokio-util 0.6.10",
]
[[package]] [[package]]
name = "actix-redis" name = "actix-redis"
version = "0.12.0" version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38446dc11c743f4f0023b1067c7cfb8f2548f24418e31a193b324e35fa059279" checksum = "38446dc11c743f4f0023b1067c7cfb8f2548f24418e31a193b324e35fa059279"
dependencies = [ dependencies = [
"actix 0.13.0", "actix",
"actix-rt", "actix-rt",
"actix-service", "actix-service",
"actix-tls", "actix-tls",
"actix-web", "actix-web",
"backoff 0.4.0", "backoff",
"derive_more", "derive_more",
"futures-core", "futures-core",
"log", "log",
"redis-async 0.13.0", "redis-async",
"time 0.3.14", "time 0.3.14",
"tokio 1.21.0", "tokio 1.21.0",
"tokio-util 0.7.4", "tokio-util 0.7.4",
@ -373,31 +323,14 @@ dependencies = [
"pin-project-lite 0.2.9", "pin-project-lite 0.2.9",
] ]
[[package]]
name = "actix-session"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53b253be4da7f0a778831d0c8b0c2de4ce8ea30f3b1d14d11843a296e53d21db"
dependencies = [
"actix-service",
"actix-utils",
"actix-web",
"derive_more",
"futures-util",
"log",
"serde",
"serde_json",
"time 0.3.14",
]
[[package]] [[package]]
name = "actix-session" name = "actix-session"
version = "0.7.1" version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d324d2a6e670f8746ae64f333c2c0fe856bdf624bcb72fb56e250e62a7e9a85f" checksum = "d324d2a6e670f8746ae64f333c2c0fe856bdf624bcb72fb56e250e62a7e9a85f"
dependencies = [ dependencies = [
"actix 0.13.0", "actix",
"actix-redis 0.12.0", "actix-redis",
"actix-service", "actix-service",
"actix-utils", "actix-utils",
"actix-web", "actix-web",
@ -512,7 +445,7 @@ version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31efe7896f3933ce03dd4710be560254272334bb321a18fd8ff62b1a557d9d19" checksum = "31efe7896f3933ce03dd4710be560254272334bb321a18fd8ff62b1a557d9d19"
dependencies = [ dependencies = [
"actix 0.13.0", "actix",
"actix-codec", "actix-codec",
"actix-http", "actix-http",
"actix-web", "actix-web",
@ -659,20 +592,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "actix_redis"
version = "1.0.0"
dependencies = [
"actix 0.12.0",
"actix-redis 0.10.0",
"actix-web",
"env_logger 0.9.0",
"futures-util",
"log",
"redis-async 0.8.1",
"serde",
]
[[package]] [[package]]
name = "adler" name = "adler"
version = "1.0.2" version = "1.0.2"
@ -926,7 +845,7 @@ version = "4.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61249530b25e497aa9023f76afa13ecbdd0078be2504ea4f4748871ca789bd31" checksum = "61249530b25e497aa9023f76afa13ecbdd0078be2504ea4f4748871ca789bd31"
dependencies = [ dependencies = [
"actix 0.13.0", "actix",
"actix-http", "actix-http",
"actix-web", "actix-web",
"actix-web-actors", "actix-web-actors",
@ -943,7 +862,7 @@ dependencies = [
name = "async-graphql-demo" name = "async-graphql-demo"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix 0.13.0", "actix",
"actix-cors", "actix-cors",
"actix-web", "actix-web",
"actix-web-lab 0.17.0", "actix-web-lab 0.17.0",
@ -1441,16 +1360,6 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[package]]
name = "backoff"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "721c249ab59cbc483ad4294c9ee2671835c1e43e9ffc277e6b4ecfef733cfdc5"
dependencies = [
"instant",
"rand 0.7.3",
]
[[package]] [[package]]
name = "backoff" name = "backoff"
version = "0.4.0" version = "0.4.0"
@ -1479,7 +1388,7 @@ name = "basics"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix-files", "actix-files",
"actix-session 0.7.1", "actix-session",
"actix-web", "actix-web",
"async-stream", "async-stream",
"env_logger 0.9.0", "env_logger 0.9.0",
@ -2002,7 +1911,7 @@ dependencies = [
name = "cookie-session" name = "cookie-session"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix-session 0.7.1", "actix-session",
"actix-web", "actix-web",
"env_logger 0.9.0", "env_logger 0.9.0",
"log", "log",
@ -2194,6 +2103,18 @@ version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ee2393c4a91429dffb4bedf19f4d6abf27d8a732c8ce4980305d782e5426d57" checksum = "3ee2393c4a91429dffb4bedf19f4d6abf27d8a732c8ce4980305d782e5426d57"
[[package]]
name = "db-redis"
version = "1.0.0"
dependencies = [
"actix-web",
"env_logger 0.9.0",
"futures-util",
"log",
"redis 0.22.0",
"serde",
]
[[package]] [[package]]
name = "deadpool" name = "deadpool"
version = "0.9.5" version = "0.9.5"
@ -2396,12 +2317,6 @@ dependencies = [
"dirs", "dirs",
] ]
[[package]]
name = "dtoa"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0"
[[package]] [[package]]
name = "dtoa" name = "dtoa"
version = "1.0.3" version = "1.0.3"
@ -2778,6 +2693,7 @@ checksum = "7f21eda599937fba36daeb58a22e8f5cee2d14c4a17b5b7739c7c8e5e3b8230c"
dependencies = [ dependencies = [
"futures-channel", "futures-channel",
"futures-core", "futures-core",
"futures-executor",
"futures-io", "futures-io",
"futures-sink", "futures-sink",
"futures-task", "futures-task",
@ -2868,6 +2784,7 @@ version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44fb6cb1be61cc1d2e43b262516aafcf63b241cffdb1d3fa115f91d9c7b09c90" checksum = "44fb6cb1be61cc1d2e43b262516aafcf63b241cffdb1d3fa115f91d9c7b09c90"
dependencies = [ dependencies = [
"futures-channel",
"futures-core", "futures-core",
"futures-io", "futures-io",
"futures-macro", "futures-macro",
@ -4943,28 +4860,6 @@ dependencies = [
"rand_core 0.5.1", "rand_core 0.5.1",
] ]
[[package]]
name = "redis"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a6ddfecac9391fed21cce10e83c65fa4abafd77df05c98b1c647c65374ce9b3"
dependencies = [
"async-trait",
"bytes 1.2.1",
"combine 4.6.6",
"dtoa 0.4.8",
"futures-util",
"itoa 0.4.8",
"native-tls",
"percent-encoding",
"pin-project-lite 0.2.9",
"sha1 0.6.1",
"tokio 1.21.0",
"tokio-native-tls",
"tokio-util 0.6.10",
"url",
]
[[package]] [[package]]
name = "redis" name = "redis"
version = "0.21.6" version = "0.21.6"
@ -4985,18 +4880,24 @@ dependencies = [
] ]
[[package]] [[package]]
name = "redis-async" name = "redis"
version = "0.8.1" version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8f5a920ab6d700f442cadd6006a2a43f7ec0a5223065f3eaa517fc3fca6d2b3" checksum = "2e5450e9add55de0ea728a964191669d3707fa20666c5f16753f3a6a51e96231"
dependencies = [ dependencies = [
"arc-swap",
"async-trait",
"bytes 1.2.1", "bytes 1.2.1",
"futures-channel", "combine 4.6.6",
"futures-sink", "futures",
"futures-util", "futures-util",
"log", "itoa 1.0.3",
"percent-encoding",
"pin-project-lite 0.2.9",
"ryu",
"tokio 1.21.0", "tokio 1.21.0",
"tokio-util 0.6.10", "tokio-util 0.7.4",
"url",
] ]
[[package]] [[package]]
@ -5018,7 +4919,7 @@ dependencies = [
name = "redis-session-example" name = "redis-session-example"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix-session 0.7.1", "actix-session",
"actix-test", "actix-test",
"actix-web", "actix-web",
"env_logger 0.9.0", "env_logger 0.9.0",
@ -6464,7 +6365,7 @@ name = "todo"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix-files", "actix-files",
"actix-session 0.7.1", "actix-session",
"actix-web", "actix-web",
"actix-web-lab 0.17.0", "actix-web-lab 0.17.0",
"dotenv", "dotenv",
@ -6649,20 +6550,6 @@ dependencies = [
"tokio 0.2.25", "tokio 0.2.25",
] ]
[[package]]
name = "tokio-util"
version = "0.6.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36943ee01a6d67977dd3f84a5a1d2efeb4ada3a1ae771cadfaa535d9d9fc6507"
dependencies = [
"bytes 1.2.1",
"futures-core",
"futures-sink",
"log",
"pin-project-lite 0.2.9",
"tokio 1.21.0",
]
[[package]] [[package]]
name = "tokio-util" name = "tokio-util"
version = "0.7.4" version = "0.7.4"
@ -7355,7 +7242,7 @@ dependencies = [
name = "websocket" name = "websocket"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix 0.13.0", "actix",
"actix-codec", "actix-codec",
"actix-files", "actix-files",
"actix-rt", "actix-rt",
@ -7373,7 +7260,7 @@ dependencies = [
name = "websocket-autobahn" name = "websocket-autobahn"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix 0.13.0", "actix",
"actix-web", "actix-web",
"actix-web-actors", "actix-web-actors",
"env_logger 0.9.0", "env_logger 0.9.0",
@ -7400,7 +7287,7 @@ dependencies = [
name = "websocket-chat-broker" name = "websocket-chat-broker"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix 0.13.0", "actix",
"actix-broker", "actix-broker",
"actix-files", "actix-files",
"actix-web", "actix-web",
@ -7428,7 +7315,7 @@ dependencies = [
name = "websocket-example" name = "websocket-example"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix 0.13.0", "actix",
"actix-files", "actix-files",
"actix-web", "actix-web",
"actix-web-actors", "actix-web-actors",
@ -7443,7 +7330,7 @@ dependencies = [
name = "websocket-tcp-example" name = "websocket-tcp-example"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix 0.13.0", "actix",
"actix-codec", "actix-codec",
"actix-files", "actix-files",
"actix-web", "actix-web",
@ -7685,7 +7572,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8dfe1ef3558dde14b4be5387bdd41e3bd45746570743521470ec3e9cd0826679" checksum = "8dfe1ef3558dde14b4be5387bdd41e3bd45746570743521470ec3e9cd0826679"
dependencies = [ dependencies = [
"buf-min", "buf-min",
"dtoa 1.0.3", "dtoa",
"itoa 1.0.3", "itoa 1.0.3",
"prettyplease", "prettyplease",
"ryu", "ryu",

View File

@ -1,15 +1,13 @@
[package] [package]
name = "actix_redis" name = "db-redis"
version = "1.0.0" version = "1.0.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
actix = "0.12"
actix-redis = "0.10"
actix-web = "4" actix-web = "4"
env_logger = "0.9" env_logger = "0.9"
futures-util = { version = "0.3.17", default-features = false, features = ["std"] } futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
log = "0.4" log = "0.4"
redis-async = { version = "0.8", default_features = false, features = ["tokio10"] } redis = { version = "0.22", default-features = false, features = ["tokio-comp", "connection-manager"] }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }

View File

@ -1,8 +1,6 @@
use actix::prelude::*; use std::io;
use actix_redis::{Command, RedisActor};
use actix_web::{error, middleware, web, App, HttpResponse, HttpServer}; use actix_web::{error, middleware, web, App, HttpResponse, HttpServer, Responder};
use futures_util::future::try_join_all;
use redis_async::{resp::RespValue, resp_array};
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
@ -13,74 +11,62 @@ pub struct CacheInfo {
} }
async fn cache_stuff( async fn cache_stuff(
info: web::Json<CacheInfo>, web::Json(info): web::Json<CacheInfo>,
redis: web::Data<Addr<RedisActor>>, redis: web::Data<redis::Client>,
) -> actix_web::Result<HttpResponse> { ) -> actix_web::Result<impl Responder> {
let info = info.into_inner(); let mut conn = redis
.get_tokio_connection_manager()
let one = redis.send(Command(resp_array!["SET", "mydomain:one", info.one]));
let two = redis.send(Command(resp_array!["SET", "mydomain:two", info.two]));
let three = redis.send(Command(resp_array!["SET", "mydomain:three", info.three]));
// Asynchronously collects the results of the futures given. The returned future will drive
// execution for all of its underlying futures, collecting the results into a destination
// `Vec<RespValue>` in the same order as they were provided. If any future returns an error then
// all other futures will be canceled and an error will be returned immediately. If all futures
// complete successfully, however, then the returned future will succeed with a `Vec` of all the
// successful results.
let res = try_join_all([one, two, three])
.await .await
.map_err(error::ErrorInternalServerError)? .map_err(error::ErrorInternalServerError)?;
.into_iter()
.map(|item| item.map_err(error::ErrorInternalServerError))
.collect::<Result<Vec<_>, _>>()?;
// successful operations return "OK", so confirm that all returned as so let res = redis::Cmd::set_multiple(&[
if res ("my_domain:one", info.one),
.iter() ("my_domain:two", info.two),
.all(|res| matches!(res, RespValue::SimpleString(x) if x == "OK")) ("my_domain:three", info.three),
{ ])
.query_async::<_, String>(&mut conn)
.await
.map_err(error::ErrorInternalServerError)?;
// not strictly necessary, but successful SET operations return "OK"
if res == "OK" {
Ok(HttpResponse::Ok().body("successfully cached values")) Ok(HttpResponse::Ok().body("successfully cached values"))
} else { } else {
Ok(HttpResponse::InternalServerError().finish()) Ok(HttpResponse::InternalServerError().finish())
} }
} }
async fn del_stuff(redis: web::Data<Addr<RedisActor>>) -> actix_web::Result<HttpResponse> { async fn del_stuff(redis: web::Data<redis::Client>) -> actix_web::Result<impl Responder> {
let res = redis let mut conn = redis
.send(Command(resp_array![ .get_tokio_connection_manager()
"DEL",
"mydomain:one",
"mydomain:two",
"mydomain:three"
]))
.await .await
.map_err(error::ErrorInternalServerError)?
.map_err(error::ErrorInternalServerError)?; .map_err(error::ErrorInternalServerError)?;
match res { let res = redis::Cmd::del(&["my_domain:one", "my_domain:two", "my_domain:three"])
RespValue::Integer(x) if x == 3 => { .query_async::<_, usize>(&mut conn)
Ok(HttpResponse::Ok().body("successfully deleted values")) .await
} .map_err(error::ErrorInternalServerError)?;
_ => { // not strictly necessary, but successful DEL operations return the number of keys deleted
log::error!("{res:?}"); if res == 3 {
Ok(HttpResponse::InternalServerError().finish()) Ok(HttpResponse::Ok().body("successfully deleted values"))
} } else {
log::error!("deleted {res} keys");
Ok(HttpResponse::InternalServerError().finish())
} }
} }
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("starting HTTP server at http://localhost:8080"); log::info!("starting HTTP server at http://localhost:8080");
HttpServer::new(|| { let redis = redis::Client::open("redis://127.0.0.1:6379").unwrap();
let redis_addr = RedisActor::start("127.0.0.1:6379");
HttpServer::new(move || {
App::new() App::new()
.app_data(web::Data::new(redis_addr)) .app_data(web::Data::new(redis.clone()))
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.service( .service(
web::resource("/stuff") web::resource("/stuff")
@ -88,6 +74,7 @@ async fn main() -> std::io::Result<()> {
.route(web::delete().to(del_stuff)), .route(web::delete().to(del_stuff)),
) )
}) })
.workers(2)
.bind(("127.0.0.1", 8080))? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await

View File

@ -231,7 +231,7 @@ where
slice slice
.iter() .iter()
.enumerate() .enumerate()
.map(|(idx, item)| Edge::new(start + idx, (map_to)(*item))), .map(|(idx, item)| Edge::new(start + idx, (map_to)(item))),
); );
Ok::<_, Error>(connection) Ok::<_, Error>(connection)