callcache

Auto cache the function return value.
5 followers

Generic Implementation : Memoize.php

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.

Here is a post about memoization in php:http://jex.im/programming/memoization-in-php.html

Also,in the archive,I upload the TestCase besides the CacheDecor class. TestCase shows more usage of class CacheDecor.

Resources

Total 2 comments

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

This is great! Thumbs up from me. :)

#2368 report it
Maurizio Domba Cerin 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