Caching a PHP Array

风流意气都作罢 提交于 2019-11-29 17:58:25

问题


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.


回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/4218623/caching-a-php-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!