Example #1
0
        private CacheInfo TryLoadFromDisk(string url)
        {
            if (_directoryOnDisk == null)
            {
                return(null); // no place to cache
            }

            CacheInfo found = null;

            // look for all files matching the prefix
            // looking for the one matching url that is not expired
            // removing expired (or invalid) ones
            string pattern = GetTempFileNamePrefixFromUrl(url) + "_*.feed";

            string[] files = Directory.GetFiles(_directoryOnDisk, pattern, SearchOption.TopDirectoryOnly);

            foreach (string feedFilename in files)
            {
                XmlDocument feedDoc               = null;
                bool        isFeedFileValid       = false;
                DateTime    utcExpiryFromFeedFile = DateTime.MinValue;
                string      urlFromFeedFile       = null;

                try
                {
                    feedDoc = new XmlDocument();
                    feedDoc.Load(feedFilename);

                    // look for special XML comment (before the root tag)'
                    // containing expiration and url
                    XmlComment comment = feedDoc.DocumentElement.PreviousSibling as XmlComment;

                    if (comment != null)
                    {
                        string c = comment.Value ?? string.Empty;
                        int    i = c.IndexOf('@');
                        long   expiry;

                        if (i >= 0 && long.TryParse(c.Substring(0, i), out expiry))
                        {
                            utcExpiryFromFeedFile = DateTime.FromBinary(expiry);
                            urlFromFeedFile       = c.Substring(i + 1);
                            isFeedFileValid       = true;
                        }
                    }
                }
                catch (XmlException)
                {
                    // error processing one file shouldn't stop processing other files
                }

                // remove invalid or expired file
                if (!isFeedFileValid || utcExpiryFromFeedFile < DateTime.UtcNow)
                {
                    TryDeleteFile(feedFilename);
                    // try next file
                    continue;
                }

                // match url
                if (urlFromFeedFile == url)
                {
                    // keep the one that expires last
                    if (found == null || found.Expiry < utcExpiryFromFeedFile)
                    {
                        // if we have a previously found older expiration, kill it...
                        if (found != null)
                        {
                            found.Dispose();
                            TryDeleteFile(found.FeedFilename);
                        }

                        // create DOM and set expiry (as found on disk)
                        found = new CacheInfo(feedDoc, utcExpiryFromFeedFile, feedFilename);
                    }
                }
            }

            // return best fit
            return(found);
        }