Skip to content

Commit f29d7e9

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 e802bb7 commit f29d7e9

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/main/java/rx/Observable.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3641,6 +3641,14 @@ public final Observable<T> cache() {
36413641
return CachedObservable.from(this);
36423642
}
36433643

3644+
/**
3645+
* @see #cacheWithCapacityHint(int)
3646+
* @deprecated Use {@link #cacheWithCapacityHint(int)} instead.
3647+
*/
3648+
@Deprecated public final Observable<T> cache(int capacityHint) {
3649+
return cacheWithCapacityHint(capacityHint);
3650+
}
3651+
36443652
/**
36453653
* Caches emissions from the source Observable and replays them in order to any subsequent Subscribers.
36463654
* This method has similar behavior to {@link #replay} except that this auto-subscribes to the source
@@ -3666,13 +3674,16 @@ public final Observable<T> cache() {
36663674
* <dt><b>Scheduler:</b></dt>
36673675
* <dd>{@code cache} does not operate by default on a particular {@link Scheduler}.</dd>
36683676
* </dl>
3677+
* <p>
3678+
* <em>Note:</em> The capacity hint is not an upper bound on cache size. For that, consider
3679+
* {@link #replay(int)} in combination with {@link ConnectableObservable#autoConnect()} or similar.
36693680
*
36703681
* @param capacityHint hint for number of items to cache (for optimizing underlying data structure)
36713682
* @return an Observable that, when first subscribed to, caches all of its items and notifications for the
36723683
* benefit of subsequent subscribers
36733684
* @see <a href="http://reactivex.io/documentation/operators/replay.html">ReactiveX operators documentation: Replay</a>
36743685
*/
3675-
public final Observable<T> cache(int capacityHint) {
3686+
public final Observable<T> cacheWithCapacityHint(int capacityHint) {
36763687
return CachedObservable.from(this, capacityHint);
36773688
}
36783689

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+
}).cacheWithCapacityHint(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)