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 . 'The cache' . PHP_EOL;
var_export($cache->getCache());
echo PHP_EOL;
echo $cache->offsetSet('0', 'manual change') . PHP_EOL;
echo $cache->offsetSet('3', 'manual entry') . PHP_EOL;
?>
The cache
array (
0 => 'a',
1 => 'b',
)
The cache
array (
0 => 'manual change',
1 => 'b',
3 => 'manual entry',
)
不需要在内部迭代器中存在偏移量,也不需要在缓存中存在偏移量。
<?php
$cache = new \CachingIterator(
new \ArrayIterator([]),
\CachingIterator::FULL_CACHE);
echo $cache->offsetSet('22', 'manual entry') . PHP_EOL;
echo PHP_EOL . 'The cache' . PHP_EOL;
var_export($cache->getCache());
echo PHP_EOL;
print_r("cache offset '22' " .
($cache->offsetExists('22') == 1
? 'exists'
: "doesn't exist"
) . PHP_EOL);
?>
The cache
array (
22 => 'manual entry',
)
cache offset '22' exists