Once in a time I need one of my sites to grab a RSS feed, parse it and display its results in the page. The main problem is that the processing takes a lot of time (and resources) which intemperate to a large delay on the client side.
This is also quite a silly thing to do due to the fact that unless its your twitter feed it usually doesn’t update so frequently.
So we want to cache the whole process in order to save time and resources and usually it’s been done by saving the feed locally and parsing it from the server. Though it shorts the grabbing time we still need to parse it again and again.
Cache the results
Instead of saving the feed a better way would be to save the parsing results, thus we only serve a text file that is on our server – which is always a lot faster.
The Idea is to have a function that looks for the cache file, if it finds it, the file content will be included, if not, the function will parse the feed and create a new cache file.
function getdatacache(){
$fileName = 'content.cache';
$file = 'cache/'.$fileName;
if (@file_exists($file))
{
$content = file_get_contents($file);
} else {
$content = parseTheFeed();
file_put_contents($file, $content);
}
return $content;
}
So, we set the file name, look for that file and if it exists we takes its content. Else we parse the feed (parseTheFeed is the function that does the actual parsing) and save the result (The function should return a string or to use output buffering ob-start – http://php.net/manual/en/function.ob-start.php).
What about Time To Live (TTL)?
Unless we want a one time only feed parsing (and what would be the point in that?) we need a TTL system. One way is to run a cron job on the cache folder that will delete files older than the required TTL. It can work but only if you’re hosting supplier supports cron jobs.
Instead of deleting files, we can set the TTL using php date function as the file name.
Instead of $fileName = ‘content.cache’; we can use
$fileName = date(‘H’).’.cache’;
With that change the script will create a new file every hour and save it to the cache folder. Although the first user in every hour will have a delay the system will be much faster for every user after that.
In order to prevent that from happening one can run a script on the last-minute of the hour that will create the next hour cache it can be done using a boolean switch and a separate call from the end of our script or by a different script called by a cron job.
function getdatacache($nextHour = false){
if($nextHour && date('i')>=57)
$fileName = (date('H')+1). '.cache';
else
$fileName = date('H').'.cache';
$file = 'cache/'.$fileName;
if (@file_exists($file))
{
$content = file_get_contents($file);
} else {
$content = parseTheFeed();
file_put_contents($file, $content);
}
return $content;
}
Note
Bare in mind that in order for this script to run properly, the cache folder should have write permissions.






Open Comment Author link in new window
I’ve been searching for a way to open a commenter link on a new window for a long time and with no success. Most of the techniques required me to open one of wordpress core files and alter its functions. Thought it did work, the changes were doom to vanish on the next upgrade.
The correct way to do that is to use the built-in hook system in order to alter the link after it was created.
add_filter('get_comment_author_link', 'authorLinkNew'); function authorLinkNew($result){ return str_replace ('<a', '<a target="_blank" ', $result); }Actually all we do here is search and replace on the anchor element and in that way to add the target blank into the link. It’s actually a bit rough, and there must be a dozen of different ways to do it which are more correct (using php domelement would probably be the best one, Using Regex with preg_replace may be another) but it’s surely the fastest way to do that.
Please note that using another plugin that rewrite links might cause a collision that way, and rewrite this function using a domelement will return better results.