Example #1
0
        public void GetETagTest()
        {
            var tag = "etag";

            //Saves the cache and pass it a timespan for expiration
            barrel.Add(key: url, data: json, expireIn: TimeSpan.FromDays(1), eTag: tag);


            var cached = barrel.GetETag(url);

            Assert.AreEqual(cached, tag);
        }
Example #2
0
        /// <summary>
        /// Send a cached requests
        /// </summary>
        /// <param name="http">Http client ot use</param>
        /// <param name="barrel">Barrel to use for cache</param>
        /// <param name="req">request to send</param>
        /// <param name="expireIn">expire in</param>
        /// <param name="forceUpdate">If we should force the update or not</param>
        /// <param name="throttled">If throttled or not</param>
        /// <returns>The new or cached response.</returns>
        public static async Task <string> SendCachedAsync(this HttpClient http, IBarrel barrel, HttpRequestMessage req, TimeSpan expireIn, bool forceUpdate = false, bool throttled = true)
        {
            var url = req.RequestUri.ToString();

            var contents = barrel.Get <string>(url);
            var eTag     = barrel.GetETag(url);

            if (!forceUpdate && !string.IsNullOrEmpty(contents) && !barrel.IsExpired(url))
            {
                return(contents);
            }

            var etag = eTag ?? null;

            if (throttled)
            {
                await getThrottle.WaitAsync();
            }

            HttpResponseMessage r;
            string c = null;

            try {
                if (!forceUpdate && !string.IsNullOrEmpty(etag) && !string.IsNullOrEmpty(contents))
                {
                    req.Headers.IfNoneMatch.Clear();
                    req.Headers.IfNoneMatch.Add(new EntityTagHeaderValue(etag));
                }

                r = await http.SendAsync(req);

                if (r.StatusCode == HttpStatusCode.NotModified)
                {
                    if (string.IsNullOrEmpty(contents))
                    {
                        throw new IndexOutOfRangeException($"Cached value missing for HTTP request: {url}");
                    }

                    return(contents);
                }

                c = await r.Content.ReadAsStringAsync();
            } finally {
                if (throttled)
                {
                    getThrottle.Release();
                }
            }

            if (r.StatusCode == HttpStatusCode.OK)
            {
                // Cache it?
                var newEtag = r.Headers.ETag != null ? r.Headers.ETag.Tag : null;
                if (!string.IsNullOrEmpty(newEtag) && newEtag != etag)
                {
                    barrel.Add(url, c, expireIn, newEtag);
                }

                return(c);
            }
            else
            {
                throw new HttpCacheRequestException(r.StatusCode, "HTTP Cache Request Failed");
            }
        }