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
Community Videos and Blogs (6/22/2018)
Visual Studio 2017 15.6 Release Notes (6/22/2018)
Visual Studio 2017 15.0 Release Notes (6/22/2018)
Visual Studio 2017 15.5 Release Notes (6/22/2018)
Visual Studio 2017 Preview Release Notes (6/22/2018)
Visual Studio 2017 for Mac - Preview Release Notes (6/22/2018)
Visual Studio 2017 Preview Release Notes (6/22/2018)
Visual Studio 2015 Release Notes (6/22/2018)
Visual Studio 2017 Preview Release Notes (6/22/2018)
Visual Studio 2017 Version 15.3 Preview Release Notes (6/22/2018)
Visual Studio 2013 Update 3 (2013.3) Release Notes (6/22/2018)
Visual Studio 2013 Update 2 RTM Release Notes (6/22/2018)
Visual Studio 2015 Update 1 (6/22/2018)
Visual Studio 2013 Update 4 (2013.4) RC Release Notes (6/22/2018)
Visual Studio 2013 Update 4 Release Notes (6/22/2018)
Visual Studio 2010 Service Pack 1 Release Notes (6/22/2018)
Add a model to a Razor Pages app in ASP.NET Core (6/22/2018)
Use streaming in ASP.NET Core SignalR (6/22/2018)
Migrate from ASP.NET Web API to ASP.NET Core (6/22/2018)
Initiate HTTP requests (6/22/2018)


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]