/// <summary>Append the response page to the cached data.</summary> public virtual void CacheRequestPage(RequestFilter filter, RequestPage <ModProfile> page) { // early out if shouldn't cache if (!this.isCachingPermitted) { return; } // asserts Debug.Assert(filter != null); Debug.Assert(page != null); // cache request string filterString = filter.GenerateFilterString(); RequestPageData cachedData; if (this.requestCache.TryGetValue(filterString, out cachedData)) { cachedData.resultTotal = page.resultTotal; this.requestCache[filterString] = RequestPageData.Append(cachedData, page.resultOffset, Utility.MapProfileIds(page.items)); } else { cachedData = new RequestPageData() { resultOffset = page.resultOffset, resultTotal = page.resultTotal, modIds = Utility.MapProfileIds(page.items), }; this.requestCache.Add(filterString, cachedData); } // cache profiles foreach (ModProfile profile in page.items) { this.profileCache[profile.id] = profile; } // store if (this.storeIfSubscribed) { IList <int> subMods = ModManager.GetSubscribedModIds(); foreach (ModProfile profile in page.items) { if (subMods.Contains(profile.id)) { CacheClient.SaveModProfile(profile); } } } }
/// <summary>Append the response page to the cached data.</summary> public virtual void CacheRequestPage(RequestFilter filter, RequestPage <ModProfile> page) { // early out if shouldn't cache if (!this.isCachingPermitted) { return; } // asserts Debug.Assert(filter != null); Debug.Assert(page != null); // cache request string filterString = filter.GenerateFilterString(); RequestPageData cachedData; if (this.requestCache.TryGetValue(filterString, out cachedData)) { cachedData.resultTotal = page.resultTotal; this.requestCache[filterString] = RequestPageData.Append(cachedData, page.resultOffset, Utility.MapProfileIds(page.items)); } else { cachedData = new RequestPageData() { resultOffset = page.resultOffset, resultTotal = page.resultTotal, modIds = Utility.MapProfileIds(page.items), }; this.requestCache.Add(filterString, cachedData); } // cache profiles this.CacheModProfiles(page.items); }
// ---------[ FUNCTIONALITY ]--------- /// <summary>Fetches page of ModProfiles grabbing from the cache where possible.</summary> public virtual void FetchModProfilePage(RequestFilter filter, int resultOffset, int profileCount, Action <RequestPage <ModProfile> > onSuccess, Action <WebRequestError> onError) { Debug.Assert(onSuccess != null); Debug.Assert(this.minimumFetchSize <= APIPaginationParameters.LIMIT_MAX); if (profileCount > APIPaginationParameters.LIMIT_MAX) { Debug.LogWarning("[mod.io] FetchModProfilePage has been called with a profileCount" + " larger than the APIPaginationParameters.LIMIT_MAX." + "\nAs such, results may not be as expected."); profileCount = APIPaginationParameters.LIMIT_MAX; } // ensure indicies are positive if (resultOffset < 0) { resultOffset = 0; } if (profileCount < 0) { profileCount = 0; } // check if results already cached string filterString = filter.GenerateFilterString(); RequestPageData cachedPage; if (this.requestCache.TryGetValue(filterString, out cachedPage)) { List <int> requestModIds = new List <int>(profileCount); int cachedPageOffset = resultOffset - cachedPage.resultOffset; if (cachedPageOffset >= 0) { int expectedIdCount = profileCount; if (profileCount + resultOffset > cachedPage.resultTotal) { expectedIdCount = cachedPage.resultTotal - resultOffset; } for (int i = 0; i < profileCount && i + cachedPageOffset < cachedPage.modIds.Length; ++i) { requestModIds.Add(cachedPage.modIds[i + cachedPageOffset]); } if (expectedIdCount == requestModIds.Count) { RequestPage <ModProfile> requestPage = new RequestPage <ModProfile>() { size = profileCount, resultOffset = resultOffset, resultTotal = cachedPage.resultTotal, items = this.PullProfilesFromCache(requestModIds), }; bool isPageComplete = true; for (int i = 0; i < requestPage.items.Length && isPageComplete; ++i) { isPageComplete = (requestPage.items[i] != null); } if (isPageComplete) { onSuccess(requestPage); return; } } } } // PaginationParameters APIPaginationParameters pagination = new APIPaginationParameters(); pagination.offset = resultOffset; pagination.limit = profileCount; if (profileCount < this.minimumFetchSize) { pagination.limit = this.minimumFetchSize; } // Send Request APIClient.GetAllMods(filter, pagination, (r) => { if (this != null) { this.CacheRequestPage(filter, r); } if (onSuccess != null) { if (pagination.limit != profileCount) { var subPage = ModProfileRequestManager.CreatePageSubset(r, resultOffset, profileCount); onSuccess(subPage); } else { onSuccess(r); } } }, onError); }