/// <summary> /// Gets a collection from the cache /// </summary> /// <param name="collectionIdentifier"></param> /// <returns></returns> private async Task <IonCollection> getCollectionFromCacheAsync(CollectionCacheIndex cacheIndex, bool serverCallAsBackup) { string collectionURL = PagesURLs.getCollectionURL(_config); // retrieve from memory cache IonCollection collection = _memoryCache.collection; if (collection != null) { return(collection); } // try to load collection from isolated storage try { collection = await StorageUtils.loadCollectionFromIsolatedStorageAsync(_config).ConfigureAwait(false); // Add collection to memory cache if (collection != null) { _memoryCache.collection = collection; } } catch (Exception e) { IonLogging.log("Error getting collection from isolated storage. Message: " + e.Message, IonLogMessageTypes.ERROR); } return(collection); }
/// <summary> /// Used to get a page with a desired identifier /// </summary> /// <param name="pageIdentifier"></param> /// <returns>Already parsed IonPage</returns> public async Task <IonPage> getPageAsync(string pageIdentifier) { string pageURL = PagesURLs.getPageURL(_config, pageIdentifier); PageCacheIndex pageCacheIndex = await PageCacheIndex.retrieve(pageURL, _config).ConfigureAwait(false); bool isNetworkConnected = NetworkUtils.isOnline(); if (pageCacheIndex == null) { if (isNetworkConnected) { IonLogging.log("Loading page \"" + pageIdentifier + "\" from server.", IonLogMessageTypes.SUCCESS); return(await getPageFromServerAsync(pageIdentifier).ConfigureAwait(false)); } else { IonLogging.log("Error getting page \"" + pageIdentifier + "\" from server or cache.", IonLogMessageTypes.ERROR); throw new PageNotAvailableException(); } } // Get collection IonCollection collection = await getCollectionAsync().ConfigureAwait(false); // Get last changed of the page from collection DateTime pageLastChanged = collection.getPageLastChanged(pageIdentifier); // Estimate, if the page is outdated or not bool isOutdated = pageCacheIndex.isOutdated(pageLastChanged); if (!isOutdated) { IonLogging.log("Loading page \"" + pageIdentifier + "\" from cache.", IonLogMessageTypes.SUCCESS); return(await getPageFromCacheAsync(pageIdentifier).ConfigureAwait(false)); } else { if (isNetworkConnected) { // Download page from server IonLogging.log("Loading newer version of page \"" + pageIdentifier + "\" from server.", IonLogMessageTypes.SUCCESS); return(await getPageFromServerAsync(pageIdentifier).ConfigureAwait(false)); } else { // get old version from cache IonLogging.log("Loading potentially old version of page \"" + pageIdentifier + "\" from cache.", IonLogMessageTypes.WARNING); return(await getPageFromCacheAsync(pageIdentifier).ConfigureAwait(false)); } } }
/// <summary> /// Used to get the collection of the class /// </summary> /// <returns>The collection of this pages</returns> public async Task <IonCollection> getCollectionAsync() { string collectionURL = PagesURLs.getCollectionURL(_config); CollectionCacheIndex cacheIndex = await CollectionCacheIndex.retrieve(collectionURL, _config).ConfigureAwait(false); // Check if there is a not outdated cacheIndex avialible bool currentCacheEntry = cacheIndex != null && !cacheIndex.isOutdated(_config); bool networkConnected = NetworkUtils.isOnline(); if (currentCacheEntry) { // retrieve current version from cache IonLogging.log("Loading collection \"" + _config.collectionIdentifier + "\" from cache.", IonLogMessageTypes.SUCCESS); return(await getCollectionFromCacheAsync(cacheIndex, false).ConfigureAwait(false)); } else { if (networkConnected) { // download collection or check for modifications IonLogging.log("Loading collection \"" + _config.collectionIdentifier + "\" from server.", IonLogMessageTypes.SUCCESS); return(await getCollectionFromServerAsync(cacheIndex, false).ConfigureAwait(false)); } else { if (cacheIndex != null) { // no network: use potential old version from cache IonLogging.log("Using potentially old version of collection \"" + _config.collectionIdentifier + "\" from cache, because there is no internet connection.", IonLogMessageTypes.WARNING); return(await getCollectionFromCacheAsync(cacheIndex, false).ConfigureAwait(false)); } else { // Collection can neither be downloaded nor be found in cache IonLogging.log("Couldn't get collection \"" + _config.collectionIdentifier + "\" either from server or cache.", IonLogMessageTypes.ERROR); throw new CollectionNotAvailableException(); } } } }