Caching of twitter requests

Jul 20, 2009 at 9:09 PM

Web requests to twitter server executes on every page load, so adding simple caching can reduce page load for about second or two (and help already overloaded twitter servers a bit).

This is changed code (inside GetTimeLine property):

// Get timeline from Twitter
                XmlDocument document =HttpRuntime.Cache.Get("twitterxml") as XmlDocument;
                if (document == null)
                {
                    WebRequest request = WebRequest.Create(timelineUrl);
                    WebResponse response = null;
                    StreamReader reader = null;
                    request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + GetCredentials());
                    request.Method = "GET";
                    response = request.GetResponse();
                    reader = new StreamReader(response.GetResponseStream());
                    document=new XmlDocument();
                    document.LoadXml(reader.ReadToEnd());
                    reader.Close();
                    response.Close();
                    HttpRuntime.Cache.Insert("twitterxml",document,null,DateTime.Now.AddMinutes(10),Cache.NoSlidingExpiration);
                }

I just added checking cache and adding xml document to it, with expiration set to 20 minutes.

What do you think?