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
Azure Active Directory code samples (10/17/2018)
ASP.NET Core tutorials (10/17/2018)
Add search to an ASP.NET Core MVC app (10/17/2018)
Format response data in ASP.NET Core Web API (10/17/2018)
Azure Key Vault configuration provider in ASP.NET Core (10/17/2018)
Add search to an ASP.NET Core MVC app (10/17/2018)
Get started with ASP.NET Core MVC and Visual Studio (10/17/2018)
ASP.NET Core SignalR configuration (10/17/2018)
Add search to an ASP.NET Core MVC app (10/17/2018)
Security considerations in ASP.NET Core SignalR (10/17/2018)
Integration tests in ASP.NET Core (10/17/2018)
Razor Pages unit tests in ASP.NET Core (10/17/2018)
Introduction to ASP.NET Core (10/17/2018)
Razor Pages authorization conventions in ASP.NET Core (10/17/2018)
Application startup in ASP.NET Core (10/17/2018)
Custom Model Binding in ASP.NET Core (10/17/2018)
Filters in ASP.NET Core (10/17/2018)
Filter methods for Razor Pages in ASP.NET Core (10/17/2018)
Custom storage providers for ASP.NET Core Identity (10/17/2018)
What's new in ASP.NET Core 1.1 (10/17/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]