The config long array included every time, and it's not cached at all!
The config first required In CApplication constructor
just like this
public function __construct($config=null) { Yii::setApplication($this); // set basePath at early as possible to avoid trouble if(is_string($config)) $config=require($config); //...
No caching goes here at all! even if you set it in main.php... At that point main.php event not loaded!
So if you want to cache it for some reason into memory for example, you can!
For this little tutorial I will use Zend Data Cache And igbinary for serialization
just open framework/base/CApplication.php see line 117 for __construct, and replace
if(is_string($config)) $config=require($config);
With:
if(is_string($config)){ if(($_config = igbinary_unserialize(zend_shm_cache_fetch("Yii::configs"))) === NULL) { zend_shm_cache_store("Yii::configs", igbinary_serialize(require($config)), 6000); $config = igbinary_unserialize(zend_shm_cache_fetch("Yii::configs")); } else { $config = $_config; } }
if cache is NULL
if(($_config = igbinary_unserialize(zend_shm_cache_fetch("Yii::configs"))) === NULL)
this means it don't exists, or expired, so we will require the config array and cache it.
zend_shm_cache_store("Yii::configs", igbinary_serialize(require($config)), 6000); $config = igbinary_unserialize(zend_shm_cache_fetch("Yii::configs"));
if the cache still exists and valid, we just read it.
That's it ...
It won't save you a lot of time, but with very big config, it could improve performance a little bit
Lets for learning purpose see all the path to CApplication::__construct
In your index.php you have
Yii::createWebApplication($config)->run();
Yii extends YiiBase
And have nothing inside
require(dirname(__FILE__).'/YiiBase.php'); class Yii extends YiiBase { }
Thats it...
Yii base have createWebApplication method
public static function createWebApplication($config=null) { return self::createApplication('CWebApplication',$config); }
lets see createApplication
public static function createApplication($class,$config=null) { return new $class($config); }
$class equals CWebApplication
CWebApplication constructor is extended from CApplication
And there you have
public function __construct($config=null) { Yii::setApplication($this); if(is_string($config)){ $config = require($config);//no cache here ;-) }
I did a test...
Like this:
$start = array_sum(explode(' ', microtime())); //... $end = array_sum(explode(' ', microtime())); $result = $end - $start; echo $result;
When using Zend Optimizer+, there is about 0.00005 advantage to simple require (this is of course the array cached to memory automaticly by optimizer+, so you just waste the time with igbinary and cache it again etc.)
If you don't use it, the memory cache work 30 times faster than simple require
simple require takes about 0.003 sec. while my way takes about
0.0001 sec.
So my way works 30 times faster, or with no difference if op code cache used.
so I would say it worth it if you don't use some good op code cache...
I'm sure most of you dont have Zend Data Cache or Zend Server...
You can use apc or xcache, it is all the same...
And for realization you can use serialize/unserialize instead of igbinary_serialize etc.
Total 10 comments
I used Zend Data Cache and igbinary, but you free to use whatever you want... You can replace igbinary with php default serialization. and instead of Zend Data Cache, you have apc, xcache, memcache etc.
I can't use Yii cache cause you define it in main.php, and that is what I cache...
And it is not worth it to define it manually twice...
Have you tried replacing zend and igserialize with Yii's file cache? That way we would not need any external component. Thanks!
if replace
with
But it is matters only on the cache creation stage...
Seems like micro optimization to me. But I like it ;)
I did a test...
Like this:
When using Zend Optimizer+, there is about 0.00005 advantage to simple require (this is of course the array cached to memory automaticly by optimizer+, so you just waste the time with igbinary and cache it again etc.)
If you don't use it, the memory cache work 30 times faster than simple require
simple require takes about 0.003 sec. while my way takes about
0.0001 sec.
So my way works 30 times faster, or with no difference if op code cache used.
so I would say it worth it if you don't use some good op code cache...
Overall application execution time w/ and w/o config cache to know if it's worth doing or not.
I'm not sure what exactly to test here...
The time the include takes, and the reading from memory?
we save disk reading... but not much time...
Any benchmarks?
Of course scripts will benefit from the caching, but this is not the same as caching the array in the memory.
Turn on APC and it is cached automatically
Leave a comment
Please login to leave your comment.