Skip to content

Commit

Permalink
修复从缓存中获取数据时不触发onComplete事件的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
LtLei committed Oct 17, 2017
1 parent 2a109b3 commit b61db5b
Showing 1 changed file with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,51 @@ public <T> Observable<Boolean> saveLocal(T data, String key, long cacheTime) {

public <T> Observable<CacheResponse<T>> get(final String key, final boolean update, final Type type) {
Utilities.checkNullOrEmpty(key, "key is null or empty.");
Observable<CacheResponse<T>> memory = Observable.create(new ObservableOnSubscribe<CacheResponse<T>>() {
return Observable.create(new ObservableOnSubscribe<CacheResponse<T>>() {
@Override
public void subscribe(ObservableEmitter<CacheResponse<T>> e) throws Exception {
CacheResponse<T> response = new CacheResponse<>();
if (update) {
remove(key);
e.onComplete();
}

T data = getDataFromCache(getMemoryCache(), key, update, type);
if (data != null) {
response.setData(data);
LogUtil.i("data from memory.");
e.onNext(response);
e.onComplete();
} else {
data = getDataFromCache(getDiskCache(), key, update, type);
if (data != null) {
response.setData(data);
LogUtil.i("data from disk");
e.onNext(response);
e.onComplete();
} else {
LogUtil.i("data is null.");
e.onNext(response);
e.onComplete();
}
}
}
});

/*Observable<CacheResponse<T>> memory = Observable.create(new ObservableOnSubscribe<CacheResponse<T>>() {
@Override
public void subscribe(ObservableEmitter<CacheResponse<T>> e) throws Exception {
CacheResponse<T> response = new CacheResponse<>();
if (update) {
remove(key);
e.onComplete();
}
T data = getDataFromCache(getMemoryCache(), key, update, type);
if (data != null) {
response.setData(data);
LogUtil.i("data from memory.");
e.onNext(response);
// e.onComplete();
} else {
e.onComplete();
}
Expand Down Expand Up @@ -248,8 +278,7 @@ public void subscribe(ObservableEmitter<CacheResponse<T>> e) throws Exception {
}
}
});

return Observable.concat(memory, disk);
return Observable.concat(memory, disk);*/
}

private <T> void save2Memory(String key, RealEntity<T> realEntity) {
Expand All @@ -273,7 +302,7 @@ private <T> T getDataFromCache(ICache cache, String key, boolean update, Type ty
T data = null;
if (result != null) {
//非永久缓存,并且缓存尚未过期,或者是永久缓存
if (result.getCacheTime()==-1 ||(result.getCacheTime() != -1 && (result.getUpdateDate() + result.getCacheTime() > System.currentTimeMillis()))) {
if (result.getCacheTime() == -1 || (result.getCacheTime() != -1 && (result.getUpdateDate() + result.getCacheTime() > System.currentTimeMillis()))) {
data = result.getDatas();
}
}
Expand All @@ -300,7 +329,7 @@ public Observable<Boolean> remove(String key) {
return Observable.just(result);
}

public Observable<Boolean> remove(String... keys){
public Observable<Boolean> remove(String... keys) {
if (getCacheMode() == CacheMode.NONE) return Observable.just(true);
boolean result = false;
for (int i = 0; i < keys.length; i++) {
Expand Down

0 comments on commit b61db5b

Please sign in to comment.