/// <summary> /// Saves a collectionCacheIndex to cache /// </summary> /// <param name="config"></param> /// <param name="lastModified"></param> public static async Task save( IonConfig config, DateTime lastModified ) { string collectionURL = PagesURLs.getCollectionURL( config ); CollectionCacheIndex cacheIndex = new CollectionCacheIndex( collectionURL, DateTimeUtils.now().ToUniversalTime(), lastModified ); await CacheIndexStore.save( collectionURL, cacheIndex, config ).ConfigureAwait( false ); }
/// <summary> /// Gets a collection from the server /// </summary> /// <param name="collectionIdentifier"></param> /// <returns></returns> private async Task<IonCollection> getCollectionFromServerAsync( CollectionCacheIndex cacheIndex, bool cacheAsBackup ) { //DateTime lastModified = cacheIndex != null ? cacheIndex.lastModified : DateTime.MinValue; try { // Retrive collecion from server and parse it HttpResponseMessage response = await _dataClient.getCollectionAsync( _config.collectionIdentifier, cacheIndex != null ? cacheIndex.lastModified : DateTime.MinValue ).ConfigureAwait( false ); // Only parse the answer if it is not newer than the cached version if( !( response.StatusCode == System.Net.HttpStatusCode.NotModified ) ) { // Parse collection IonCollection collection = await DataParser.parseCollectionAsync( response ).ConfigureAwait( false ); // Add collection to memory cache _memoryCache.collection = collection; // Save collection to isolated storage await StorageUtils.saveCollectionToIsolatedStorageAsync( collection, _config ).ConfigureAwait( false ); // save cacheIndex await saveCollectionCacheIndexAsync( collection.last_changed ).ConfigureAwait( false ); return collection; } else { // Collection in the server is the same as stored already in isolated storage cache if( _memoryCache.collection == null ) { // Only load collection from isolated storage cache, if the memory cache has no collection cached try { // Get collection from isolated storage IonCollection collection = await StorageUtils.loadCollectionFromIsolatedStorageAsync( _config ).ConfigureAwait( false ); // Add collection to memory cache if( collection != null ) { _memoryCache.collection = collection; } // change the last-mofied date in the cacheIndex to now await saveCollectionCacheIndexAsync( collection.last_changed ).ConfigureAwait( false ); return collection; } catch( Exception e ) { IonLogging.log( "Error getting collection from isolated storage. Message: " + e.Message, IonLogMessageTypes.ERROR ); return null; } } else { // change the last-mofied date in the cacheIndex to now await saveCollectionCacheIndexAsync( _memoryCache.collection.last_changed ).ConfigureAwait( false ); return _memoryCache.collection; } } } catch( Exception e ) { IonLogging.log( "Error retreiving collection data: " + e.Message, IonLogMessageTypes.ERROR ); return null; } }
/// <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> /// Saves a collectionCacheIndex to cache /// </summary> /// <param name="config"></param> /// <param name="lastModified"></param> public static async Task save(IonConfig config, DateTime lastModified) { string collectionURL = PagesURLs.getCollectionURL(config); CollectionCacheIndex cacheIndex = new CollectionCacheIndex(collectionURL, DateTimeUtils.now().ToUniversalTime(), lastModified); await CacheIndexStore.save(collectionURL, cacheIndex, config).ConfigureAwait(false); }