12

My problem is im creating a large nested PHP array which is parsing information from multiple external sources.

On the first return I would like to cache this data.

Im pretty new to caching so don't really know what I should be looking for, any good or bad methods or even if this is common practise! Have googled but not really found anything decent for a cache noob.

Im already using smarty to cache my page content (excluding the dynamic bits), done apache tweaks, minifying etc to increase performance but page loading is still far to long. Sometimes upto 8 seconds!

Using PHP5 with Smarty. Using cURL to parse the XML which is then being stored into the array.

hakre
  • 193,403
  • 52
  • 435
  • 836
fl3x7
  • 3,723
  • 6
  • 26
  • 37
  • check the performance of downloading the xml using CURL, is always calling external resources/contents that cause the performance, NOT the PHP itself – ajreal Nov 18 '10 at 19:21
  • Thanks ajreal for your comment. After reading your comment ive been searching and will be looking into using php multi_exec to run things in parallel. Is this the only way to improve multiple GET and POSTS parsing? Any other tips and tricks for performance increase (other than getting a more powerful server LOL) – fl3x7 Nov 19 '10 at 14:46

3 Answers3

21

You could try to cache to a file:

file_put_contents("cache_file", serialize($myArray));

Then to load the cache:

$myArray = unserialize(file_get_contents("cache_file"));

This will work if the things in your array are serializable: no DB connections or file handles, or things like that. Strings and numbers are just fine.

If you need something fancier you can use a memory-based cache like memcached.

robbrit
  • 17,560
  • 4
  • 48
  • 68
  • Thanks a lot! The serialize option seems very useful. Ive tried it and it looks like it works. However I get the warning: Warning: unserialize() [function.unserialize]: Node no longer exists in C:\xampp\htdocs\foo.php on line 75 Is this because of the strings issue. Ive checked all my content in the array and it is all strings or integers. The result also seems to look right as well. Any help appreciated – fl3x7 Nov 18 '10 at 21:33
  • Wont let me edit my comment above but just wanted to say although i havent found a fix to the error I am getting I think its to do with this: http://stackoverflow.com/questions/119234/php-unserialize-keeps-throwing-same-error-over-100-times Il keep trying anyway. if someone can shed some light on a solution that would be great. Again thank you all for your help :) – fl3x7 Nov 18 '10 at 22:17
  • Woo. I just explicitly cast the the variables as strings and ints and its worked! – fl3x7 Nov 18 '10 at 22:29
  • This is fine to cache to a file. But if the external data is changed according to time how can we put an expiry for the file ..? – Happy Coder Sep 12 '12 at 08:59
  • You can't really put an expiry time, however you can check the file's modified time with `filemtime()`. – robbrit Sep 13 '12 at 11:15
  • As alternative option, an array can be stored in cache as JSON - when debugging cached items it is more readable than the result of serialize($array). – Jakhongir Oct 10 '20 at 06:56
1

If you use Smarty template engine, it exists a plugin for v3.1 that enable APC (Alternate PHP Cache) as an op-code cache, you also have a built-in memory storage area for lightning fast access to data.

Available here : https://www.smarty.net/forums/viewtopic.php?p=86501&sid=efc098e0cfb090a94e8c0d362c609263#86501

Meloman
  • 3,558
  • 3
  • 41
  • 51
0

have you thought about putting static $yourData = array(); in your method where you download the data then test whether theres any data in this static array and use that, overwise get the data? hope this helps in some way :D

codeguy
  • 662
  • 4
  • 21