Пример #1
0
        /// <summary>
        /// Used to save a page to the memory and isolated storage cache
        /// </summary>
        /// <param name="page"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public async Task savePageToCachesAsync(IonPage page, IonConfig config)
        {
            // Memory cache
            _memoryCache.savePage(page, _config);

            // Local storage cache
            await StorageUtils.savePageToIsolatedStorageAsync(page, config).ConfigureAwait(false);

            // Save page cache index
            await PageCacheIndex.save(page, _config).ConfigureAwait(false);
        }
Пример #2
0
        /// <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));
                }
            }
        }