Beispiel #1
0
        public override ICacheItem Read(Stream inputStream)
        {
            string s        = UtilityMethods.ReadString(inputStream);
            string response = UtilityMethods.ReadString(inputStream);

            string[] chunks = s.Split('\n');

            // Corrupted cache record, so throw IOException which is then handled and returns partial cache.
            if (chunks.Length != 2)
            {
                throw new IOException("Unexpected number of chunks found");
            }

            string url          = chunks[0];
            var    creationTime = new DateTime(long.Parse(chunks[1], System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo));
            var    item         = new ResponseCacheItem(new Uri(url), response, creationTime);

            return(item);
        }
Beispiel #2
0
        private T GetResponse <T>(Dictionary <string, string> parameters, TimeSpan cacheTimeout) where T : ITwentyThreeParsable, new()
        {
            // Flow for GetResponse.
            // 1. Check API Key
            // 2. Calculate Cache URL.
            // 3. Check Cache for URL.
            // 4. Get Response if not in cache.
            // 5. Write Cache.
            // 6. Parse Response.

            CheckApiKey();

            parameters["api_key"] = ApiKey;

            // If performing one of the old 'flickr.auth' methods then use old authentication details.
            var method = parameters["method"];

            if (!string.IsNullOrEmpty(AuthToken))
            {
                parameters["auth_token"] = AuthToken;
            }

            var url = CalculateUri(parameters, !string.IsNullOrEmpty(sharedSecret));

            lastRequest = url;

            string responseXml;

            if (InstanceCacheDisabled)
            {
                responseXml = TwentyThreeResponder.GetDataResponse(this, BaseUri.AbsoluteUri, parameters);
            }
            else
            {
                var urlComplete = url;

                var cached = (ResponseCacheItem)Cache.Responses.Get(urlComplete, cacheTimeout, true);
                if (cached != null)
                {
                    Debug.WriteLine("Cache hit.");
                    responseXml = cached.Response;
                }
                else
                {
                    Debug.WriteLine("Cache miss.");
                    responseXml = TwentyThreeResponder.GetDataResponse(this, BaseUri.AbsoluteUri, parameters);

                    var resCache = new ResponseCacheItem(new Uri(urlComplete), responseXml, DateTime.UtcNow);

                    Cache.Responses.Shrink(Math.Max(0, Cache.CacheSizeLimit - responseXml.Length));
                    Cache.Responses[urlComplete] = resCache;
                }
            }

            lastResponse = responseXml;

            var item = new T();

            item.Load(responseXml);

            return(item);
        }