Skip to content

Commit bfe7412

Browse files
committed
Rename cache(int) to cacheWithCapacityHint(int)
The parameter is a capacity hint, but more frequently confused with a buffer size like replay(int) than it is correctly understood. It also offers no guarantees, only the weak hope of optimization. This change renames the method, deprecating the old name. It also adds javadoc calling out that the parameter is not a bound and referencing replay(int).autoConnect() as a way to achieve that behavior.
1 parent f60f8ce commit bfe7412

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/main/java/rx/Observable.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3664,6 +3664,15 @@ public final Observable<T> cache() {
36643664
return CachedObservable.from(this);
36653665
}
36663666

3667+
/**
3668+
* @see #cacheWithInitialCapacity(int)
3669+
* @deprecated Use {@link #cacheWithInitialCapacity(int)} instead.
3670+
*/
3671+
@Deprecated
3672+
public final Observable<T> cache(int initialCapacity) {
3673+
return cacheWithInitialCapacity(initialCapacity);
3674+
}
3675+
36673676
/**
36683677
* Caches emissions from the source Observable and replays them in order to any subsequent Subscribers.
36693678
* This method has similar behavior to {@link #replay} except that this auto-subscribes to the source
@@ -3689,14 +3698,17 @@ public final Observable<T> cache() {
36893698
* <dt><b>Scheduler:</b></dt>
36903699
* <dd>{@code cache} does not operate by default on a particular {@link Scheduler}.</dd>
36913700
* </dl>
3701+
* <p>
3702+
* <em>Note:</em> The capacity hint is not an upper bound on cache size. For that, consider
3703+
* {@link #replay(int)} in combination with {@link ConnectableObservable#autoConnect()} or similar.
36923704
*
3693-
* @param capacityHint hint for number of items to cache (for optimizing underlying data structure)
3705+
* @param initialCapacity hint for number of items to cache (for optimizing underlying data structure)
36943706
* @return an Observable that, when first subscribed to, caches all of its items and notifications for the
36953707
* benefit of subsequent subscribers
36963708
* @see <a href="http://reactivex.io/documentation/operators/replay.html">ReactiveX operators documentation: Replay</a>
36973709
*/
3698-
public final Observable<T> cache(int capacityHint) {
3699-
return CachedObservable.from(this, capacityHint);
3710+
public final Observable<T> cacheWithInitialCapacity(int initialCapacity) {
3711+
return CachedObservable.from(this, initialCapacity);
37003712
}
37013713

37023714
/**

src/test/java/rx/ObservableTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ public void run() {
663663
}
664664
}).start();
665665
}
666-
}).cache(1);
666+
}).cacheWithInitialCapacity(1);
667667

668668
// we then expect the following 2 subscriptions to get that same value
669669
final CountDownLatch latch = new CountDownLatch(2);

0 commit comments

Comments
 (0)