/// <inheritdoc/> public byte[] SerializeCacheItem <T>(CacheItem <T> value) { NotNull(value, nameof(value)); var jsonValue = this.Serialize(value.Value); var jsonItem = JsonCacheItem.FromCacheItem(value, jsonValue); return(this.Serialize(jsonItem)); }
/// <summary> /// Handle request to convert data from remote url /// </summary> /// <remarks> /// Example client: http://jsfiddle.net/s7UGv/4/ /// </remarks> /// <param name="context">context of request/response</param> /// <returns>Task</returns> public async Task HandleRequest(IOwinContext context) { var uri = HttpUtility.UrlDecode(context.Request.Query["uri"]); var callback = context.Request.Query["callback"]; var xml = string.Empty; //see if we have the result cached var jsonCache = Cache.Get(HttpUtility.UrlEncode(uri)) as JsonCacheItem; if (jsonCache == null) { using (var httpClient = new HttpClient()) { var response = await httpClient.GetAsync(uri); xml = await response.Content.ReadAsStringAsync(); } var json = await Converter.ConvertToJsonAsync(xml); jsonCache = new JsonCacheItem(json); //cache it... Cache.Add(HttpUtility.UrlEncode(uri), jsonCache, DateTimeOffset.Now.AddMinutes(CACHE_DURATION_MINS)); } //check If-None-Match header etag to see if it matches our data hash else if (context.Request.Headers["If-None-Match"] == String.Concat("\"", jsonCache.Hash, "\"")) { //if it does return 304 Not Modified context.Response.StatusCode = 304; return; } var result = string.Empty; if (string.IsNullOrWhiteSpace(callback)) { context.Response.ContentType = "application/json"; result = jsonCache.Data; } else { context.Response.ContentType = "application/javascript"; result = callback + "(" + jsonCache.Data + ");"; } //set the response as cached for cache duration context.Response.Headers["Cache-Control"] = string.Format("max-age={0}", CACHE_DURATION_MINS * 60); //set etag context.Response.ETag = String.Concat("\"", jsonCache.Hash, "\""); await context.Response.WriteAsync(result); }
public static async Task AddToCache(JsonCacheItem item, string key) { var storageFolder = ApplicationData.Current.LocalFolder; await EnsureClearedKeyAsync(key); await storageFolder.CreateFileAsync(key, CreationCollisionOption.ReplaceExisting); var storageFile = await storageFolder.GetFileAsync(key); await FileIO.WriteTextAsync(storageFile, item.JsonResult); }