I am trying to figure out the best way to establish a Redis Pool and then make calls to Redis from within a Spray route. I want to make sure that I can use the connection pool for Redis connections. What would be the best way to instantiate the pool and use it within my spray routes? Is there a better way to establish a "global" pool that can be used? Should I create an actor instead and use that to make the redis calls? I am obviously a 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 that ends up calling a function that uses the 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 was $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)
}
}