Problem
I would like to make two parallel executions with the help of futures. One queries the cache and the another one the database. The caller should return the first successful completed future or the database future.
import scala.concurrent.{ExecutionContext, Future}
class UserServiceImpl(
private val cacheRepository: CacheRepository,
private val databaseRepository: DatabaseRepository
)(implicit val executionContext: ExecutionContext) extends UserService {
override def getUserByName(username: String): Future[User] = {
val cacheFuture: Future[User] = Future {
cacheRepository.getUser(username)
}
val databaseFuture: Future[User] = Future {
databaseRepository.getUser(username)
}
// now return the first successful finished Future[User] or the databaseFuture if completed.
}
}