Search in sources :

Example 6 with CacheMode

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;
}
Also used : CacheMode(com.lzy.okgo.cache.CacheMode)

Example 7 with CacheMode

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);
            }
        }
    });
}
Also used : CacheMode(com.lzy.okgo.cache.CacheMode)

Aggregations

CacheMode (com.lzy.okgo.cache.CacheMode)7 HttpHeaders (com.lzy.okgo.model.HttpHeaders)4 AbsCallbackWrapper (com.lzy.okgo.callback.AbsCallbackWrapper)2 OkGoException (com.lzy.okgo.exception.OkGoException)2 Response (com.lzy.okgo.model.Response)2 BaseRequest (com.lzy.okgo.request.BaseRequest)2 IOException (java.io.IOException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 Request (okhttp3.Request)2 RequestBody (okhttp3.RequestBody)2