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 Posts
XhtmlTextWriter Class (System.Web.UI) (1/17/2019)
WebResourceAttribute.PerformSubstitution Property (System.Web.UI) (1/17/2019)
Xml.AddParsedSubObject(Object) Method (System.Web.UI.WebControls) (1/17/2019)
Xml.Render(HtmlTextWriter) Method (System.Web.UI.WebControls) (1/17/2019)
WebResourceAttribute.WebResource Property (System.Web.UI) (1/17/2019)
WebResourceAttribute Class (System.Web.UI) (1/17/2019)
WebResourceAttribute.ContentType Property (System.Web.UI) (1/17/2019)
WebZone Class (System.Web.UI.WebControls.WebParts) (1/17/2019)
WebPartZoneBase.HelpVerb Property (System.Web.UI.WebControls.WebParts) (1/17/2019)
WebPartZone Constructor (System.Web.UI.WebControls.WebParts) (1/17/2019)
WebPartZoneBase.OnCreateVerbs(WebPartVerbsEventArgs) Method (System.Web.UI.WebControls.WebParts) (1/17/2019)
WebPartZone Class (System.Web.UI.WebControls.WebParts) (1/17/2019)
WebPartVerb.Text Property (System.Web.UI.WebControls.WebParts) (1/17/2019)
WebPartZoneBase.CreateVerbs Event (System.Web.UI.WebControls.WebParts) (1/17/2019)
WebPartVerb.Enabled Property (System.Web.UI.WebControls.WebParts) (1/17/2019)
WebPartTransformer Class (System.Web.UI.WebControls.WebParts) (1/17/2019)
WebPartVerb.ImageUrl Property (System.Web.UI.WebControls.WebParts) (1/17/2019)
WebPartVerb Class (System.Web.UI.WebControls.WebParts) (1/17/2019)
WebPartVerb.Description Property (System.Web.UI.WebControls.WebParts) (1/17/2019)
WebPartManager Class (System.Web.UI.WebControls.WebParts) (1/17/2019)

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 feed for 20 minutes
    Dim dt as DataTable = CType(Cache("aspNetNews"), DataTable)
    If dt Is Nothing then
      'Item not in cache, get it manually    
      dt = GetRSSFeed("")
      Cache.Insert("aspNetNews", dt, Nothing, DateTime.Now.AddMinutes(20), TimeSpan.Zero)
      lblCacheStatus.Text = "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!)

" Else lblCacheStatus.Text = "The data was retrieved from the cache...

" 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 PrintDateOnly(s as DateTime) as String Return s.ToString("d") 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 Posts"> <ItemTemplate> <a href="<%# DataBinder.Eval(Container.DataItem, "link")%>"> <%# DataBinder.Eval(Container.DataItem, "title") %> </a> (<i><%# PrintDateOnly(DataBinder.Eval(Container.DataItem, "pubDate")) %></i>) </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid>

[Return to the article]