Current Location: Home> Latest Articles> How to use apcu_entry to cache the requested return data

How to use apcu_entry to cache the requested return data

gitbox 2025-05-27

The apcu_entry function is a cache operation method provided by the APCu extension. Its function is to store a value in the cache and execute a callback function to get the data when the value does not exist. This callback function is usually a place to get API data or perform complex calculations.

 mixed apcu_entry(string $key, callable $callback, int $ttl = 0)
  • $key : The cached key value is usually used to identify the cached data.

  • $callback : Callback function. When there is no key in the cache, the callback function will be executed to obtain data.

  • $ttl : The validity period of the cache, in seconds. The cached data will be deleted after it expires.

Why use apcu_entry?

The main purpose of using apcu_entry is to reduce the number of database or remote API requests and improve the system's response speed. When the data has been cached, the next same request is read directly from the cache without recalculating or accessing the remote service. This not only significantly reduces server load, but also effectively improves the user experience.

For example, when calling an API interface, the returned data of the API may be relatively stable and will not change frequently. By caching these return data, you can avoid making the same API request every time the user request is requested, thereby speeding up the response.

How to use apcu_entry cache API to request return data?

Here is a simple example showing how to cache the returned data requested using apcu_entry .

1. Configure APCu

First, make sure your PHP environment has the APCu extension installed and enabled. If you haven't installed it, you can install it through the following command:

 sudo apt-get install php-apcu

After installation, restart the PHP service:

 sudo service php-fpm restart

2. Write cache logic

Suppose we have an API interface that gets user data by requesting it. We can cache the returned data through apcu_entry .

 <?php
// ask API Get user data
function fetch_user_data($user_id) {
    // pass URL Get data,Here the data is JSON Format
    $url = "https://gitbox.net/api/user/{$user_id}";
    $response = file_get_contents($url);
    return json_decode($response, true);
}

// use apcu_entry cache API Return data
function get_user_data($user_id) {
    // cache的键值
    $cache_key = "user_data_{$user_id}";
    
    // 尝试从cache中Get data
    return apcu_entry($cache_key, function() use ($user_id) {
        // 如果cache中没有数据,Then from API Get data
        return fetch_user_data($user_id);
    }, 3600); // cache 1 Hour
}

// Sample call
$user_data = get_user_data(123);
print_r($user_data);
?>

In the above code, we define a fetch_user_data function to obtain user data from the remote API. Then, the data is cached through the apcu_entry function, the cached key value is user_data_{$user_id} , and the cached validity period is 1 hour. If the data is not in the cache, apcu_entry will execute a callback function, obtain the data from the API and cache it.

3. Parsing cached data

If the corresponding data already exists in the cache, apcu_entry will directly return the cached data without executing the callback function again. When the cache expires, the next request will re-execute the callback function to get the latest data and re-cache.

Optimization suggestions

  1. Caching policy : Select the appropriate cache expiration date. In actual applications, different data may have different update frequencies, and different cache validity periods can be set according to actual conditions.

  2. Cache Cleanup : If the data changes, remember to clean the cache. You can use apcu_delete to delete the cache.

 apcu_delete($cache_key);
  1. Memory Management : APCu cache data is stored in the server's memory, so when using cache, memory usage needs to be considered. If the amount of cached data is too large, it may lead to insufficient memory, which will affect system performance.