RSS Demo with Caching

This demo shows how to cache the RSS feed to reduce the load on the remote server. The RSS contents are cached for a duration of 20 minutes.


The data was NOT found in the cache... (refresh the Web page and you'll see that the content is now stored in the cache!)

20 Most Recent ASPMessageboard.com Posts
i need help setting up a intranet webpage.. (moonhead) (12:58 PM EST)
RE: OOps...minor typo... (brj1) (11:54 AM EST)
Query slow first time then fast???? (nbkf1ab) (10:59 AM EST)
Retrieve data into Gridview (spitfire77) (10:34 AM EST)
Automatically Selecting Record Every Hour (ShotoCon) (10:32 AM EST)
RE: Capture Users Local Time (Xanderno) (10:23 AM EST)
Capture Users Local Time (steve wilson) (10:06 AM EST)
RE: OOps...minor typo... (brj1) (8:54 AM EST)
Help getting FreeTextBox to work (Wilbo) (8:53 AM EST)
Yeah, 99.7% is probably more correct. (Xanderno) (8:28 AM EST)
Thanks very much Bill! (wilbo) (4:45 AM EST)
Improving the Sort Arrows GridView Control (painthu) (2:33 AM EST)
No code no help (bill wilkinson) (1:09 AM EST)
Never mind (KurtW) (12:09 AM EST)
RE: Cookies feeling (KurtW) (12:09 AM EST)
Cookies feeling "sugar" free! (KurtW) (11:42 PM EST)
RE: Only a minor comment... (KurtW) (11:38 PM EST)
RE: Need to do it in two steps... (yoganew) (10:27 PM EST)
Ahh...no wonder... (bill wilkinson) (9:38 PM EST)
Maybe I should just rewrite it... (bill wilkinson) (9:35 PM EST)


Source Code
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<script language="VB" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
    'Cache the ASPMB posts for 20 minutes
    If Cache("ASPMBPosts") Is Nothing then
      'Item not in cache, get it manually    
      Dim dt as DataTable = GetRSSFeed("http://www.aspmessageboard.com/scripts/rss.asp")
      Cache.Insert("ASPMBPosts", dt, Nothing, DateTime.Now.AddMinutes(20), TimeSpan.Zero)
      
      lblCacheStatus.Text = "The data was NOT found in the cache...<p>"
    Else
      lblCacheStatus.Text = "The data was retrieved from the cache...<p>"
    End If
    
    recentPosts.DataSource = Cache("ASPMBPosts")
    recentPosts.DataBind()      
  End Sub


  Function GetRSSFeed(strURL as String) as DataTable
    'Get the XML data
    Dim reader as XmlTextReader = New XmlTextReader(strURL)
    
    'return a new DataSet
    Dim ds as DataSet = New DataSet()
    ds.ReadXml(reader)    
    Return ds.Tables(2)
  End Function
  
  
  Function PrintTimeOnly(s as DateTime) as String
    Return s.ToString("t") & " EST"
  End Function
</script>

<asp:label id="lblCacheStatus" runat="server" />
<asp:DataGrid runat="server" id="recentPosts" AutoGenerateColumns="False"
     Font-Name="Arial" Font-Size="10pt"
     HeaderStyle-Font-Bold="True"
     HeaderStyle-HorizontalAlign="Center"
     HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White"
     HeaderStyle-Font-Size="15pt"
     AlternatingItemStyle-BackColor="#eeeeee">
  <Columns>
    <asp:TemplateColumn HeaderText="20 Most Recent ASPMessageboard.com Posts">
      <ItemTemplate>
        <a href="<%# DataBinder.Eval(Container.DataItem, "link")%>">
          <%# DataBinder.Eval(Container.DataItem, "title") %>
        </a> (<i><%# PrintTimeOnly(DataBinder.Eval(Container.DataItem, "datePosted")) %></i>)
      </ItemTemplate>
    </asp:TemplateColumn>
  </Columns>
</asp:DataGrid>


[Return to the article]