Facebook Graph API caching JSON response Facebook Graph API caching JSON response json json

Facebook Graph API caching JSON response


There are some thinks that could come in handy when trying to construct cache and to cache actual object (or even arrays).

The functions serialize and unserialize allows you to get a string representation of an object or of an array so you can cache it as plain text and then pop the object/array as it was before from the string.

filectime which allows you to get the last modification date of a file, so when it is created, you can rely on this information to see if your cache is outdated like you tried to implement it.

And for the whole working code, there you go :

function get_data($url) {    /** @var $cache_file is path/to/the/cache/file/based/on/md5/url */    $cache_file = 'cache' . DIRECTORY_SEPARATOR . md5($url);    if(file_exists($cache_file)){        /**          * Using the last modification date of the cache file to check its validity          */        if(filectime($cache_file) < strtotime('-60 minutes')){            unlink($cache_file);        } else {            echo 'TRACE -- REMOVE ME -- out of cache';            /**              * unserializing the object on the cache file              * so it gets is original "shape" : object, array, ...               */            return unserialize(file_get_contents($cache_file));        }    }    $ch = curl_init();    $timeout = 5;    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    $data = curl_exec($ch);    curl_close($ch);    /**      * We actually did the curl call so we need to (re)create the cache file      * with the string representation of our curl return we got from serialize      */    file_put_contents($cache_file, serialize($data));    return $data;}

PS : note that I changed the $datos variable on your actual function get_data to a more common $data.


This answer will add a few more dependencies to your project, but it may be well worth it instead of rolling your own stuff.

You could use the Guzzle HTTP client, coupled with the HTTP Cache plugin.

$client = new Client('http://www.test.com/');$cachePlugin = new CachePlugin(array(    'storage' => new DefaultCacheStorage(        new DoctrineCacheAdapter(            new FilesystemCache('/path/to/cache/files')        )    )));$client->addSubscriber($cachePlugin);$request = $client->get('https://graph.facebook.com/12345678/posts?access_token=1111112222233333&limit=20&fields=full_picture,link,message,likes,comments&date_format=U');$request->getParams()->set('cache.override_ttl', 3600*8); // 8hrs$data = $request->send()->getBody();$result = json_decode($data);


Not sure is you can use memcache, if you can:

$cacheFile = 'cache' . DIRECTORY_SEPARATOR . md5($url);$mem = new Memcached();$mem->addServer("127.0.0.1", 11211);$cached = $mem->get($cacheFile);if($cached){  return $cached;}else{  $data = get_data($url);  $mem->set($cacheFile, json_encode($data), time() + 60*10); //10 min}