APCIterator::__construct

(PECL apc >= 3.1.1)

APCIterator::__construct构造一个 APCIterator 迭代器对象

说明

public APCIterator::__construct ( string $cache [, mixed $search = NULL [, int $format = APC_ITER_ALL [, int $chunk_size = 100 [, int $list = APC_LIST_ACTIVE ]]]] )

构造一个 APCIterator object.

参数

cache

缓存类型,可以是 user 或者 file

search

匹配 APC 键名的 PCRE 正则表达式,既可以是单个正则表达式 string,也可以是 array 的正则表达式。或者可以是 NULL 来略过搜索。

format

需要的格式可以用一个或多个 APC_ITER_* 常量。

chunk_size

块的大小。必须是一个大于0的值,默认是100。

list

需要列出的类型。可以是 APC_LIST_ACTIVEAPC_LIST_DELETED

返回值

成功时返回 APCIterator object,失败时返回 NULL

范例

Example #1 APCIterator::__construct() 例子

<?php
foreach (new APCIterator('user''/^counter\./') as $counter) {
    echo 
"$counter[key]$counter[value]\n";
    
apc_dec($counter['key'], $counter['value']);
}
?>

参见

User Contributed Notes

puiumarius at gmail dot com 18-Apr-2014 09:13
There is a bug in APCIterator: whatever the chunk size, APCIterator gets ALL keys matching the specified pattern (it seems that $chunk_size parameter is ignored).

Also, the documentation states that the default chunk size is 100, but even that is ignored.

Tested APC version is 3.1.13.

Example (I have 180 keys for this pattern in total):
$objIterator = new APCIterator('user', '/^key_prefix_\.*/', APC_ITER_ALL, 20);

If I itterate $objIterator  and then count() the results, I get 180.

Also, for:
$objIterator = new APCIterator('user', '/^key_prefix_\.*/');
Same count(), 180.

Even $objIterator->getTotalCount() returns 180.
petabyte 10-Aug-2011 06:38
With MAMP (using PHP 5.3.5 and APC 3.1.7) passing an array with multiple regex strings as $search arg always yields an empty APCIterator. Whereas passing a single string regex works. (Note: I only tried cache type 'user'.)

Workaround: Concatenated all regexes to single one with alternation. Example:

<?php
$results
= new APCIterator('user', '/^('.implode('|', $patterns).')');
?>
Anti Veeranna 27-Oct-2010 12:46
Iterating (and specifically using current()) does not expunge cache entries for which $ttl has passed, you need to use apc_fetch to get rid of stale entries.