Caching JSON output in PHP -


Got a problem with a little bit of Facebook and Tweeter API and playing a problem with JSON output of search queries Not, though I have read further and felt that I can end the "rate limited" as quoted from the documentation.

I was thinking that it is easy to cache JSON output every hour so that I can try at least and prevent it from happening? If so, how is it done? As I tried a YouTube video, but it does not really give much information about how to write a directory listing content in the cache.ff file, but it does not really tell that it can be done with JSON output Whether or not to use 60 minutes time interval or how to get information back from the cache file.

Any help or code will be highly appreciated as it seems very little in this tutorial on this type of object.

Here's a simple task that adds caching to get some URL content:

  function getJson ($ url) {// cache files are created like cache / ABCDF 123456 ... $ cacheFile = 'cache' DIRECTORY_SEPARATOR md5 ($ url); If (file_exists ($ cachefile)) {$ fh = fopen ($ cacheFile, 'r'); $ CacheTime = trim (fgets ($ fh)); // If the data was recently cached, then return the cached data ($ cacheTime & gt; strtotime ('60 min')) {return Fred ($ FH); } // delete the cache file fclose ($ fh); Unlink to ($ cacheFile); } $ Json = / * Generally from Twitter * /; $ FH = FOPN ($ cachefile, 'W'); FILIT ($ FH, time (). "\ N"); FILIT ($ FH, $ JSN); Fclose ($ fh); Return $ json; }   

This URL is used to identify cache files, next time the duplicate request from the same URL will be read from the cache. This writes the timestamp in the first line of the cache file, and an hour-long cached data is discarded. This is just a simple example and you probably want to customize it.

Comments