librelist archives

« back to archive

Basic usage question: queries

Basic usage question: queries

From:
Ankur Mathur
Date:
2014-05-09 @ 14:08
Assuming BrandoTest exemplifies the typical usage from an Actor, how do 
you correlate the response to , say a "get" request?

I see that the order of requests is maintained, so would the client actor 
have to maintain a queue? Any examples of doing this?

Re: [brando] Basic usage question: queries

From:
Chris Dinn
Date:
2014-05-09 @ 14:55
A queue would definitely work, though most often I use the ask pattern.

For instance:

  def getName(id: String): Future[String] = {
    val request = redis ? Request("GET", id + ":name")

    request.mapTo[Option[ByteString]].map { resp ⇒
      resp.getOrElse(ByteString.empty).utf8String
    }
  }

In this case I'm casting the response, since I know to expect a string in
response to a request for a user's name. Alternatively you can use the
response extractors to pattern match on the result. That will recover the
necessary type information and allow you to take action on multiple
possible response types.



On Fri, May 9, 2014 at 10:08 AM, Ankur Mathur <ankur.mathur@nurun.com>wrote:

> Assuming BrandoTest exemplifies the typical usage from an Actor, how do
> you correlate the response to , say a “get” request?
>
>
>
> I see that the order of requests is maintained, so would the client actor
> have to maintain a queue? Any examples of doing this?
>

Re: [brando] Basic usage question: queries

From:
Ankur Mathur
Date:
2014-05-09 @ 15:49
That makes sense – thanks!

From: brando@librelist.com [mailto:brando@librelist.com] On Behalf Of Chris Dinn
Sent: May-09-14 10:56 AM
To: brando@librelist.com
Subject: Re: [brando] Basic usage question: queries

A queue would definitely work, though most often I use the ask pattern.

For instance:

  def getName(id: String): Future[String] = {
    val request = redis ? Request("GET", id + ":name")

    request.mapTo[Option[ByteString]].map { resp ⇒
      resp.getOrElse(ByteString.empty).utf8String
    }
  }

In this case I'm casting the response, since I know to expect a string in 
response to a request for a user's name. Alternatively you can use the 
response extractors to pattern match on the result. That will recover the 
necessary type information and allow you to take action on multiple 
possible response types.


On Fri, May 9, 2014 at 10:08 AM, Ankur Mathur 
<ankur.mathur@nurun.com<mailto:ankur.mathur@nurun.com>> wrote:
Assuming BrandoTest exemplifies the typical usage from an Actor, how do 
you correlate the response to , say a “get” request?

I see that the order of requests is maintained, so would the client actor 
have to maintain a queue? Any examples of doing this?