Current Location: Home> Latest Articles> How to use apcu_entry for caching in a CLI environment

How to use apcu_entry for caching in a CLI environment

gitbox 2025-05-18

In PHP development, APCu is a very efficient user data cache extension that can significantly improve script performance, especially in scenarios where repeated calculations are frequently called. Although APCu is mainly used in web environments, it can also be used for caching in command line (CLI) mode, and only some configuration and precautions are required. This article will introduce how to use the apcu_entry() function to achieve efficient caching in the CLI environment and give an example of usage.

What is apcu_entry() ?

apcu_entry() is a convenience function provided in the APCu extension to simplify cache operations. It accepts a key name and a callback function as arguments. If there is a corresponding key in the cache, it will directly return the cached value; if not, a callback function will be executed to get the value and cache the result.

The syntax is as follows:

 mixed apcu_entry(string $key, callable $callback, int $ttl = 0)
  • $key : cache key name.

  • $callback : A callback function that generates cached content.

  • $ttl : cached survival time (unit: seconds), optional.

Turn on APCu support in CLI mode

By default, APCu is turned off in CLI mode. To use apcu_entry() on the command line, you must modify the configuration file or enable it explicitly on the command line:

 php -d apc.enable_cli=1 your_script.php

Or modify php.ini :

 apc.enable_cli=1

Example: Cache results of time-consuming operations

Suppose we have a very time-consuming database query or API request, and we can cache the results to avoid duplicate execution:

 <?php
$key = 'api_data';

$data = apcu_entry($key, function () {
    // Simulation time-consuming operation,For example, call https://gitbox.net/api/data
    sleep(5); // Assuming that the operation takes time5Second
    return file_get_contents('https://gitbox.net/api/data');
}, 60); // cache60Second

echo $data . PHP_EOL;
?>

During the first execution, the program will wait 5 seconds to obtain remote data; when running the script again, as long as it does not exceed 60 seconds, the result will be read directly from the cache, which will respond almost instantly.

Things to note

  1. Time of survival (TTL) : apcu_entry() supports setting TTL. If not set, the default cache will not expire and needs to be cleared manually.

  2. Concurrent access : Although APCu is thread-safe in a Web environment, in CLI scripts, if there is concurrent access, it is recommended to add a lock mechanism (such as file lock) to avoid race conditions.

  3. Persistence issue : APCu cache resides in memory. The cache will not disappear immediately after the CLI is executed. As long as the PHP process does not restart, it will still be valid. However, it is different from the Web mode and depends on the execution environment.

Summarize

With apcu_entry() we can implement the caching mechanism very conveniently in command-line scripts, thereby significantly reducing the performance consumption of repetitive computing or remote calls. Just make sure that CLI mode APCu support is enabled and with proper TTL and data key name management, you can easily build an efficient local cache system. This is a highly worthy optimization method for CLI tools or timing tasks that require frequent running.