CachedObjects

CodeIgniter already has web page caching, so why yet another caching solution? The builtin cache in CodeIgniter is "all or nothing", great and simple if your site is not complex but isnt suitable if you have more demanding needs for cache.

HMVC patterns, per-user content, the list goes on. With CachedObjects you can separate the complex code that you like to cache from the per-user code that you need to dinamically generate every pageview, caching exactly what you want.

Its the dream of cache granularity: cache only what you want.

CachedObjects is a core extension (MY_Controller and MY_Model extensions) to CodeIgniter that implements caching at method level at call time using PHP Object overloading. You dont need to polute your code with cache control, its clean, its simple.

With CachedObjects you can cache Controller and Model methods using one of the two cache backends currently implemented, ci and memcache. ci use the same infrastructure already used by CodeIgniter's page cache base on filesystem (usually system/cache). If you have more demanding needs for cache, memcache is another option with way more scalability.

Instructions

In the package, there is a model / view / controller as example caching a controller method and a model method. Open it in your application directory and call http://yourserver/index.php/examplepage/. Your first access will be slow, more than 10 seconds, running the 2 sleep(5) in the model and controller examples. Once cached, cached-fast.

Example config

/*
| -------------------------------------------------------------------------
| CACHED OBJECTS
| -------------------------------------------------------------------------
| This file lets you configure Cached Objects backends
|
| cachedobjects_backend: 
|       ci        - CodeIgniter original cache system, normally system/cache
|       memcache  - Use a memcache pool as backend
|
|  memcache_config:
|       a tipical array configuring the memcached-client, example:
|           array(
|               'servers' => array('127.0.0.1:10000',
|                                   array('127.0.0.1:10001', 2),
|                                   '127.0.0.1:10002'),
|               'debug' => false,
|               'compress_threshold' => 10240,
|               'persistant' => true
|           );
|       note that all entries are required (servers, debug, compress_threshold, persistant).
|
*/
$config['cachedobjects_backend'] = 'memcache';
$config['memcache_config'] = array(
	'servers' => array('127.0.0.1:11211'),
	'debug' => false,
	'compress_threshold' => 10240,
	'persistant' => true
	);

Example

class ExampleController extends MY_Controller {

	function ExampleController() {
		parent::MY_Controller();
		$this->methodcache(60); // my methods will be cached for 60 minutes
	}

	function slowfunc() { // look ma, no special code here to cache it!
		sleep(2); // really slow thing here
		return 'result of slow function is '.rand();
	}

	function index() {
		$res = $this->slowfuncCached(); // here is the cache magic!
		echo "examplecontroller saying hello world, $res";
	}

}

TODO

Warning

CachedObjects will use object name, method name and parameter values to hash the cache name. If your method can accept a multitude of diferent values as parameters, you will get a multitude of cache entries. USE IT WISELY!

Last version

Last version of CachedObjects is v1.3.1. We released v1.3 with a 'small' glitch, 4 files missing from the packages, v1.3.1 corrects that and namespaced some files with 'co_' to avoid conflicts.

Download

 14078 22 Jun 15:23:05 2008 cachedobjects-1.3.1.tar.gz
 20024 22 Jun 15:23:06 2008 cachedobjects-1.3.1.zip

Last modified on Sun Jun 22 15:23:06 2008