To use apcu, the apcu extension has to be installed. You can find it here https://pecl.php.net/package/APCu
Note: apcu is not the same as apc!
APCu is the official replacement for the outdated APC extension. APC provided both opcode caching (opcache) and object caching. As PHP versions 5.5 and above include their own opcache, APC was no longer compatible, and its opcache functionality became useless. The developers of APC then created APCu, which offers only the object caching (read "in memory data caching") functionality (they removed the outdated opcache).
Wondering how to use apcu? The following example should give you a basic understanding.
<?php
date_default_timezone_set('Europe/Amsterdam');
$apcuAvailabe = function_exists('apcu_enabled') && apcu_enabled();
if($apcuAvailabe)
{
//apcu_clear_cache();
$test1 = apcu_fetch('test1');
$test2 = apcu_fetch('test2');
}
$test1[] = rand(1, 1000);
$test2[] = rand(1, 1000);
if($apcuAvailabe)
{
apcu_store('test1', $test1);
apcu_store('test2', $test2);
}
echo sprintf('current - value = %s<br/>', implode(' ,', $test1));
echo sprintf('current - value = %s<br/>', implode(' ,', $test2));
$aPCUIterator = new APCUIterator();
echo sprintf('totalCount = %s<br/>', $aPCUIterator->getTotalCount());
//echo sprintf('totalHits = %s<br/>', $aPCUIterator->getTotalHits()); // Not implemneted/available?
echo sprintf('totalSize = %s<br/>', $aPCUIterator->getTotalSize());
echo '----------------------------------<br/>';
$aPCUIterator->rewind();
echo sprintf('key = %s<br/>', $aPCUIterator->key());
echoCurrent($aPCUIterator->current());
$aPCUIterator->next();
echo '----------------------------------<br/>';
echo sprintf('key = %s<br/>', $aPCUIterator->key());
echoCurrent($aPCUIterator->current());
echo sprintf('valid = %s<br/>', $aPCUIterator->valid() ? 'true' : 'false');
function echoCurrent($current)
{
echo sprintf('current - type = %s<br/>', $current['type']);
echo sprintf('current - key = %s<br/>', $current['key']);
echo sprintf('current - value = %s<br/>', implode(' ,', $current['value']));
//echo sprintf('current - num_hits = %s<br/>', $current['num_hits']); // Not implemneted/available?
echo sprintf('current - mtime = %s<br/>', date("d-m-Y H:i:s", $current['mtime']));
echo sprintf('current - creation_time = %s<br/>', date("d-m-Y H:i:s", $current['creation_time']));
echo sprintf('current - deletion_time = %s<br/>', date("d-m-Y H:i:s", $current['deletion_time']));
echo sprintf('current - access_time = %s<br/>', date("d-m-Y H:i:s", $current['access_time']));
//echo sprintf('current - ref_count = %s<br/>', $current['ref_count']); // Not implemneted/available?
echo sprintf('current - mem_size = %s<br/>', $current['mem_size']);
//echo sprintf('current - ttl = %s<br/>', $current['ttl']); // Not implemneted/available?
}
?>| CARVIEW |
Select Language
HTTP/2 301
server: myracloud
date: Sun, 28 Dec 2025 11:55:40 GMT
content-type: text/html
content-length: 161
location: https://www.php.net/apcu
HTTP/2 200
server: myracloud
date: Sun, 28 Dec 2025 11:55:41 GMT
content-type: text/html; charset=utf-8
content-language: en
permissions-policy: interest-cohort=()
x-frame-options: SAMEORIGIN
status: 200 OK
link: ; rel=shorturl
last-modified: Sun, 28 Dec 2025 10:08:17 GMT
vary: accept-encoding
content-encoding: gzip
expires: Sun, 28 Dec 2025 11:55:41 GMT
cache-control: max-age=0
PHP: APCu - Manual
update page now
APC User Cache
- Introduction
- Installing/Configuring
- Predefined Constants
- APCu Functions
- apcu_add — Cache a new variable in the data store
- apcu_cache_info — Retrieves cached information from APCu's data store
- apcu_cas — Updates an old value with a new value
- apcu_clear_cache — Clears the APCu cache
- apcu_dec — Decrease a stored number
- apcu_delete — Removes a stored variable from the cache
- apcu_enabled — Whether APCu is usable in the current environment
- apcu_entry — Atomically fetch or generate a cache entry
- apcu_exists — Checks if entry exists
- apcu_fetch — Fetch a stored variable from the cache
- apcu_inc — Increase a stored number
- apcu_key_info — Get detailed information about the cache key
- apcu_sma_info — Retrieves APCu Shared Memory Allocation information
- apcu_store — Cache a variable in the data store
- APCUIterator — The APCUIterator class
- APCUIterator::__construct — Constructs an APCUIterator iterator object
- APCUIterator::current — Get current item
- APCUIterator::getTotalCount — Get total count
- APCUIterator::getTotalHits — Get total cache hits
- APCUIterator::getTotalSize — Get total cache size
- APCUIterator::key — Get iterator key
- APCUIterator::next — Move pointer to next item
- APCUIterator::rewind — Rewinds iterator
- APCUIterator::valid — Checks if current position is valid
+add a note
User Contributed Notes 1 note
boefje at hotmail dot com ¶
5 years ago
↑ and ↓ to navigate •
Enter to select •
Esc to close • / to open
Press Enter without
selection to search using Google