CI has a caching mechanism that is not well documented, not very integrated with the rest of CI, not very extensible (sloppy design), but does seem to work. So I decided to give it a try. I also wrote an extra driver which could become permanent part of the CI framework (see attachment).
This is the subsystem I mean.
http://ellislab.com/codeigniter/user-guide/libraries/caching.html
First of all, I could only get it to work using the following ‘hack’. Can not really see what I do wrong or why it is neccessary, but it works:
$adapter = 'memcached';
$this->ci->load->driver('cache', array('adapter' => $adapter, 'backup' => 'dummy'));
// leave the following line in, for some reason the mechanism fails without it
$this->ci->cache->{$adapter}->is_supported();
After that it works like described in the link above. Nowhere I could find how to set the /config/memcached.php, but the default works for me. (Does someone now where???)
Currently the CI framework supports ‘memcached’, ‘apc’, ‘file’, ‘dummy’. Strangely enough no MySQL. Caching complex joins or multiple queries or complex output in a simple MySQL cache can be useful certainly in environment where memchached or apc is not available / possible. So I wrote a MySQL cache driver. I tested it, but not exhaustively yet.
However, I could not find or read or detect anywhere how extra drivers can be installed in the application part of the whole CI tree. So at the moment you will have to place it in /system/libraries/cache/drivers. Next to the other drivers.
What is even worse, is that to be able to use it, you will need to change a codeigniter file. While I do not recommend that, I point this out so that the CI boys and girls can do a little redesign of this subsystem. Allowing for some flexibility and extensibility such as custom cache drivers.
In /system/libraries/Cache/cache.php
Change (line 29,30,31):
protected $valid_drivers = array('cache_apc', 'cache_file', 'cache_memcached', 'cache_dummy');
To:
protected $valid_drivers = array('cache_apc', 'cache_file', 'cache_memcached', 'cache_dummy', 'cache_mysql');
After that you will be able to use MySQL caching. Like this:
$adapter = 'mysql';
$this->ci->load->driver('cache', array('adapter' => $adapter, 'backup' => 'dummy'));
// leave the following line in, for some reason the mechanism fails without it
$this->ci->cache->{$adapter}->is_supported();
You do need to create a MySQL table also:
CREATE TABLE IF NOT EXISTS ci_cache (
id CHAR(100) NOT NULL,
data LONGTEXT NOT NULL,
PRIMARY KEY (id) )
ENGINE = MyISAM;
Next change to CI I would like to suggest, is rewrite the output caching (/system/core/Output.php) to use the caching subsystem. I do have new Output.php that does just this if someone is interested.
