PHP Caching array values

Sunday, 19th July 2009

I'm currently working on my OpenXCJ project, which uses SOAP to get an array of links. To make life a little simpler I wanted to try caching this array, making over all run time alot smoother.

So after much googling I've come up with a working code snippet i thought I'd share with you all.

= $twodays) { // The cache must be out of date $fh = fopen($cacheFile, 'w') or die("can't open file"); fwrite($fh, " $value){ fwrite($fh, "\$links[".$key."] = \"".$value."\";\n"); } fclose($fh); } else { // Reading from cache file require_once($cacheFile); }

This snippet will work with out any problems for a 1-Dimentional array, however, the array I want to work with it 2-dimentional. So I needed to make a few changes bellow which might still be of use to someone...

= $twodays) { // The cache must be out of date $linkCount = 0; // A running counter $fh = fopen($cacheFile, 'w') or die("can't open file"); fwrite($fh, " $value){ // Loop threw out 2nd dimention fwrite($fh, "\$links[".$linkCount."][".$key."] = \"".$value."\";\n"); // Print each of our second dimention in the cache file } $linkCount++; //Increment $linkCount } fclose($fh); } else { // Reading from cache file require_once($cacheFile); }

Tags: