Source:
<code>public Sequence<T> run(Query<T> query) { final CacheStrategy strategy = toolChest.getCacheStrategy(query); final boolean populateCache = Boolean.parseBoolean(query.getContextValue(CacheConfig.POPULATE_CACHE, "true")) && strategy != null && cacheConfig.isPopulateCache(); final boolean useCache = Boolean.parseBoolean(query.getContextValue(CacheConfig.USE_CACHE, "true")) && strategy != null && cacheConfig.isPopulateCache(); final Cache.NamedKey key = CacheUtil.computeSegmentCacheKey( segmentIdentifier, segmentDescriptor, strategy.computeCacheKey(query) ); if(useCache) { final Function cacheFn = strategy.pullFromCache(); <START> final byte[] cachedResult = cache.get(key); <END> if(cachedResult != null) { final TypeReference cacheObjectClazz = strategy.getCacheObjectClazz(); return Sequences.map( new BaseSequence<>( new BaseSequence.IteratorMaker<T, Iterator<T>>() { @Override public Iterator<T> make() { try { if (cachedResult.length == 0) { return Iterators.emptyIterator(); } return mapper.readValues( mapper.getFactory().createParser(cachedResult), cacheObjectClazz ); } catch (IOException e) { throw Throwables.propagate(e); } } @Override public void cleanup(Iterator<T> iterFromMake) { } } ), cacheFn ); } } if (populateCache) { final Function cacheFn = strategy.prepareForCache(); final List<Object> cacheResults = Lists.newLinkedList(); return Sequences.withEffect( Sequences.map( base.run(query), new Function<T, T>() { @Nullable @Override public T apply(@Nullable T input) { cacheResults.add(cacheFn.apply(input)); return input; } } ), new Runnable() { @Override public void run() { CacheUtil.populate(cache, mapper, key, cacheResults); } }, MoreExecutors.sameThreadExecutor() ); } else { return base.run(query); } }</code><technical_language>wondering if move this a higher level in hierarchy do a bulkGet for segments in trip cache done broker level</technical_language>

Target:
public Sequence<T> run(Query<T> query) { final CacheStrategy strategy = toolChest.getCacheStrategy(query); final boolean populateCache = query.getContextPopulateCache(true) && strategy != null && cacheConfig.isPopulateCache(); final boolean useCache = query.getContextUseCache(true) && strategy != null && cacheConfig.isPopulateCache(); final Cache.NamedKey key = CacheUtil.computeSegmentCacheKey( segmentIdentifier, segmentDescriptor, strategy.computeCacheKey(query) ); if(useCache) { final Function cacheFn = strategy.pullFromCache(); final byte[] cachedResult = cache.get(key); if(cachedResult != null) { final TypeReference cacheObjectClazz = strategy.getCacheObjectClazz(); return Sequences.map( new BaseSequence<>( new BaseSequence.IteratorMaker<T, Iterator<T>>() { @Override public Iterator<T> make() { try { if (cachedResult.length == 0) { return Iterators.emptyIterator(); } return mapper.readValues( mapper.getFactory().createParser(cachedResult), cacheObjectClazz ); } catch (IOException e) { throw Throwables.propagate(e); } } @Override public void cleanup(Iterator<T> iterFromMake) { } } ), cacheFn ); } } if (populateCache) { final Function cacheFn = strategy.prepareForCache(); final List<Object> cacheResults = Lists.newLinkedList(); return Sequences.withEffect( Sequences.map( base.run(query), new Function<T, T>() { @Override public T apply(T input) { cacheResults.add(cacheFn.apply(input)); return input; } } ), new Runnable() { @Override public void run() { CacheUtil.populate(cache, mapper, key, cacheResults); } }, MoreExecutors.sameThreadExecutor() ); } else { return base.run(query); } }