This extension can be used to cache function or method output in Memcache or other cache you specified.Usually,some functions's call will connect to the database,compute the result base many data records,these operation will waste much time,and lot of them can be cached(Maybe you even do this though memcache api or Yii::app()->cache->set manually).For example,select user's profiles from the database,the result should be cache in memory and keep 2 minutes than expires(or until the user modified his profiles).
You should specify a cache components at least.
CacheDecor is a configurable components.Just one php file. You can put it in your app components directory and add it to components config:
'components'=>array( 'cacheDecor'=>array( 'class'=>'application.components.CacheDecor', 'cacheID'=>'cache',//You must specify one cache component at leat. ) )
When you want to call a function with automatically cache,you can use
//Call a function Yii::app()->cacheDecor->run('your_function_name',$argumentsArray); //Or a method Yii::app()->cacheDecor->run(array($myObject,'theMethodNameHere'),$argsArray);
Another usage,it can auto decorate a method call. But this way may be obtrusive.You must use a method prefix.So I do not introduct it for more.
For more usage and config documentation,please read the comment in the php file. The CacheDecor class is very detailed commented but in chinese.--! If needed,some english documentation will follow uploaded.(Sorry,my english is not good.^^) Also,in the archive,I upload the TestCase besides the CacheDecor class. TestCase shows more usage of class CacheDecor.
Total 3 comments
Thank you for mdomba. In fact,I have another idea. In python,you can put "@decoratorFunctionName" before a method to use an auto decorator. Code:
But php hasn't this syntax,so I think Yii's core class:"CComponent" should add this useful function. The implementation should be like below:
But another hand,this is a obtrusive way,you must change you method name.
I am seeking the better way.....
This is great! Thumbs up from me. :)
English translation would be handy... I wote for it :)
Leave a comment
Please login to leave your comment.