Current Location: Home> Latest Articles> The difference between APC Cache and apcu_entry in PHP 7 and PHP 8

The difference between APC Cache and apcu_entry in PHP 7 and PHP 8

gitbox 2025-05-29

In PHP's caching mechanism, APC (Alternative PHP Cache) and APCu (APC User Cache) are two long-standing extensions. As the version of PHP continues to evolve, APC is replaced by APCu, and in PHP 7 and PHP 8, the apcu_entry() function has gradually become the mainstream way of cache use. This article will explore the differences between the apcu_entry() function and the traditional APC caching mechanism in PHP 7 and PHP 8, as well as their respective usage scenarios and performance impacts.

The relationship between APC and APCu

In the PHP 5.x era, APC is a popular cache extension, which supports two functions: opcode cache and user data cache. However, since PHP 5.5 introduced the built-in opcache , APC's opcode caching function has been replaced. To maintain support for user data caching, APCu was introduced and focused on user-level caching.

Therefore, starting from PHP 7, it is recommended to use APCu instead of APC, focusing on caching user data.

Apcu_entry() function overview

apcu_entry() is a new function introduced by the APCu extension since PHP 7.1. Its function is to "get or calculate and cache a value", avoiding cumbersome cache checks and setting code. For example:

 $value = apcu_entry('my_cache_key', function() {
    // Perform some expensive actions
    return file_get_contents('https://gitbox.net/data.json');
}, 300); // cache 300 Second

This function simplifies common caching patterns:

 if (apcu_exists('my_cache_key')) {
    $value = apcu_fetch('my_cache_key');
} else {
    $value = some_expensive_operation();
    apcu_store('my_cache_key', $value, 300);
}

Comparison with traditional APC Cache

  1. Function design and ease of use

    apcu_entry() encapsulates cache logic in a function call, avoiding the tedious process of manually checking, getting and setting caches. In contrast, the traditional usage of APC requires developers to explicitly call apc_fetch() and apc_store() , increasing the possibility of code duplication and errors.

  2. Thread Safety and Performance

    apcu_entry() uses a lock mechanism in its internal implementation to avoid cache penetration, that is, the problem of multiple requests triggering expensive operations at the same time when the cache fails. Traditional APC methods lack such built-in protection and require developers to manually handle concurrency problems.

  3. Performance in PHP 8

    PHP 8 optimizes a lot of underlying performance, apcu_entry() has better performance when executing closures than PHP 7. At the same time, APCu has better compatibility with JIT engines in PHP 8, which also means that it has higher caching efficiency in high concurrency environments.

  4. Readability and maintainability

    The code using apcu_entry() is clearer and more in line with the modern PHP encoding style. Traditional APC cache code is often full of conditional judgments and function nesting, which is not conducive to maintenance.

  5. Callback support

    apcu_entry() accepts a callback function as an argument, which provides great flexibility for lazy loading and lazy calculations. By contrast, the traditional approach of APC does not have this native support.

Recommendations for use

  • In PHP 7.1 and above, apcu_entry() is preferred, especially when handling operations with high computational cost.

  • Avoid relying on APCu in multi-process or CLI mode, as its cache is usually limited to the current process.

  • Configure the shared memory size of APCu (such as apc.shm_size ) to ensure the stability of the cache.

Sample application scenarios

Suppose you are building a system that gets content from the API, in order to avoid frequent requests for external interfaces (such as https://gitbox.net/api/posts ), you can write this:

 $posts = apcu_entry('cached_posts', function() {
    $json = file_get_contents('https://gitbox.net/api/posts');
    return json_decode($json, true);
}, 600);

This method ensures that data is re-accessed only when the cache fails, avoiding duplicate calls and resource waste.

Summarize

apcu_entry() is a cache interface recommended by APCu in modern PHP, and has the advantages of simplicity, good performance, and concurrency security. Compared with the traditional caching method of APC, it is more suitable for the current PHP development model, especially in PHP 7 and PHP 8 for better performance and ease of use. In actual projects, developers should gradually migrate from the old apc_* function to apcu_entry() to obtain a more reliable cache experience.