This is a nice, lightweight tool that I've made use of for one of the sites I worked on recently. I made some code adjustments. See below.
The code in the control contains the following lines.
if (File.Exists(RSSFile) && IsBelowThreshold(RSSFile))
{
ProcessRSSFeed();
}
else
{
// if the file isn't created yet
ProcessRSSFeed();
}
ProcessRSSXml();
Note that both the if and else clauses do the same thing, making the if statement unnecessary. I think what is meant here is something like this.
if (!File.Exists(RSSFile) || !IsBelowThreshold(RSSFile))
ProcessRSSFeed();
ProcessRSSXml();
As the code is currently, the caching is, in essence, non-functional. It pulls the RSS feed each time the code is run. With the fix, it will only pull the RSS feed when the threshold is reached.
As a further improvement, you could save processing time by caching the HTML output, perhaps by building it all in a StringBuilder and then storing that content in the file rather than the raw RSS feed. Then ProcessRSSXml would probably be renamed ProcessCachedContent and would just pull the HTML from the cache and send it straight to the client.
Finally, the code doesn't currently allow for more than one reader on a website, or indeed, on the same web server, since it writes to a constant file name. If this control is used to read more than one feed, even across multiple sites on the same server, the current cache setup will cause the most recently cached rss content to be sent to the client regardless of which rss feed it came from. Here's how I updated the code to get around this issue.
private string cacheKey { get { return "RSSFeed-" + rssURL; } }
private string GetCachedFilePath()
{
if (Cache[cacheKey] != null)
return Cache[cacheKey].ToString();
return null;
}
private string CreateCachedFile()
{
string tempFile = Path.GetTempFileName(); // create a new temp file
Cache.Add(cacheKey,
tempFile, // store the temp file's name at this cache location
null, // no cache dependencies
DateTime.Now.AddMinutes(updateInterval_Minutes),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal,
null); // no callback
return tempFile;
}
This uses Path.GetTempFileName() to generate a unique temp file. The path to the temp file is stored in the web server's cache and expires after the threshold set on the control. This way, the temp file will be orphaned and eventually cleaned up, and a new temp file will be created when the time comes to refresh the cache.
The above methods are called from Page_Load as follows:
I've changed this in version 1.1.1 which is available now, however I choose a more simple model for this version
if (File.Exists(RSSFile) && IsBelowThreshold(RSSFile))
{
ProcessRSSFeed();
}
else if (!File.Exists(RSSFile))
{
// if the file isn't created yet
ProcessRSSFeed();
}
I'll look into your suggestions to implement multi feeds, excellent idea!
Caching doesn't work
This is a nice, lightweight tool that I've made use of for one of the sites I worked on recently. I made some code adjustments. See below.
The code in the control contains the following lines.
Note that both the if and else clauses do the same thing, making the if statement unnecessary. I think what is meant here is something like this.
if (!File.Exists(RSSFile) || !IsBelowThreshold(RSSFile)) ProcessRSSFeed(); ProcessRSSXml();
As the code is currently, the caching is, in essence, non-functional. It pulls the RSS feed each time the code is run. With the fix, it will only pull the RSS feed when the threshold is reached.
As a further improvement, you could save processing time by caching the HTML output, perhaps by building it all in a StringBuilder and then storing that content in the file rather than the raw RSS feed. Then ProcessRSSXml would probably be renamed ProcessCachedContent and would just pull the HTML from the cache and send it straight to the client.
Finally, the code doesn't currently allow for more than one reader on a website, or indeed, on the same web server, since it writes to a constant file name. If this control is used to read more than one feed, even across multiple sites on the same server, the current cache setup will cause the most recently cached rss content to be sent to the client regardless of which rss feed it came from. Here's how I updated the code to get around this issue.
In place of:
I put:
This uses Path.GetTempFileName() to generate a unique temp file. The path to the temp file is stored in the web server's cache and expires after the threshold set on the control. This way, the temp file will be orphaned and eventually cleaned up, and a new temp file will be created when the time comes to refresh the cache.
The above methods are called from Page_Load as follows:
Note that RSSFile is no longer a property, so it must be passed to the processing methods.
I hope these notes are helpful toward the future improvement of a handy package.
Thank you for your message George
I've changed this in version 1.1.1 which is available now, however I choose a more simple model for this version
Guys, question of the day: did you test that thing against Umbraco 6.0+?
Or do you know about any update avaliable to make it compatible with latest Umbraco?
Thanks
is working on a reply...