Skip to content

Commit

Permalink
chore: update db-redis redis dep
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Dec 28, 2024
1 parent f6955a2 commit 0633fd4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 36 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"reqwest",
"rustls",
"rustup",
"serde",
"sparklepost",
"sparkpost",
"sqlx",
Expand Down
33 changes: 5 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions databases/redis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 21 additions & 5 deletions databases/redis/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)?;

Expand All @@ -43,15 +57,15 @@ 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)?;

// not strictly necessary, but successful DEL operations return the number of keys deleted
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())
}
}
Expand All @@ -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))?
Expand Down

0 comments on commit 0633fd4

Please sign in to comment.