scala - Call Redis (or other db) from within Spray Route -
i trying figure out best way establish redis pool , make calls redis within spray route. want make sure can use connection pool redis connections. best way instantiate pool , use within spray routes? there better way establish "global" pool can used? should create actor instead , use make redis calls? bit ignorant here.
crude redis client:
object redisclient { val pool = new jedispool(new jedispoolconfig(), "localhost") def getvalue(key: string): string= { try{ val jedis = pool.getresource() //returns redis value jedis.get(key) } } }
route ends calling function uses redis client
trait demoservice extends httpservice { val messageapirouting = path("summary" / segment / segment) { (dataset, timeslice) => oncomplete(getsummary(dataset, timeslice)) { case success(value) => complete(s"the result $value") case failure(ex) => complete(s"an error occurred: ${ex.getmessage}") } } def getsummary(dataset: string, timeslice: string): future[string] = future { val key = dataset + timeslice redisclient.getvalue(key) } }
as far know jedis client not non-blocking , async. may not benefits of using spray if use blocking client. suggest looking @ rediscala.
second delegate actual interaction actor has redisclient interacting redis instance/cluster.
finally, can complete
spray route giving future
. means entire pipeline async , non-blocking.
note: redis still single threaded , don't think there anyway around afaik.
in general, should use reactive driver if possible (e.g., slick, reactivemongo )
Comments
Post a Comment