memcached practice: A weird twitter gadget

memcached is widely used in modern websites like flickr, wikipedia, twitter, youtube, digg, WordPress and a long hot continuing list. In the past years, everyone is talking about XXX 2.0, and recently, yes, you got it, NoSQL….

Here’s a weird twitter gadget, to be used by other pages as gadget. You may notice some bad practice on variable naming, function organizing, but ignore them.
The main purpose is to demonstrate a memcached usage, when combined with HTTP cache control.

source generated by “php -s”


<?php
define
('SECRETSTRING''==='); // base64_encode('user:password')
define('USERTIMELINEURL''https://api.twitter.com/1/statuses/user_timeline.json?count=20');
define('EXPIRESECONDS'300); 

function gettweets()
{
    
$MEMCACHE_SERVERS = array(
        
"127.0.0.1"//localhost
    
);
    
$keyname 'fvn:tweets';

    $mc = new Memcache();
    foreach(
$MEMCACHE_SERVERS as $server){
        
$mc->addServer $server );
    }
    
$ret FALSE;
    if( !(isset(
$_SERVER['HTTP_IF_MODIFIED_SINCE']))) {// force refresh
        
$tweets gettweetsdirectly();
        if (
$mc->get($keyname) === FALSE) {
            
$ret $mc->set($keyname$tweetsMEMCACHE_COMPRESSEDEXPIRESECONDS);
        } else {
            
$ret $mc->replace($keyname$tweetsMEMCACHE_COMPRESSEDEXPIRESECONDS);
        }
    } else {
        
$tweets $mc->get($keyname);
        if (
$tweets === FALSE)
        {
            
$tweets gettweetsdirectly();
            
// cache for 10 minutes
            
$mc->set($keyname$tweetsMEMCACHE_COMPRESSEDEXPIRESECONDS);
        }
    }
    return 
$tweets;
}

function gettweetsdirectly()
{
    
$context stream_context_create(array(
        
'http' => array(
            
'header'  => "Authorization: Basic " SECRETSTRING)
        )
    );
    
$data file_get_contents(USERTIMELINEURLfalse$context);
    
$tweets json_decode($datatrue);
    if (
$tweets == NULL)
        return 
FALSE;
    return 
$tweets;
}

// from http://github.com/dizzytree/PHP-Twitter-Client/blob/master/config.php
function  format_tweet($str)
{
        
$formatted_text preg_replace('/(\b(www\.|http\:\/\/|https\:\/\/)\S+\b)/'"<a target='_blank' href='$1'>$1</a>"$str);
        
$formatted_text preg_replace('/\#(\w+)/'"<a target='_blank' href='https://search.twitter.com/search?q=$1'>#$1</a>"$formatted_text);
        
$formatted_text preg_replace('/\@(\w+)/'"<a target='_blank' href='https://twitter.com/$1'>@$1</a>"$formatted_text);
        return 
$formatted_text;
}

$t gettweets();
if (
$t == FALSE)
{
    
header ('Failed to get tweets.'500);
    exit;
}

//cache control
$lasttime strtotime($t[0]['created_at']);
if (isset(
$_SERVER['HTTP_IF_MODIFIED_SINCE']) && (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lasttime)) {
    
header('Last-Modified: '.gmdate('D, d M Y H:i:s'$lasttime).' GMT'true304);
    exit;
} else {
    
header('Last-Modified: ' gmdate("D, d M Y H:i:s"$lasttime) . ' GMT');
    
header('Expires: ' gmdate("D, d M Y H:i:s"$lasttime EXPIRESECONDS) . ' GMT');
}

//echo tweets
echo '<div class=\'gadget\' id=\'twitter\'>
    <span><a href="https://twitter.com/feuvan">tweets by feuvan</a></span>
    <ul>'
;
$len count($t);
for (
$i 0$i $len$i++)
{
    echo 
'
        <li><span>'
;
    echo 
format_tweet($t[$i]['text']);

    echo '
        </span><hr/></li>'
;
}
echo 
'</ul></div>';

?>


Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
This entry was posted in Default. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">