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
What's New in ASP.NET 4.5 and Visual Studio 2012 (8/19/2016)
Getting Started with ASP.NET Web API (7/19/2016)
Getting Started with ASP.NET MVC (7/19/2016)
Introducing ASP.NET Web Pages (7/19/2016)
Getting Started with ASP.NET Web Forms (7/19/2016)
ASP.NET 4.5 and Visual Studio 2012 (6/21/2016)
ASP.NET Identity Tips (Video) (6/21/2016)
ASP.NET Identity: Two-Factor Authentication (Video) (6/21/2016)
ASP.NET Identity (dotnetConf 2014) (6/21/2016)
Azure Active Directory Video Series (6/21/2016)
Introduction to ASP.NET Identity (Video) (6/21/2016)
ASP.NET Identity Videos (6/21/2016)
ASP.NET SignalR Videos (6/21/2016)
ASP.NET Web API Videos (6/21/2016)
ASP.NET MVC Videos (6/21/2016)
ASP.NET Web Forms Videos (6/21/2016)
ASP.NET Web Pages Videos (6/21/2016)
ASP.NET 3.5 and Visual Studio 2008 (6/21/2016)
ASP.NET 4 and Visual Studio 2010 (6/21/2016)
ASP.NET 4.5 and Visual Studio 2012 (6/21/2016)


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("http://www.asp.net/news/rss.ashx")
      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 ASPMessageboard.com 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]