offsetSet($index, $newval) 將會更改現有的快取值或建立新的快取項目
<?php
$cache = new \CachingIterator(
new \ArrayIterator(['a', 'b', 'c', 'd']),
\CachingIterator::FULL_CACHE);
$shortRange = range(0, 1);
foreach ($shortRange as $index) {
$cache->next();
}
echo PHP_EOL . '快取' . PHP_EOL;
var_export($cache->getCache());
echo PHP_EOL;
echo $cache->offsetSet('0', '手動變更') . PHP_EOL;
echo $cache->offsetSet('3', '手動新增') . PHP_EOL;
?>
快取
array (
0 => 'a',
1 => 'b',
)
快取
array (
0 => '手動變更',
1 => 'b',
3 => '手動新增',
)
索引鍵不需要存在於內部迭代器中,也不需要存在於快取中。
<?php
$cache = new \CachingIterator(
new \ArrayIterator([]),
\CachingIterator::FULL_CACHE);
echo $cache->offsetSet('22', '手動新增') . PHP_EOL;
echo PHP_EOL . '快取' . PHP_EOL;
var_export($cache->getCache());
echo PHP_EOL;
print_r("快取索引 '22' " .
($cache->offsetExists('22') == 1
? '存在'
: "不存在"
) . PHP_EOL);
?>
快取
array (
22 => '手動新增',
)
快取索引 '22' 存在