Should I add a cache to this script?

MultiformeIngegno

  • Posts: 1,337
Should I add a cache to this script?
« on February 2nd, 2013, 01:49 PM »
I have this script that authenticates with Twitter API 1.1 and retrieves the last 6 tweets from my timeline.

Code: [Select]
<?php
function buildBaseString($baseURI, $method, $params)
{
    
$r = array();
    
ksort($params);
    foreach(
$params as $key=>$value){
        
$r[] = "$key=" . rawurlencode($value);
    }

    return 
$method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r)); //return complete base string
}

function 
buildAuthorizationHeader($oauth)
{
    
$r = 'Authorization: OAuth ';
    
$values = array();
    foreach(
$oauth as $key=>$value)
        
$values[] = "$key=\"" . rawurlencode($value) . "\"";

    
$r .= implode(', ', $values);
    return 
$r;
}

$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";

$oauth_access_token = "INSERIRE TOKEN";
$oauth_access_token_secret = "INSERIRE TOKEN";
$consumer_key = "INSERIRE KEY";
$consumer_secret = "INSERIRE KEY";

$oauth = array( 'oauth_consumer_key' => $consumer_key,
    
'oauth_nonce' => time(),
    
'oauth_signature_method' => 'HMAC-SHA1',
    
'oauth_token' => $oauth_access_token,
    
'oauth_timestamp' => time(),
    
'count' => 6,
    
'oauth_version' => '1.0');

$base_info = buildBaseString($url, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;


$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
    
CURLOPT_HEADER => false,
    
CURLOPT_URL => $url . '?count=6',
    
CURLOPT_RETURNTRANSFER => true,
    
CURLOPT_SSL_VERIFYPEER => false);

$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

$twitter_data = json_decode($json);

    echo 
"<ul style='color:#6E6E6E'>";
    foreach (
$twitter_data as $tweet)
    {
        if (!empty(
$tweet)) {
            
$text = $tweet->text;
$text_in_tooltip = str_replace('"', '', $text); // replace " to avoid conflicts with title="" opening tags
            
$id = $tweet->id;
            
$time = strftime('%d %B', strtotime($tweet->created_at));
            
$username = $tweet->user->name;
        }
        echo 
'<li><span title="'; echo $text_in_tooltip; echo '">'; echo $text . "</span><br>
<a href=\"http://twitter.com/"
; echo $username ; echo '/status/'; echo $id ; echo '"><small>'; echo $time; echo ' </small></a> - 
<a href="http://twitter.com/intent/tweet?in_reply_to='
; echo $id; echo '"><small>rispondi</small></a> - 
<a href="http://twitter.com/intent/retweet?tweet_id='
; echo $id; echo '"><small>retweet</small></a> - 
<a href="http://twitter.com/intent/favorite?tweet_id='
; echo $id; echo '"><small>preferito</small></a></li>';
    }

    echo 
'</ul>';
    
?>


Should I add a cache mechanism? That maybe stores in a file the JSON fetched and updates it only if it's older than X minutes. Without caching does this impact much on server load?

Anthony`

  • Posts: 53
Re: Should I add a cache to this script?
« Reply #1, on February 2nd, 2013, 06:04 PM »
Twitter can be quite slow sometimes, which I have experienced using it, so I'd imagine maybe caching it would save the requests being made (assuming this is in a page with alot of traffic) and ease down the strain when Twitter is actually being a bit slow.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Should I add a cache to this script?
« Reply #2, on February 2nd, 2013, 06:55 PM »
I would definitely be inclined to cache it. Unless you need the data to be absolutely live (and honest I'm not sure you do), cache for probably 10 minutes would be enough and it will limit performance hurts in general.
When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest. | Game Memorial