Comments (8)
firstCache
只会发出一个结果,也就是调用onNext()一次,优先取缓存中的如果缓存中有数据,就不会在取网络了
cacheAndRemote
如果有缓存会发出2个结果,也就是有缓存就会调用onNext()2次,先发出缓存的的结果,在发出网络的结果,如果没有缓存就只会有网络的结果发出
from rxcache.
你好 那我想 优先显示缓存,同时请求网络,网络有数据的时候 刷新缓存,显示网络数据,网络数据请求失败的时候,或者无网的时候,就显示缓存的数据,可以搞嘛?
from rxcache.
你这个需求cacheAndRemote
应该是可以实现的才对
from rxcache.
你可以看下源码,重写IObservableStrategy
或者IFlowableStrategy
接口定制你的缓存策略
from rxcache.
好的 谢谢啦
from rxcache.
cacheAndRemote这个缓存策略当无网的时候 也还是会请求网络,最后显示不出来数据。。。
from rxcache.
会发出2次结果,如果网络失败了第二次是会走onError的,如果你不想第二次的结果走onError,可以在CacheAndRemoteStrategy 的基础上小作修改
这样:
public class CustomStrategy implements IStrategy {
private boolean isSync;
public CacheAndRemoteStrategy() {
isSync = false;
}
public CacheAndRemoteStrategy(boolean isSync) {
this.isSync = isSync;
}
@Override
public <T> Observable<CacheResult<T>> execute(RxCache rxCache, String key, Observable<T> source, Type type) {
Observable<CacheResult<T>> cache = RxCacheHelper.loadCache(rxCache, key, type, true);
Observable<CacheResult<T>> remote;
if (isSync) {
remote = RxCacheHelper.loadRemoteSync(rxCache, key, source, CacheTarget.MemoryAndDisk, true);//把最后一个参数改为true
} else {
remote = RxCacheHelper.loadRemote(rxCache, key, source, CacheTarget.MemoryAndDisk, true);//把最后一个参数改为true
}
return Observable.concat(cache, remote)
.filter(new Predicate<CacheResult<T>>() {
@Override
public boolean test(@NonNull CacheResult<T> result) throws Exception {
return result != null && result.getData() != null;
}
});
}
@Override
public <T> Publisher<CacheResult<T>> flow(RxCache rxCache, String key, Flowable<T> source, Type type) {
Flowable<CacheResult<T>> cache = RxCacheHelper.loadCacheFlowable(rxCache, key, type, true);
Flowable<CacheResult<T>> remote;
if (isSync) {
remote = RxCacheHelper.loadRemoteSyncFlowable(rxCache, key, source, CacheTarget.MemoryAndDisk, true);//把最后一个参数改为true
} else {
remote = RxCacheHelper.loadRemoteFlowable(rxCache, key, source, CacheTarget.MemoryAndDisk, true);//把最后一个参数改为true
}
return Flowable.concat(cache, remote)
.filter(new Predicate<CacheResult<T>>() {
@Override
public boolean test(@NonNull CacheResult<T> result) throws Exception {
return result != null && result.getData() != null;
}
});
}
}
和已有的缓存策略使用方式是一样的:
observable
.compose(rxCache.<~>>transformObservable("custom_key", type, new CustomStrategy()))
.subscribe(new Observer<CacheResult<~>>() {
...
@Override
public void onNext(CacheResult<~> cacheResult) {
Object data=cacheResult.getData();//获取你的数据
}
...
}
这样如果网络请求失败了使不会再走onError的
from rxcache.
好的 我试试 麻烦了大佬
from rxcache.
Related Issues (20)
- 在使用磁盘缓存时,在remove或者clean时,会出现空指针,程序未做非空判断。
- RxCache 几种模式在移动端场景。
- 建议,缓存策略,添加只缓存内存,或者只保存硬盘的。
- 建议添加到公共jcenter仓库 HOT 2
- 添加2.x版本,Fragment就找不到getContext() HOT 2
- LruDiskCache出现问题 HOT 3
- 优先缓存,并设置超时时间 这条没懂。。
- 关于分页缓存的管理
- 建议添加获取所有缓存key的方法。这样更有利于对于缓存的管理
- 报这个是什么错误额? HOT 2
- cacheAndRemote使用问题 HOT 3
- Expected BEGIN_OBJECT but was STRING at line 1 column 78 path $.data.version HOT 1
- 此项目还维护吗 HOT 1
- 不支持返回Single 吗?。。
- 可以支持优先加载缓存,并且同时缓存最新的网络数据 HOT 1
- 缓存第二次获取不到
- Jcenter 仓库地址已失效 HOT 1
- 2.3.3的库引用失败 HOT 1
- could not resolve HOT 1
- 支持post缓存吗?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rxcache.