use of org.aisen.android.network.cache.ICacheUtility in project AisenWeiBo by wangdan.
the class ABizLogic method doGet.
@Override
public <T> T doGet(HttpConfig config, Setting action, Params params, Class<T> responseCls) throws TaskException {
// 缓存器
ICacheUtility cacheUtility = null;
// 返回的缓存
T cache = null;
// 配置的缓存模式
if (action.getExtras().containsKey(CACHE_UTILITY)) {
if (!TextUtils.isEmpty(action.getExtras().get(CACHE_UTILITY).getValue())) {
try {
cacheUtility = (ICacheUtility) Class.forName(action.getExtras().get(CACHE_UTILITY).getValue()).newInstance();
} catch (Exception e) {
Logger.w(getTag(action, "Get"), "CacheUtility 配置错误");
}
}
} else {
Logger.v(getTag(action, "Get"), "CacheUtility 没有配置");
}
if (mCacheMode != CacheMode.disable) {
// 拉取缓存数据
if (cacheUtility != null) {
long time = System.currentTimeMillis();
cache = (T) cacheUtility.findCacheData(action, params);
if (cache != null) {
Logger.d(getTag(action, "Cache"), "读取缓存耗时 %s ms", String.valueOf(System.currentTimeMillis() - time));
}
}
}
// 缓存不存在、服务数据优先等情况下,拉取服务数据
if (cache == null || mCacheMode == CacheMode.servicePriority) {
try {
long time = System.currentTimeMillis();
T result = getHttpUtility(action).doGet(resetHttpConfig(config, action), action, params, responseCls);
Logger.d(getTag(action, "Get-Http"), "耗时 %s ms", String.valueOf(System.currentTimeMillis() - time));
if (result != null && result instanceof IResult) {
putToCache(action, params, (IResult) result, cacheUtility);
}
Logger.d(getTag(action, "Get-Http"), "返回服务器数据");
return result;
} catch (TaskException e) {
Logger.w(getTag(action, "Exception"), e + "");
throw e;
} catch (Exception e) {
Logger.w(getTag(action, "Exception"), e + "");
throw new TaskException(TextUtils.isEmpty(e.getMessage()) ? "服务器错误" : e.getMessage());
}
} else {
if (cache != null) {
Logger.d(getTag(action, "Cache"), "返回缓存数据");
// 缓存存在,且有效
return cache;
}
}
throw null;
}
Aggregations