callcache

Auto cache the function return value.
3 followers

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).

Requirements

You should specify a cache components at least.

Usage

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.

Resources

Total 3 comments

#2380 report it
Jam at 2010/12/19 09:49pm
Thank you for mdomba

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:

def decorate(f):
    def wrap(*args,**kargs):
        print "Wrap:",args,kargs
        f(*args,**kargs)
    return wrap
 
#Auto decorate call
@decorate
def runit(a1,a2):
    print "Test",a1,a2

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:

class Demo extends CComponent {
    public function yiideco_methodName() {
        static $yiidecorators=array('cacheCall','otherDecorName');
    }
}
 
$d=new Demo();
$d->methodName();

But another hand,this is a obtrusive way,you must change you method name.

I am seeking the better way.....

#2370 report it
jacmoe at 2010/12/17 10:52am
Awesome!

This is great! Thumbs up from me. :)

#2368 report it
Maurizio Domba at 2010/12/17 07:48am
translation

English translation would be handy... I wote for it :)

Leave a comment

Please to leave your comment.

Create extension
Downloads
  • License: New BSD License
  • Developed by: Jam
  • Category: Caching
  • Votes: +7
  • Downloaded: 390 times
  • Created on: Dec 17, 2010
  • Last updated: Dec 17, 2010
  • Tags: Cache