From aff38dc15bf598db821457910076ae130bca1030 Mon Sep 17 00:00:00 2001 From: dowwie Date: Sat, 11 Aug 2018 09:59:07 -0400 Subject: [PATCH] added delete operations --- actix_redis/README.md | 5 +++-- actix_redis/src/main.rs | 24 ++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/actix_redis/README.md b/actix_redis/README.md index bd0c9879..da1e2bbf 100644 --- a/actix_redis/README.md +++ b/actix_redis/README.md @@ -4,11 +4,12 @@ is so can be read at https://github.com/benashford/redis-async-rs/issues/19#issu -To test the demo, POST a json object containing three strings to the /cache_stuff endpoint: +To test the demo, POST a json object containing three strings to the /stuff endpoint: {"one": "first entry", "two": "second entry", "three": "third entry" } - These three entries will cache to redis, keyed accordingly. + +to delete these, simply issue a DELETE http request to /stuff endpoint diff --git a/actix_redis/src/main.rs b/actix_redis/src/main.rs index 9134666d..d9ccfa8e 100644 --- a/actix_redis/src/main.rs +++ b/actix_redis/src/main.rs @@ -59,6 +59,22 @@ fn cache_stuff((info, req): (Json, HttpRequest)) .responder() } +fn del_stuff(req: HttpRequest) + -> impl Future { + let redis = req.state().redis_addr.clone(); + + redis.send(Command(resp_array!["DEL", "mydomain:one", "mydomain:two", "mydomain:three"])) + .map_err(AWError::from) + .and_then(|res: Result| + match &res { + Ok(RespValue::Integer(x)) if x==&3 => + Ok(HttpResponse::Ok().body("successfully deleted values")), + _ =>{println!("---->{:?}", res); + Ok(HttpResponse::InternalServerError().finish())} + }) + .responder() + +} pub struct AppState { pub redis_addr: Arc> @@ -75,8 +91,12 @@ fn main() { App::with_state(app_state) .middleware(middleware::Logger::default()) - .resource("/cache_stuff", |r| r.method(Method::POST) - .with_async(cache_stuff)) + .resource("/stuff", |r| { + r.method(Method::POST) + .with_async(cache_stuff); + r.method(Method::DELETE) + .with_async(del_stuff)}) + }).bind("0.0.0.0:8080") .unwrap() .workers(1)