1
0
mirror of https://github.com/actix/examples synced 2024-11-30 17:14:35 +01:00

added delete operations

This commit is contained in:
dowwie 2018-08-11 09:59:07 -04:00
parent 7ccde8c2d3
commit aff38dc15b
2 changed files with 25 additions and 4 deletions

View File

@ -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", {"one": "first entry",
"two": "second entry", "two": "second entry",
"three": "third entry" } "three": "third entry" }
These three entries will cache to redis, keyed accordingly. These three entries will cache to redis, keyed accordingly.
to delete these, simply issue a DELETE http request to /stuff endpoint

View File

@ -59,6 +59,22 @@ fn cache_stuff((info, req): (Json<CacheInfo>, HttpRequest<AppState>))
.responder() .responder()
} }
fn del_stuff(req: HttpRequest<AppState>)
-> impl Future<Item=HttpResponse, Error=AWError> {
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<RespValue, ARError>|
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 struct AppState {
pub redis_addr: Arc<Addr<RedisActor>> pub redis_addr: Arc<Addr<RedisActor>>
@ -75,8 +91,12 @@ fn main() {
App::with_state(app_state) App::with_state(app_state)
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.resource("/cache_stuff", |r| r.method(Method::POST) .resource("/stuff", |r| {
.with_async(cache_stuff)) r.method(Method::POST)
.with_async(cache_stuff);
r.method(Method::DELETE)
.with_async(del_stuff)})
}).bind("0.0.0.0:8080") }).bind("0.0.0.0:8080")
.unwrap() .unwrap()
.workers(1) .workers(1)