at 0xb3870d4>
CodeIgniter

3.0-dev User Guide

Caching Driver

CodeIgniter features wrappers around some of the most popular forms of fast and dynamic caching. All but file-based caching require specific server requirements, and a Fatal Exception will be thrown if server requirements are not met.

Example Usage

The following example will load the cache driver, specify APC as the driver to use, and fall back to file-based caching if APC is not available in the hosting environment.

$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));

if ( ! $foo = $this->cache->get('foo'))
{
        echo 'Saving to the cache!<br />';
        $foo = 'foobarbaz!';

        // Save into the cache for 5 minutes
        $this->cache->save('foo', $foo, 300);
}

echo $foo;

You can also prefix cache item names via the key_prefix setting, which is useful to avoid collisions when you’re running multiple applications on the same environment.

$this->load->driver('cache',
        array('adapter' => 'apc', 'backup' => 'file', 'key_prefix' => 'my_')
);

$this->cache->get('foo'); // Will get the cache entry named 'my_foo'

Function Reference

class CI_Cache

is_supported()

CI_Cache::is_supported($driver)

This function is automatically called when accessing drivers via $this->cache->get(). However, if the individual drivers are used, make sure to call this function to ensure the driver is supported in the hosting environment.

Parameters:
  • $driver (string) – the name of the caching driver
Returns:

TRUE if supported, FALSE if not

Return type:

Boolean

if ($this->cache->apc->is_supported()
{
        if ($data = $this->cache->apc->get('my_cache'))
        {
                // do things.
        }
}

get()

CI_Cache::get($id)

This function will attempt to fetch an item from the cache store. If the item does not exist, the function will return FALSE.

Parameters:
  • $id (string) – name of cached item
Returns:

The item if it exists, FALSE if it does not

Return type:

Mixed

$foo = $this->cache->get('my_cached_item');

save()

CI_Cache::save($id, $data[, $ttl])

This function will save an item to the cache store. If saving fails, the function will return FALSE.

Parameters:
  • $id (string) – name of the cached item
  • $data (mixed) – the data to save
  • $ttl (int) – Time To Live, in seconds (default 60)
Returns:

TRUE on success, FALSE on failure

Return type:

Boolean

$this->cache->save('cache_item_id', 'data_to_cache');

delete()

CI_Cache::delete($id)

This function will delete a specific item from the cache store. If item deletion fails, the function will return FALSE.

Parameters:
  • $id (string) – name of cached item
Returns:

TRUE if deleted, FALSE if the deletion fails

Return type:

Boolean

$this->cache->delete('cache_item_id');

clean()

CI_Cache::clean()

This function will ‘clean’ the entire cache. If the deletion of the cache files fails, the function will return FALSE.

Returns:TRUE if deleted, FALSE if the deletion fails
Return type:Boolean
$this->cache->clean();

cache_info()

CI_Cache::cache_info()

This function will return information on the entire cache.

Returns:information on the entire cache
Return type:Mixed
var_dump($this->cache->cache_info());

Note

The information returned and the structure of the data is dependent on which adapter is being used.

get_metadata()

CI_Cache::get_metadata($id)

This function will return detailed information on a specific item in the cache.

Parameters:
  • $id (string) – name of cached item
Returns:

metadadta for the cached item

Return type:

Mixed

var_dump($this->cache->get_metadata('my_cached_item'));

Note

The information returned and the structure of the data is dependent on which adapter is being used.

Drivers

Alternative PHP Cache (APC) Caching

All of the functions listed above can be accessed without passing a specific adapter to the driver loader as follows:

$this->load->driver('cache');
$this->cache->apc->save('foo', 'bar', 10);

For more information on APC, please see http://php.net/apc.

File-based Caching

Unlike caching from the Output Class, the driver file-based caching allows for pieces of view files to be cached. Use this with care, and make sure to benchmark your application, as a point can come where disk I/O will negate positive gains by caching.

All of the functions listed above can be accessed without passing a specific adapter to the driver loader as follows:

$this->load->driver('cache');
$this->cache->file->save('foo', 'bar', 10);

Memcached Caching

Multiple Memcached servers can be specified in the memcached.php configuration file, located in the _application/config/* directory.

All of the methods listed above can be accessed without passing a specific adapter to the driver loader as follows:

$this->load->driver('cache');
$this->cache->memcached->save('foo', 'bar', 10);

For more information on Memcached, please see http://php.net/memcached.

WinCache Caching

Under Windows, you can also utilize the WinCache driver.

All of the functions listed above can be accessed without passing a specific adapter to the driver loader as follows:

$this->load->driver('cache');
$this->cache->wincache->save('foo', 'bar', 10);

For more information on WinCache, please see http://php.net/wincache.

Redis Caching

All of the methods listed above can be accessed without passing a specific adapter to the driver loader as follows:

$this->load->driver('cache');
$this->cache->redis->save('foo', 'bar', 10);

Important

Redis may require one or more of the following options: host, post, timeout, password.

The Redis PHP extension repository is located at https://github.com/nicolasff/phpredis.

Dummy Cache

This is a caching backend that will always ‘miss.’ It stores no data, but lets you keep your caching code in place in environments that don’t support your chosen cache.