Clump多源数据组合
Clump 是一个 Scala 库,通过一个优雅和高效的方式将来自多个源的数据组合在一起。
例如:
import io.getclump.Clump // Creates sources using the batched interfaces val tracksSource = Clump.source(tracksService.fetch _)(_.id) val usersSource = Clump.source(usersService.fetch _)(_.id) def renderTrackPosts(userId: Long) = { // Defines the clump val clump: Clump[List[EnrichedTrack]] = enrichedTrackPosts(userId) // Triggers execution val future: Future[Option[List[EnrichedTrack]]] = clump.get // Renders the response future.map { case Some(trackPosts) => render.json(trackPosts) case None => render.notFound } } // Composes a clump with the user's track posts def enrichedTrackPosts(userId: Long) = for { trackPosts <- Clump.future(timelineService.fetchTrackPosts(userId)) enrichedTracks <- Clump.traverse(trackPosts)(enrichedTrack(_)) } yield { enrichedTracks } // Composes an enriched track clump def enrichedTrack(trackId: Long) = for { track <- tracksSource.get(trackId) creator <- usersSource.get(track.creatorId) } yield { new EnrichedTrack(track, creator) }
评论