Your home page is a great place to display a custom feed that shows your recent blog posts, tweets and news. If you work in PHP, then there’s a great tool called SimplePie that eases the creation of these custom RSS feeds; the graphic below says it best.

SimplePie is an awesome tool for combining multiple feeds such as your Twitter feed and your blog posts feed; and it has a great caching feature. However, there is one problem: loading two or more feeds can cause an extremely long delay in your page’s load time. The caching feature works well in this case, but that still means that one of your visitors will experience a “stale” cache at some point, and that means they will have to wait an extra 4, 5, even 10 seconds longer for your page to load. This is especially unacceptable on your homepage.

How you can speed up SimplePie using an AJAX request

I recently thought of a solution to this issue and it’s quite simple. When creating a SimplePie feed and specifying options such as what feeds will be loaded, whether or not we want to cache the results, and how long the cache should last, we can play a little trick on SimplePie.

  1. Tell SimplePie that it should cache its results for one, even two weeks. This means that even if I visit your website now, and then a week later, your homepage feed will load almost instantly as it’s getting the results from the cache. This does, however, introduce the new issue of the feed items being really stale.
  2. Fix the issue of old feed items: when a user visits your page, we show them the stale results (which show up instantly) and then in the background using AJAX, we tell the web page to refresh the cache without interrupting the user. Specifically, we’ll tell SimplePie to fetch the results for the feeds again, but without using the cache this time. Since the heavy lifting happens in the background, it never interrupts the user and we can even display the new fresher information when it’s done being fetched. This freshly fetched data will also update the cache to a more recent state.

Using the above technique, your users will always see some sort of feed information instantly, and it’ll always be only as old as when the last visitor landed on your home page; their very presence would have refreshed the cache!

I’m sure that there are other ways to accomplish a similar result, how do you handle feed caching on your site?

Related Reading:

SimplePie FAQ
A Collection of Beginner SimplePie Tutorials

Comments
  • Harish

    Good article. Do you have a tutorial posted with more information?.