RxJava-缓存直到排放闲置一段时间?
RxJava- Caching until emissions go idle for a period?
我经常 运行 陷入一种模式,我不太确定如何有效地绕过它。
基本上,如果我有一个 Observable<T>
拿着一个昂贵的物品 T
,我不想在每次有人使用它或映射它时重新构建那个 T
物品到一千个不同的其他可观察对象,这将导致它被构建 1000 次。
所以我开始使用 replay()
将其缓存一段时间,但理想情况下我希望它在排放闲置一段时间后清除缓存。
是否有我可以使用的运算符或转换器来完成此操作?
public final class ActionManager {
private final Observable<ImmutableList<Action>> actionMap;
private ActionManager() {
this.actionMap = Observable.defer(() -> buildExpensiveList()).replay(10, TimeUnit.SECONDS).autoConnect();
}
//this method could get called thousands of times
//I don't want to rebuild the map for every call
public Observable<Action> forItem(Item item) {
actionMap.map(l -> //get Action for item);
}
}
更新
尝试将其实现为 Transformer/Operator 组合。我在这里做错了什么吗?
public static <T> Transformer<T,T> recacheOnIdle(long time, TimeUnit timeUnit) {
return obs -> obs.timeout(time, timeUnit).lift(new Operator<T,T>() {
private volatile T cachedItem;
private volatile boolean isCurrent = false;
@Override
public Subscriber<? super T> call(Subscriber<? super T> s) {
return new Subscriber<T>(s) {
@Override
public void onCompleted() {
if(!s.isUnsubscribed()) {
s.onCompleted();
}
}
@Override
public void onError(Throwable e) {
if(!s.isUnsubscribed()) {
if (e instanceof TimeoutException) {
isCurrent = false;
cachedItem = null;
} else {
s.onError(e);
}
}
}
@Override
public void onNext(T t) {
if(!s.isUnsubscribed()) {
if (!isCurrent) {
cachedItem = t;
}
s.onNext(cachedItem);
}
}
};
}
});
}
您可以使用 timeout operator and a connectable observable(保持和同步多个订阅者):
mirror the source Observable, but issue an error notification if a particular period of time elapses without any emitted items
通过这种方式,您可以通过重新缓存昂贵的项目来响应抛出的错误。假设这是 "rare" 个案例:
// if no emissions are made for a period of 3 seconds - will call onError
observableWithCache.timeout(3000, TimeUnit.MILLISECONDS).subscribe(new Subscriber<SomeObject>() {
public void onCompleted() {
}
public void onError(Throwable arg0) {
doClearCache(); // make sure to re-subscribe with timeout
}
public void onNext(SomeObject item) {
System.out.println("Got item: " + item); // you can ignore this
}
});
请注意,onError
不会取消原始可观察量,如图所示:
但是您可以对没有排放的时间段做出反应。
看看我为 rxjava2 创建的 gist。这是一个自定义转换器,可以在一段时间内保留缓存值。
我经常 运行 陷入一种模式,我不太确定如何有效地绕过它。
基本上,如果我有一个 Observable<T>
拿着一个昂贵的物品 T
,我不想在每次有人使用它或映射它时重新构建那个 T
物品到一千个不同的其他可观察对象,这将导致它被构建 1000 次。
所以我开始使用 replay()
将其缓存一段时间,但理想情况下我希望它在排放闲置一段时间后清除缓存。
是否有我可以使用的运算符或转换器来完成此操作?
public final class ActionManager {
private final Observable<ImmutableList<Action>> actionMap;
private ActionManager() {
this.actionMap = Observable.defer(() -> buildExpensiveList()).replay(10, TimeUnit.SECONDS).autoConnect();
}
//this method could get called thousands of times
//I don't want to rebuild the map for every call
public Observable<Action> forItem(Item item) {
actionMap.map(l -> //get Action for item);
}
}
更新
尝试将其实现为 Transformer/Operator 组合。我在这里做错了什么吗?
public static <T> Transformer<T,T> recacheOnIdle(long time, TimeUnit timeUnit) {
return obs -> obs.timeout(time, timeUnit).lift(new Operator<T,T>() {
private volatile T cachedItem;
private volatile boolean isCurrent = false;
@Override
public Subscriber<? super T> call(Subscriber<? super T> s) {
return new Subscriber<T>(s) {
@Override
public void onCompleted() {
if(!s.isUnsubscribed()) {
s.onCompleted();
}
}
@Override
public void onError(Throwable e) {
if(!s.isUnsubscribed()) {
if (e instanceof TimeoutException) {
isCurrent = false;
cachedItem = null;
} else {
s.onError(e);
}
}
}
@Override
public void onNext(T t) {
if(!s.isUnsubscribed()) {
if (!isCurrent) {
cachedItem = t;
}
s.onNext(cachedItem);
}
}
};
}
});
}
您可以使用 timeout operator and a connectable observable(保持和同步多个订阅者):
mirror the source Observable, but issue an error notification if a particular period of time elapses without any emitted items
通过这种方式,您可以通过重新缓存昂贵的项目来响应抛出的错误。假设这是 "rare" 个案例:
// if no emissions are made for a period of 3 seconds - will call onError
observableWithCache.timeout(3000, TimeUnit.MILLISECONDS).subscribe(new Subscriber<SomeObject>() {
public void onCompleted() {
}
public void onError(Throwable arg0) {
doClearCache(); // make sure to re-subscribe with timeout
}
public void onNext(SomeObject item) {
System.out.println("Got item: " + item); // you can ignore this
}
});
请注意,onError
不会取消原始可观察量,如图所示:
但是您可以对没有排放的时间段做出反应。
看看我为 rxjava2 创建的 gist。这是一个自定义转换器,可以在一段时间内保留缓存值。