Home arrow PHP arrow Page 2 - Caching Dynamic Twitter Signature Images with PHP

Implementing caching - PHP

Welcome to fifth and final part of this series on creating a dynamic Twitter signature image in PHP. In the last segment, I showed you how to implement custom PHP exceptions as an error-handling mechanism in your signature image application. Today we’re going to wrap up our signature image application by adding a caching feature. This is a two-fold solution that both boosts performance and overcomes a pitfall in the Twitter API.

TABLE OF CONTENTS:
  1. Caching Dynamic Twitter Signature Images with PHP
  2. Implementing caching
  3. Understanding the logic
  4. Finishing up the application
By: Nilpo
Rating: starstarstarstarstar / 1
September 01, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Now you need to make changes to the functional portions of the SignatureImage class.  In order to implement caching we'll need to rewrite part of the fetchUserInfo method used for retrieving the user's Twitter feed information.  Let's look at the method in its current form.

    private function fetchUserInfo($name)

    {

        $url        = "http://twitter.com/statuses/user_timeline/{$name}.xml?count=1";

        $xml = $this->curlRequest($url);

        if ($xml === false) {

            throw new SignatureImageException('User feed unavailable.');

        }

        $statuses = new SimpleXMLElement($xml);

        if (!$statuses || !$statuses->status) {

            throw new SignatureImageException('Invalid user channel.');

        }

       foreach ($statuses->status as $status) {

            $this->status_text   = (string) $status->text;

            $this->profile_image = (string) $status->user->profile_image_url;

            $this->screen_name   = (string) $status->user->screen_name;

            break;

        }

    }

To begin, we'll need to add another parameter to this function as well for passing in the location of the local cache folder.

    private function fetchUserInfo($name, $cache)

    {

        $url        = "http://twitter.com/statuses/user_timeline/{$name}.xml?count=1";

Next, we need to add some logic.  Currently, the function will begin by retrieving the user feed data from the Twitter API.  Since we want this to be performed through our caching mechanism, we'll need to add a few If blocks to determine when and if a Twitter API call should be implemented.

        if ($xml === false) {

            throw new SignatureImageException('User feed unavailable.');

        }

We'll be replacing the above section of code with the code below.

        $cache      = preg_match('#^(.*?)/$#i', $cache) ? $cache : "{$cache}/";

       $cache_file = $cache . md5($url) . '.xml';

        if (@filemtime($cache_file) + $this->cache_expires > time()) {

            $xml = @file_get_contents($cache_file);

        } else {

            $xml = $this->curlRequest($url);

            if ($xml !== false) {

                file_put_contents($cache_file, $xml);

            }

        }

        if ($xml === false && file_exists($cache_file)) {

            $xml = file_get_contents($cache_file);

        } elseif ($xml === false) {

            throw new SignatureImageException('User feed unavailable.');

        }

Here is the entire caching logic in its entirety.  Let's take a look at this one piece at a time and see how it works.

        $cache      = preg_match('#^(.*?)/$#i', $cache) ? $cache : "{$cache}/";

       $cache_file = $cache . md5($name) . '.xml';

This first piece of code establishes some ground work.  The first line will examine the contents of the $cache parameter (the local cache folder name).  It uses a regular expression to check and add a trailing slash if necessary.  The second line creates a file name that will be used to store or retrieve the cached user feed.



 
 
>>> More PHP Articles          >>> More By Nilpo
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap

Dev Shed Tutorial Topics: