當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP CachingIterator getCache()用法及代碼示例


CachingIterator::getCache()函數是PHP中的內置函數,用於檢索緩存的內容。

用法:

array CachingIterator::getCache( void )

參數:該函數不接受任何參數。


返回值:該函數返回一個包含緩存項的數組。

以下示例程序旨在說明PHP中的CachingIterator::getCache()函數:

示例1:

<?php 
    
// Declare an array 
$arr = array('G', 'e', 'e', 'k', 's'); 
    
// Create a new CachingIterator 
$cachIt = new CachingIterator( 
    new ArrayIterator($arr),  
    CachingIterator::FULL_CACHE 
); 
   
// Move to next position 
$cachIt->next(); 
  
// Display the content of cache 
var_dump($cachIt->getCache()); 
  
// Move to next position 
$cachIt->next(); 
  
// Display the content of cache 
var_dump($cachIt->getCache()); 
    
?>
輸出:
array(1) {
  [0]=>
  string(1) "G"
}
array(2) {
  [0]=>
  string(1) "G"
  [1]=>
  string(1) "e"
}

示例2:

<?php 
    
// Declare an ArrayIterator 
$arr = array( 
    "a" => "Geeks", 
    "b" => "for", 
    "c" => "Geeks", 
    "d" => "Computer", 
    "e" => "Science", 
    "f" => "Portal"
); 
  
// Create a new CachingIterator 
$cachIt = new CachingIterator( 
    new ArrayIterator($arr),  
    CachingIterator::FULL_CACHE 
); 
  
// Move to next position 
$cachIt->next(); 
$cachIt->next(); 
$cachIt->next(); 
  
// Display the content of cache 
var_dump($cachIt->getCache()); 
  
// Move to next position 
$cachIt->next(); 
  
// Display the content of cache 
var_dump($cachIt->getCache()); 
    
?>
輸出:
array(3) {
  ["a"]=>
  string(5) "Geeks"
  ["b"]=>
  string(3) "for"
  ["c"]=>
  string(5) "Geeks"
}
array(4) {
  ["a"]=>
  string(5) "Geeks"
  ["b"]=>
  string(3) "for"
  ["c"]=>
  string(5) "Geeks"
  ["d"]=>
  string(8) "Computer"
}

參考: https://www.php.net/manual/en/cachingiterator.getcache.php



相關用法


注:本文由純淨天空篩選整理自jit_t大神的英文原創作品 PHP | CachingIterator getCache() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。