7

The thing I have a web app (asp.net) which needs to have a feed. So I used the System.ServiceModel.Syndication namespace to create the function that creates the 'news'. The thing is it executes everytime somebody calls it, should I use it just to create an xml file and make my rss url point to the file?

What's the best approach?

Edit: Maybe that's where I'm wrong. I'm not using a handler... I'm just using a WCF service returning a Rss20FeedFormatter with the data.

Kelsey
  • 47,246
  • 16
  • 124
  • 162
sebagomez
  • 9,501
  • 7
  • 51
  • 89

3 Answers3

8

In the past when I have implemented RSS I cached the RSS data in the HttpContext.Current.Cache.

RSS data usually doesn't have to get updated that often (eg. once a minute is more than enough) so you would only have to actually hit the DB once every minute instead of every single time someone requests your RSS data.

Here is an example of how to use the cache:

// To save to the cache
HttpContext.Current.Cache.Insert("YourCachedName", pObjectToCache, null, DateTime.UtcNow.AddMinutes(1), System.Web.Caching.Cache.NoSlidingExpiration);
// To fetch from the cache
YourRssObject pObject = HttpContext.Current.Cache[("YourCachedName"] as YourRssObject : null;

You could also set the following in your ashx:

context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(1));

This will make your RSS page serve up a cached version until it expires. This takes even less resources but if you have other things that use your RSS data access layer calls then this will not cache that data.

You can also make it cache based on a query param that your RSS might receive as well by setting:

context.Response.Cache.VaryByParams["YourQueryParamName"] = true;
Kelsey
  • 47,246
  • 16
  • 124
  • 162
  • I appreciate the comments... but they do not answer my question... should I use the cache you are suggesting or just create a static file? – sebagomez May 29 '09 at 19:59
  • 1
    All the feeds I have made have used one of these methods. I have never used a static file because it was so easy to not have worry about having to write new / replace files on the fly. – Kelsey May 29 '09 at 20:18
1

ASP.Net has some pretty good server-side caching built in. Assuming you are using these classes as part of normal aspx page or http handler (ashx), you can just tell ASP.Net to cache it aggressively rather than re-build your feed on each request.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

In your .aspx page can't you just use:

<%@ OutputCache Duration="60" VaryByParam="none" %>

I have a blog posting where I have the results of a stored proc sent to my phone as an RSS feed HERE.

JBrooks
  • 9,901
  • 2
  • 28
  • 32