1
0
mirror of https://github.com/actix/examples synced 2025-01-22 14:05:55 +01:00

chore: update db-redis redis dep

This commit is contained in:
Rob Ede 2024-12-28 20:25:06 +00:00
parent f6955a278a
commit 0633fd447b
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
5 changed files with 30 additions and 36 deletions

View File

@ -22,6 +22,7 @@
"reqwest", "reqwest",
"rustls", "rustls",
"rustup", "rustup",
"serde",
"sparklepost", "sparklepost",
"sparkpost", "sparkpost",
"sqlx", "sqlx",

33
Cargo.lock generated
View File

@ -1753,7 +1753,7 @@ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
"cexpr", "cexpr",
"clang-sys", "clang-sys",
"itertools 0.13.0", "itertools 0.12.1",
"proc-macro2", "proc-macro2",
"quote", "quote",
"regex", "regex",
@ -2586,9 +2586,9 @@ version = "1.0.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"env_logger", "env_logger",
"log", "redis 0.27.6",
"redis 0.25.4",
"serde", "serde",
"tracing",
] ]
[[package]] [[package]]
@ -6045,7 +6045,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9552f850d5f0964a4e4d0bf306459ac29323ddfbae05e35a7c0d35cb0803cc5" checksum = "e9552f850d5f0964a4e4d0bf306459ac29323ddfbae05e35a7c0d35cb0803cc5"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"itertools 0.13.0", "itertools 0.12.1",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.90", "syn 2.0.90",
@ -6226,30 +6226,6 @@ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
] ]
[[package]]
name = "redis"
version = "0.25.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0d7a6955c7511f60f3ba9e86c6d02b3c3f144f8c24b288d1f4e18074ab8bbec"
dependencies = [
"arc-swap",
"async-trait",
"bytes",
"combine",
"futures",
"futures-util",
"itoa",
"percent-encoding",
"pin-project-lite",
"ryu",
"sha1_smol",
"socket2",
"tokio",
"tokio-retry",
"tokio-util",
"url",
]
[[package]] [[package]]
name = "redis" name = "redis"
version = "0.26.1" version = "0.26.1"
@ -6293,6 +6269,7 @@ dependencies = [
"pin-project-lite", "pin-project-lite",
"ryu", "ryu",
"sha1_smol", "sha1_smol",
"socket2",
"tokio", "tokio",
"tokio-util", "tokio-util",
"url", "url",

View File

@ -109,6 +109,7 @@ openssl = { version = "0.10.60", features = ["v110"] }
parking_lot = "0.12" parking_lot = "0.12"
pin-project-lite = "0.2" pin-project-lite = "0.2"
rand = "0.8" rand = "0.8"
redis = { version = "0.27" }
reqwest = { version = "0.12", features = ["json", "stream"] } reqwest = { version = "0.12", features = ["json", "stream"] }
rustls = "0.23" rustls = "0.23"
rustls-pemfile = "2" rustls-pemfile = "2"

View File

@ -5,8 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
actix-web.workspace = true actix-web.workspace = true
env_logger.workspace = true env_logger.workspace = true
log.workspace = true redis = { workspace = true, features = ["tokio-comp", "connection-manager"] }
redis = { version = "0.25", features = ["tokio-comp", "connection-manager"] }
serde.workspace = true serde.workspace = true
tracing.workspace = true

View File

@ -3,6 +3,20 @@ use std::io;
use actix_web::{error, middleware, web, App, HttpResponse, HttpServer, Responder}; use actix_web::{error, middleware, web, App, HttpResponse, HttpServer, Responder};
use serde::Deserialize; use serde::Deserialize;
async fn get_from_cache(redis: web::Data<redis::Client>) -> actix_web::Result<impl Responder> {
let mut conn = redis
.get_connection_manager()
.await
.map_err(error::ErrorInternalServerError)?;
let res = redis::Cmd::mget(&["my_domain:one", "my_domain:two", "my_domain:three"])
.query_async::<Vec<String>>(&mut conn)
.await
.map_err(error::ErrorInternalServerError)?;
Ok(HttpResponse::Ok().json(res))
}
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct CacheInfo { pub struct CacheInfo {
one: String, one: String,
@ -24,7 +38,7 @@ async fn cache_stuff(
("my_domain:two", info.two), ("my_domain:two", info.two),
("my_domain:three", info.three), ("my_domain:three", info.three),
]) ])
.query_async::<_, String>(&mut conn) .query_async::<String>(&mut conn)
.await .await
.map_err(error::ErrorInternalServerError)?; .map_err(error::ErrorInternalServerError)?;
@ -43,7 +57,7 @@ async fn del_stuff(redis: web::Data<redis::Client>) -> actix_web::Result<impl Re
.map_err(error::ErrorInternalServerError)?; .map_err(error::ErrorInternalServerError)?;
let res = redis::Cmd::del(&["my_domain:one", "my_domain:two", "my_domain:three"]) let res = redis::Cmd::del(&["my_domain:one", "my_domain:two", "my_domain:three"])
.query_async::<_, usize>(&mut conn) .query_async::<usize>(&mut conn)
.await .await
.map_err(error::ErrorInternalServerError)?; .map_err(error::ErrorInternalServerError)?;
@ -51,7 +65,7 @@ async fn del_stuff(redis: web::Data<redis::Client>) -> actix_web::Result<impl Re
if res == 3 { if res == 3 {
Ok(HttpResponse::Ok().body("successfully deleted values")) Ok(HttpResponse::Ok().body("successfully deleted values"))
} else { } else {
log::error!("deleted {res} keys"); tracing::error!("deleted {res} keys");
Ok(HttpResponse::InternalServerError().finish()) Ok(HttpResponse::InternalServerError().finish())
} }
} }
@ -60,19 +74,21 @@ async fn del_stuff(redis: web::Data<redis::Client>) -> actix_web::Result<impl Re
async fn main() -> 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"); tracing::info!("starting HTTP server at http://localhost:8080");
let redis = redis::Client::open("redis://127.0.0.1:6379").unwrap(); let redis = redis::Client::open("redis://127.0.0.1:6379").unwrap();
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.app_data(web::Data::new(redis.clone())) .app_data(web::Data::new(redis.clone()))
.wrap(middleware::Logger::default())
.service( .service(
web::resource("/stuff") web::resource("/stuff")
.route(web::get().to(get_from_cache))
.route(web::post().to(cache_stuff)) .route(web::post().to(cache_stuff))
.route(web::delete().to(del_stuff)), .route(web::delete().to(del_stuff)),
) )
.wrap(middleware::NormalizePath::trim())
.wrap(middleware::Logger::default())
}) })
.workers(2) .workers(2)
.bind(("127.0.0.1", 8080))? .bind(("127.0.0.1", 8080))?