/// <summary> /// Gets the resource response. /// </summary> /// <param name="uri">The URI of the request.</param> /// <param name="args">The arguments to be used on the request.</param> /// <returns></returns> public override ResourceResponse GetResponse(string uri, NetworkResourceArguments args) { ResourceResponseCache response = new ResourceResponseCache(uri, args); // find the item in the cache index CacheIndex cacheIndex = CacheIndexMap.GetFromUri(uri); CacheIndexItem cacheIndexItem = cacheIndex.Get(uri); // ensure cache item is current response.ReturnStatus = cacheIndex.EnsureCurrentCache(cacheIndexItem, args); // store local references to cache index and item to prevent having to look up // values again later. response.CacheIndex = cacheIndex; response.CacheIndexItem = cacheIndexItem; // populate response data collection with cache index values; response.Data.Add("RelativeUri", cacheIndexItem.RelativeUri); response.Data.Add("AttemptToRefresh", cacheIndexItem.AttemptToRefresh.ToString()); response.Data.Add("Downloaded", cacheIndexItem.Downloaded.ToString()); response.Data.Add("Expiration", cacheIndexItem.Expiration.ToString()); response.Data.Add("IsExpired", cacheIndexItem.IsExpired.ToString()); response.Data.Add("IsStale", cacheIndexItem.IsStale.ToString()); response.Data.Add("ETag", cacheIndexItem.ETag); return(response); }
/// <summary> /// extension method to load url into Xdocument from Network Resource Library cache (refreshing if needed) /// </summary> /// <param name="element">XElement</param> /// <param name="url">URL for resource being requested</param> /// <param name="cachePeriod">TimeSpan to retain cached resource, if not specified on server</param> /// <param name="prefetch">Indicates whether resource should be maintained by prefetcher</param> /// <returns></returns> public static XElement Load(this XElement element, string url, TimeSpan cachePeriod, bool prefetch) { ResourceResponseCache response = null; try { ResourceRequest request = NetworkResourceLibrary.Instance.GetResourceRequest(url, ResourceStrategyType.Cache); response = (ResourceResponseCache)request.GetResponse(60000); if (response.CacheIndexItem.Expiration == DateTime.MinValue.ToUniversalTime()) { response.CacheIndexItem.Expiration = DateTime.UtcNow.Add(cachePeriod); } response.CacheIndexItem.PreFetch = prefetch; return(XElement.Load(new StringReader(Device.File.ReadString(response.GetResponseFileName(), EncryptionMode.NoEncryption)))); } catch (XmlException) { if (response != null) { response.CacheIndex.RemoveCurrentCache(response.CacheIndexItem); } return(null); } catch (WebException) { return(null); } }