Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • George 30 posts 122 karma points
    Mar 24, 2011 @ 15:04
    George
    0

    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.

    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.

    In place of:

    private string RSSFile = Path.GetTempPath() + @"\blog.xml";

    I put:

    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:

    string RSSFile = GetCachedFilePath();
    
    if (RSSFile == null)
    {
        RSSFile = CreateCachedFile();
        ProcessRSSFeed(RSSFile);
    }
    
    ProcessRSSXml(RSSFile);

    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.

  • Eric Herlitz 97 posts 129 karma points
    Mar 26, 2011 @ 15:28
    Eric Herlitz
    0

    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

    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!

     

  • All Blonde 86 posts 138 karma points
    Jun 13, 2013 @ 18:38
    All Blonde
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft