Skip to content

Commit

Permalink
Read through store - do not query backing store when no cache miss
Browse files Browse the repository at this point in the history
  • Loading branch information
rubanm committed Dec 8, 2014
1 parent 9ac82a0 commit b1f9fd3
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ class ReadThroughStore[K, V](backingStore: ReadableStore[K, V], cache: Store[K,
val hits = responses.filter { !_._2.isEmpty }
val missedKeys = responses.filter { _._2.isEmpty }.keySet

FutureOps.mapCollect(backingStore.multiGet(missedKeys ++ failedKeys)).flatMap { storeResult =>
// write fetched keys to cache, best effort
mutex.acquire.flatMap { p =>
FutureOps.mapCollect(cache.multiPut(storeResult))(FutureCollector.bestEffort[(K1, Unit)])
.map { u => hits ++ storeResult }
.ensure { p.release }
val remaining = missedKeys ++ failedKeys
if (remaining.isEmpty) {
Future.value(hits) // no cache misses
} else {
FutureOps.mapCollect(backingStore.multiGet(remaining)).flatMap { storeResult =>
// write fetched keys to cache, best effort
mutex.acquire.flatMap { p =>
FutureOps.mapCollect(cache.multiPut(storeResult))(FutureCollector.bestEffort[(K1, Unit)])
.map { u => hits ++ storeResult }
.ensure { p.release }
}
}
}
}
Expand Down

0 comments on commit b1f9fd3

Please sign in to comment.