use of com.lzy.okgo.cache.CacheMode in project okhttp-OkGo by jeasonlzy.
the class BaseCachePolicy method prepareCache.
@Override
public CacheEntity<T> prepareCache() {
// check the config of cache
if (request.getCacheKey() == null) {
request.cacheKey(HttpUtils.createUrlFromParams(request.getBaseUrl(), request.getParams().urlParamsMap));
}
if (request.getCacheMode() == null) {
request.cacheMode(CacheMode.NO_CACHE);
}
CacheMode cacheMode = request.getCacheMode();
if (cacheMode != CacheMode.NO_CACHE) {
// noinspection unchecked
cacheEntity = (CacheEntity<T>) CacheManager.getInstance().get(request.getCacheKey());
HeaderParser.addCacheHeaders(request, cacheEntity, cacheMode);
if (cacheEntity != null && cacheEntity.checkExpire(cacheMode, request.getCacheTime(), System.currentTimeMillis())) {
cacheEntity.setExpire(true);
}
}
if (cacheEntity == null || cacheEntity.isExpire() || cacheEntity.getData() == null || cacheEntity.getResponseHeaders() == null) {
cacheEntity = null;
}
return cacheEntity;
}
use of com.lzy.okgo.cache.CacheMode in project BaseProject by feer921.
the class CacheCall method sendSuccessResultCallback.
/**
* 成功回调,发送到主线程
*/
private void sendSuccessResultCallback(final boolean isFromCache, final T t, final okhttp3.Call call, final okhttp3.Response response) {
final CacheMode cacheMode = baseRequest.getCacheMode();
OkGo.getInstance().getDelivery().post(new Runnable() {
@Override
public void run() {
if (isFromCache) {
// 缓存成功回调 (UI线程)
mCallback.onCacheSuccess(t, call);
if (cacheMode == CacheMode.DEFAULT || cacheMode == CacheMode.REQUEST_FAILED_READ_CACHE || cacheMode == CacheMode.IF_NONE_CACHE_REQUEST) {
// 请求结束回调 (UI线程)
mCallback.onAfter(t, null);
}
} else {
// 请求成功回调 (UI线程)
mCallback.onSuccess(t, call, response);
// 请求结束回调 (UI线程)
mCallback.onAfter(t, null);
}
}
});
}
Aggregations