Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best implementation for an RSS feed in C# (ASP.net)

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.

like image 651
sebagomez Avatar asked May 28 '09 18:05

sebagomez


2 Answers

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;
like image 78
Kelsey Avatar answered Oct 06 '22 23:10

Kelsey


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.

like image 24
Joel Coehoorn Avatar answered Oct 07 '22 00:10

Joel Coehoorn