From b1f9fd39f50a8fb8bf18625e3bab6f616a859bb0 Mon Sep 17 00:00:00 2001 From: Ruban Monu Date: Mon, 8 Dec 2014 11:13:10 -0800 Subject: [PATCH] Read through store - do not query backing store when no cache miss --- .../twitter/storehaus/ReadThroughStore.scala | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/storehaus-core/src/main/scala/com/twitter/storehaus/ReadThroughStore.scala b/storehaus-core/src/main/scala/com/twitter/storehaus/ReadThroughStore.scala index 874d2b6d..cbd9646d 100644 --- a/storehaus-core/src/main/scala/com/twitter/storehaus/ReadThroughStore.scala +++ b/storehaus-core/src/main/scala/com/twitter/storehaus/ReadThroughStore.scala @@ -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 } + } } } }