94 lines
3.0 KiB
Java
94 lines
3.0 KiB
Java
package com.ubt.jimu.base.http.cache;
|
|
|
|
import android.support.v4.media.MediaDescriptionCompat;
|
|
import android.text.TextUtils;
|
|
import com.ubt.jimu.utils.LogUtils;
|
|
import io.reactivex.Observer;
|
|
import io.reactivex.disposables.Disposable;
|
|
import java.lang.reflect.Method;
|
|
|
|
/* loaded from: classes.dex */
|
|
public abstract class CacheSubscriber<T> implements Observer<T> {
|
|
private String key;
|
|
private final CacheManager<T> mCacheManager = new CacheManager<>();
|
|
|
|
private void getCacheConfiguration() {
|
|
CacheKey cacheKey;
|
|
if (!TextUtils.isEmpty(setCacheKey())) {
|
|
T objectCache = this.mCacheManager.getObjectCache(this.key, 3600000L);
|
|
if (objectCache != null) {
|
|
LogUtils.c("调用缓存显示方法");
|
|
showCacheData(objectCache);
|
|
return;
|
|
}
|
|
return;
|
|
}
|
|
for (Method method : getClass().getDeclaredMethods()) {
|
|
if (method.getName().equals("showCacheData") && (cacheKey = (CacheKey) method.getAnnotation(CacheKey.class)) != null) {
|
|
String Key = cacheKey.Key();
|
|
LogUtils.c("缓存Key:" + Key);
|
|
long Time = cacheKey.Time();
|
|
if (!TextUtils.isEmpty(Key)) {
|
|
this.key = Key;
|
|
T objectCache2 = this.mCacheManager.getObjectCache(Key, Time);
|
|
LogUtils.c("缓存中的T:" + objectCache2);
|
|
if (objectCache2 != null) {
|
|
LogUtils.c("调用缓存显示方法");
|
|
showCacheData(objectCache2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void complete() {
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onComplete() {
|
|
complete();
|
|
CacheManager<T> cacheManager = this.mCacheManager;
|
|
if (cacheManager != null) {
|
|
cacheManager.closeDiskLruCache();
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onError(Throwable th) {
|
|
CacheManager<T> cacheManager = this.mCacheManager;
|
|
if (cacheManager != null) {
|
|
cacheManager.closeDiskLruCache();
|
|
}
|
|
onFailure(th);
|
|
}
|
|
|
|
public abstract void onFailure(Throwable th);
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onNext(T t) {
|
|
this.mCacheManager.putObjectCache(this.key, t);
|
|
onSucceed(t);
|
|
CacheManager<T> cacheManager = this.mCacheManager;
|
|
if (cacheManager != null) {
|
|
cacheManager.closeDiskLruCache();
|
|
}
|
|
}
|
|
|
|
public abstract void onStart(Disposable disposable);
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onSubscribe(Disposable disposable) {
|
|
getCacheConfiguration();
|
|
onStart(disposable);
|
|
}
|
|
|
|
public abstract void onSucceed(T t);
|
|
|
|
public String setCacheKey() {
|
|
return null;
|
|
}
|
|
|
|
@CacheKey(Key = "KEY", Time = MediaDescriptionCompat.BT_FOLDER_TYPE_MIXED)
|
|
public abstract void showCacheData(T t);
|
|
}
|