src/main/java/io/reactivex/Observable.java
@CheckReturnValue
    @SchedulerSupport(SchedulerSupport.NONE)
    public final Observable<T> cacheWithInitialCapacity(int initialCapacity) {
        ObjectHelper.verifyPositive(initialCapacity, "initialCapacity");
        return RxJavaPlugins.onAssembly(new ObservableCache<T>(this, initialCapacity));
    }
@CheckReturnValue
    @SchedulerSupport(SchedulerSupport.NONE)
    public final Observable<T> cacheWithInitialCapacity(int initialCapacity) {
        ObjectHelper.verifyPositive(initialCapacity, "initialCapacity");
        return RxJavaPlugins.onAssembly(new ObservableCache<T>(this, initialCapacity));
    }
Returns an Observable that subscribes to this ObservableSource lazily, caches all of its events
and replays them, in the same order as received, to all the downstream subscribers.
<p>
<img width="640" height="410" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/cacheWithInitialCapacity.o.png" alt="">
<p>
This is useful when you want an ObservableSource to cache responses and you can't control the
subscribe/dispose behavior of all the {@link Observer}s.
<p>
The operator subscribes only when the first downstream subscriber subscribes and maintains
a single subscription towards this ObservableSource. In contrast, the operator family of {@link #replay()}
that return a {@link ConnectableObservable} require an explicit call to {@link ConnectableObservable#connect()}.
<p>
<em>Note:</em> You sacrifice the ability to dispose the origin when you use the {@code cache}
Observer so be careful not to use this Observer on ObservableSources that emit an infinite or very large number
of items that will use up memory.
A possible workaround is to apply `takeUntil` with a predicate or
another source before (and perhaps after) the application of cache().
<pre><code>
AtomicBoolean shouldStop = new AtomicBoolean();

source.takeUntil(v -&gt; shouldStop.get())
.cache()
.takeUntil(v -&gt; shouldStop.get())
.subscribe(...);
</code></pre>
Since the operator doesn't allow clearing the cached values either, the possible workaround is
to forget all references to it via {@link #onTerminateDetach()} applied along with the previous
workaround:
<pre><code>
AtomicBoolean shouldStop = new AtomicBoolean();

source.takeUntil(v -&gt; shouldStop.get())
.onTerminateDetach()
.cache()
.takeUntil(v -&gt; shouldStop.get())
.onTerminateDetach()
.subscribe(...);
</code></pre>
<dl>
<dt><b>Scheduler:</b></dt>
<dd>{@code cacheWithInitialCapacity} does not operate by default on a particular {@link Scheduler}.</dd>
</dl>
<p>
<em>Note:</em> The capacity hint is not an upper bound on cache size. For that, consider
{@link #replay(int)} in combination with {@link ConnectableObservable#autoConnect()} or similar.

@param initialCapacity hint for number of items to cache (for optimizing underlying data structure)
@return an Observable that, when first subscribed to, caches all of its items and notifications for the
benefit of subsequent subscribers
@see <a href="http://reactivex.io/documentation/operators/replay.html">ReactiveX operators documentation: Replay</a>