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.

2
  • check the performance of downloading the xml using CURL, is always calling external resources/contents that cause the performance, NOT the PHP itself Commented Nov 18, 2010 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) Commented Nov 19, 2010 at 14:46

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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
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: stackoverflow.com/questions/119234/… Il keep trying anyway. if someone can shed some light on a solution that would be great. Again thank you all for your help :)
Woo. I just explicitly cast the the variables as strings and ints and its worked!
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 ..?
You can't really put an expiry time, however you can check the file's modified time with filemtime().
|
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.