CodeIgniter 用户指南 版本 2.0.0

PHP100中文网
查看原文

缓存适配器

CodeIgniter提供了多种目前业界流行的快速动态缓存组件的封装类。除了基于纯文本的缓存(文件缓存)外,其他缓存组件均需对服务器环境进行正确配置才能使用,否则程序会抛出致命异常(Fatal Exception)错误。

目录

支持的缓存适配器

用法举例

下面这个例子:首先加载缓存适配器,然后指定 APC 作为适配器优先使用的缓存实现,同时,我们指定文本缓存作为替代方案。这样,在一些服务器不支持APC的情况下(如国内的虚拟主机),我们可以使用替代方案保证程序正常运行。

$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;

译者注:为了便于理解上面的代码,我们不妨举个例子。缓存适配器,我们可以理解为一个“通电的插座”;而APC缓存,相当于我们希望插在插座上使用的空调;文本缓存我们可以理解与空调功能相同但效率却完全不同的电风扇。

Function Reference

is_supported(driver['string'])

如果你通过 $this->cache->get() 来访问缓存适配器,此函数将自动触发。但是,如果你希望有针对性的使用某个具体的缓存实现(如下例中的 APC),请确保调用此函数,用来检查服务器环境是否支持这种缓存类型。

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

get(id['string'])

此函数将尝试从缓存系统中获取指定的缓存项。如果缓存不存在,则返回 FALSE

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

save(id['string'], data['mixed'], ttl['int'])

此函数尝试将一个缓存项存储到对应的缓存系统中。如果存储失败,则返回FALSE

第三个参数(可选项)指定了缓存的存活时间,默认为60秒。

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

delete(id['string'])

此函数尝试从缓存系统中删除某个指定的缓存项。如果删除失败,则返回FALSE

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

clean()

此函数用来清空所有缓存。如果清空失败,则返回 FALSE.

$this->cache->clean();

cache_info()

此函数将返回所有缓存信息.

var_dump($this->cache->cache_info());

get_metadata(id['string'])

此函数将返回缓存系统中指定缓存项的详细信息。

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

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 functions 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

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.

 

翻译贡献者: cnsaturn, Hex, LSvKing
最后修改: 2011-02-10 15:21:13