Skip to content

Commit e8dd4ed

Browse files
committed
Revert "cache now supports backpressure"
This reverts commit 18ff5af.
1 parent 5411086 commit e8dd4ed

File tree

7 files changed

+242
-871
lines changed

7 files changed

+242
-871
lines changed

src/main/java/rx/Observable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3594,7 +3594,7 @@ public final <B> Observable<List<T>> buffer(Observable<B> boundary, int initialC
35943594
* @see <a href="http://reactivex.io/documentation/operators/replay.html">ReactiveX operators documentation: Replay</a>
35953595
*/
35963596
public final Observable<T> cache() {
3597-
return CachedObservable.from(this);
3597+
return create(new OnSubscribeCache<T>(this));
35983598
}
35993599

36003600
/**
@@ -3629,7 +3629,7 @@ public final Observable<T> cache() {
36293629
* @see <a href="http://reactivex.io/documentation/operators/replay.html">ReactiveX operators documentation: Replay</a>
36303630
*/
36313631
public final Observable<T> cache(int capacityHint) {
3632-
return CachedObservable.from(this, capacityHint);
3632+
return create(new OnSubscribeCache<T>(this, capacityHint));
36333633
}
36343634

36353635
/**
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.internal.operators;
17+
18+
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
19+
20+
import rx.Observable;
21+
import rx.Observable.OnSubscribe;
22+
import rx.Subscriber;
23+
import rx.subjects.ReplaySubject;
24+
import rx.subjects.Subject;
25+
26+
/**
27+
* This method has similar behavior to {@link Observable#replay()} except that this auto-subscribes
28+
* to the source Observable rather than returning a connectable Observable.
29+
* <p>
30+
* <img width="640" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/cache.png" alt="">
31+
* <p>
32+
* This is useful with an Observable that you want to cache responses when you can't control the
33+
* subscribe/unsubscribe behavior of all the Observers.
34+
* <p>
35+
* <em>Note:</em> You sacrifice the ability to unsubscribe from the origin when you use this operator, so be
36+
* careful not to use this operator on Observables that emit infinite or very large numbers of
37+
* items, as this will use up memory.
38+
*
39+
* @param <T>
40+
* the cached value type
41+
*/
42+
public final class OnSubscribeCache<T> implements OnSubscribe<T> {
43+
protected final Observable<? extends T> source;
44+
protected final Subject<? super T, ? extends T> cache;
45+
volatile int sourceSubscribed;
46+
@SuppressWarnings("rawtypes")
47+
static final AtomicIntegerFieldUpdater<OnSubscribeCache> SRC_SUBSCRIBED_UPDATER
48+
= AtomicIntegerFieldUpdater.newUpdater(OnSubscribeCache.class, "sourceSubscribed");
49+
50+
public OnSubscribeCache(Observable<? extends T> source) {
51+
this(source, ReplaySubject.<T> create());
52+
}
53+
54+
public OnSubscribeCache(Observable<? extends T> source, int capacity) {
55+
this(source, ReplaySubject.<T> create(capacity));
56+
}
57+
58+
/* accessible to tests */OnSubscribeCache(Observable<? extends T> source, Subject<? super T, ? extends T> cache) {
59+
this.source = source;
60+
this.cache = cache;
61+
}
62+
63+
@Override
64+
public void call(Subscriber<? super T> s) {
65+
if (SRC_SUBSCRIBED_UPDATER.compareAndSet(this, 0, 1)) {
66+
source.subscribe(cache);
67+
/*
68+
* Note that we will never unsubscribe from 'source' unless we receive `onCompleted` or `onError`,
69+
* as we want to receive and cache all of its values.
70+
*
71+
* This means this should never be used on an infinite or very large sequence, similar to toList().
72+
*/
73+
}
74+
cache.unsafeSubscribe(s);
75+
}
76+
}

0 commit comments

Comments
 (0)