99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

CodeIgniter 緩存驅(qū)動器

2018-07-21 15:38 更新

緩存驅(qū)動器

CodeIgniter 提供了幾種最常用的快速緩存的封裝,除了基于文件的緩存, 其他的緩存都需要對服務(wù)器進行特殊的配置,如果配置不正確,將會拋出 一個致命錯誤異常(Fatal Exception)。

使用示例

下面的示例代碼用于加載緩存驅(qū)動器,使用 APC 作為緩存, 如果 APC 在服務(wù)器環(huán)境下不可用,將降級到基于文件的緩存。

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

你也可以設(shè)置 key_prefix 參數(shù)來給緩存名添加前綴,當你在同一個環(huán)境下運行多個應(yīng)用時,它可以避免沖突。

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

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

類參考

classCI_Cache

is_supported($driver)

參數(shù):

  • $driver (string) -- the name of the caching driver

返回: TRUE if supported, FALSE if not

返回類型: bool

當使用 $this->cache->get() 方法來訪問驅(qū)動器時該方法會被自動調(diào)用,但是,如果你使用了某些個人的驅(qū)動器, 應(yīng)該先調(diào)用該方法確保這個驅(qū)動器在服務(wù)器環(huán)境下是否被支持。

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

get($id)

參數(shù):

  • $id (string) -- Cache item name

返回: Item value or FALSE if not found

返回類型: mixed

該方法用于從緩存中獲取一項條目,如果獲取的條目不存在,方法返回 FALSE 。

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

save($id, $data[, $ttl = 60[, $raw = FALSE]])

參數(shù):

  • $id (string) -- Cache item name
  • $data (mixed) -- the data to save
  • $ttl (int) -- Time To Live, in seconds (default 60)
  • $raw (bool) -- Whether to store the raw value

返回: TRUE on success, FALSE on failure

返回類型: string

該方法用于將一項條目保存到緩存中,如果保存失敗,方法返回 FALSE 。

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

注解

參數(shù) $raw 只有在使用 APC 和 Memcache 緩存時才有用, 它用于 increment() 和 decrement() 方法。

delete($id)

參數(shù):

  • $id (string) -- name of cached item

返回: TRUE on success, FALSE on failure

返回類型: bool

該方法用于從緩存中刪除一項指定條目,如果刪除失敗,方法返回 FALSE 。

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

increment($id[, $offset = 1])

參數(shù):

  • $id (string) -- Cache ID
  • $offset (int) -- Step/value to add

返回: New value on success, FALSE on failure

返回類型: mixed

對緩存中的值執(zhí)行原子自增操作。

// 'iterator' has a value of 2

$this->cache->increment('iterator'); // 'iterator' is now 3

$this->cache->increment('iterator', 3); // 'iterator' is now 6

decrement($id[, $offset = 1])

參數(shù):

  • $id (string) -- Cache ID
  • $offset (int) -- Step/value to reduce by

返回: New value on success, FALSE on failure

返回類型: mixed

對緩存中的值執(zhí)行原子自減操作。

// 'iterator' has a value of 6

$this->cache->decrement('iterator'); // 'iterator' is now 5

$this->cache->decrement('iterator', 2); // 'iterator' is now 3

clean()

返回: TRUE on success, FALSE on failure

返回類型: bool

該方法用于清空整個緩存,如果清空失敗,方法返回 FALSE 。

$this->cache->clean();

cache_info()

返回: Information on the entire cache database

返回類型: mixed

該方法返回整個緩存的信息。

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

注解

返回的信息以及數(shù)據(jù)結(jié)構(gòu)取決于使用的緩存驅(qū)動器。

get_metadata($id)

參數(shù):

  • $id (string) -- Cache item name

返回: Metadata for the cached item

返回類型: mixed

該方法用于獲取緩存中某個指定條目的詳細信息。

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

注解

返回的信息以及數(shù)據(jù)結(jié)構(gòu)取決于使用的緩存驅(qū)動器。

驅(qū)動器

可選 PHP 緩存(APC)

上述所有方法都可以直接使用,而不用在加載驅(qū)動器時指定 adapter 參數(shù),如下所示:

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

關(guān)于 APC 的更多信息,請參閱 http://php.net/apc

基于文件的緩存

和輸出類的緩存不同的是,基于文件的緩存支持只緩存視圖的某一部分。使用這個緩存時要注意, 確保對你的應(yīng)用程序進行基準測試,因為當磁盤 I/O 頻繁時可能對緩存有負面影響。

上述所有方法都可以直接使用,而不用在加載驅(qū)動器時指定 adapter 參數(shù),如下所示:

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

Memcached 緩存

可以在 memcached.php 配置文件中指定多個 Memcached 服務(wù)器,配置文件位于 application/config/ 目錄。

上述所有方法都可以直接使用,而不用在加載驅(qū)動器時指定 adapter 參數(shù),如下所示:

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

關(guān)于 Memcached 的更多信息,請參閱 http://php.net/memcached

WinCache 緩存

在 Windows 下,你還可以使用 WinCache 緩存。

上述所有方法都可以直接使用,而不用在加載驅(qū)動器時指定 adapter 參數(shù),如下所示:

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

關(guān)于 WinCache 的更多信息,請參閱 http://php.net/wincache

Redis 緩存

Redis 是一個在內(nèi)存中以鍵值形式存儲數(shù)據(jù)的緩存,使用 LRU(最近最少使用算法)緩存模式, 要使用它,你需要先安裝 Redis 服務(wù)器和 phpredis 擴展。

連接 Redis 服務(wù)器的配置信息必須保存到 application/config/redis.php 文件中,可用參數(shù)有:

$config['socket_type'] = 'tcp'; //`tcp` or `unix`
$config['socket'] = '/var/run/redis.sock'; // in case of `unix` socket type
$config['host'] = '127.0.0.1';
$config['password'] = NULL;
$config['port'] = 6379;
$config['timeout'] = 0;

上述所有方法都可以直接使用,而不用在加載驅(qū)動器時指定 adapter 參數(shù),如下所示:

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

關(guān)于 Redis 的更多信息,請參閱 http://redis.io

虛擬緩存(Dummy Cache)

這是一個永遠不會命中的緩存,它不存儲數(shù)據(jù),但是它允許你在當使用的緩存在你的環(huán)境下不被支持時, 仍然保留使用緩存的代碼。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號