1
0
mirror of https://github.com/actix/examples synced 2025-01-22 05:55:56 +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",
"rustls",
"rustup",
"serde",
"sparklepost",
"sparkpost",
"sqlx",

33
Cargo.lock generated
View File

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

View File

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

View File

@ -5,8 +5,7 @@ edition = "2021"
[dependencies]
actix-web.workspace = true
env_logger.workspace = true
log.workspace = true
redis = { version = "0.25", features = ["tokio-comp", "connection-manager"] }
redis = { workspace = true, features = ["tokio-comp", "connection-manager"] }
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 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)]
pub struct CacheInfo {
one: String,
@ -24,7 +38,7 @@ async fn cache_stuff(
("my_domain:two", info.two),
("my_domain:three", info.three),
])
.query_async::<_, String>(&mut conn)
.query_async::<String>(&mut conn)
.await
.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)?;
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
.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 {
Ok(HttpResponse::Ok().body("successfully deleted values"))
} else {
log::error!("deleted {res} keys");
tracing::error!("deleted {res} keys");
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<()> {
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();
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(redis.clone()))
.wrap(middleware::Logger::default())
.service(
web::resource("/stuff")
.route(web::get().to(get_from_cache))
.route(web::post().to(cache_stuff))
.route(web::delete().to(del_stuff)),
)
.wrap(middleware::NormalizePath::trim())
.wrap(middleware::Logger::default())
})
.workers(2)
.bind(("127.0.0.1", 8080))?