示例#1
0
        public string DownloadString(string url)
        {
            this.LazyLoad();

            string content = this.GetCachedPage(url);

            if (content == null)
            {
                using (WebClient client = new WebClient())
                {
                    content = client.DownloadString(url);
                }

                CachedPage page = new CachedPage();
                page.Id       = GetNextId();
                page.FileName = string.Format("{0}.htm", page.Id);
                page.Status   = CacheStatus.Cached;
                page.Url      = url;
                page.Date     = DateTime.Now;

                lock (this.pages)
                {
                    pages.Add(url, page);
                }

                this.CachePage(page, content);
                this.SaveCache();
            }

            return(content);
        }
示例#2
0
        private void CachePage(CachedPage page, string content)
        {
            if (!Directory.Exists(this.cacheDirectory))
            {
                Directory.CreateDirectory(this.cacheDirectory);
            }

            string cachedFileName = Path.Combine(this.cacheDirectory, page.FileName);

            File.WriteAllText(cachedFileName, content);
        }
示例#3
0
        private string GetCachedPage(string url)
        {
            lock (this.pages)
            {
                if (this.pages.ContainsKey(url))
                {
                    CachedPage page = this.pages[url];

                    string cachedFileName = Path.Combine(this.cacheDirectory, page.FileName);

                    return(File.ReadAllText(cachedFileName));
                }
            }

            return(null);
        }