Current Location: Home> Latest Articles> How to set the valid time of apcu_entry to ensure cache refresh

How to set the valid time of apcu_entry to ensure cache refresh

gitbox 2025-05-26

The apcu_entry function is a function provided by the APCu extension to set cached data. Unlike apcu_store and apcu_fetch , apcu_entry can automatically execute a callback function to generate data when the cache does not exist, thus avoiding duplicate data generation process.

The function prototype is as follows:

 mixed apcu_entry ( string $key , callable $callback , int $ttl = 0 )
  • $key : The cached key, uniquely identifying the cached data.

  • $callback : A callback function used to generate cached data. If the cache does not exist, APCu will execute the callback function to generate data.

  • $ttl : The valid time (in seconds) of cached data. The default is 0 , which means that the cache will never expire.

2. Set the cache valid time

In apcu_entry , the ttl parameter is used to set the valid time of the cache. After the validity time expires, the cache will expire and APCu will regenerate the cached data. By setting ttl reasonably, we can ensure that cached data is updated in time and avoid the problems caused by cache expiration.

Example:

Suppose we have a database query result that needs frequent access and want to cache it for 5 minutes (300 seconds). You can use apcu_entry to set the valid time of the cache, as shown below:

 <?php
// Suppose this is a function that querys the database
function fetchDataFromDatabase() {
    // Simulate getting data from a database
    return "This is the data fetched from the database!";
}

// use apcu_entry Cache query results,And set the cache valid time to 5 minute
$data = apcu_entry('database_data', 'fetchDataFromDatabase', 300);

// Output cache or database query results
echo $data;
?>

In this example, when apcu_entry is called, if there is no data named 'database_data' in the cache, APCu will execute the fetchDataFromDatabase function to get the data and cache the result for 5 minutes. If the cache is requested again within 5 minutes, APCu will directly return the cached data.

3. Refresh the cache in time

In some cases, changes in data may not match the expiration date of the cache. To ensure that the cached data is always up to date, we can refresh the cache programmatically. This can delete the cache by calling apcu_delete at the appropriate time and then regenerating the cache via apcu_entry .

Example: Manually refresh the cache

 <?php
// Suppose we need to refresh the cache
if (someConditionToRefreshCache()) {
    // Delete old cached data
    apcu_delete('database_data');
}

// Then regenerate cached data
$data = apcu_entry('database_data', 'fetchDataFromDatabase', 300);

// Output cache or database query results
echo $data;
?>

In this example, the cache will be manually refreshed only when a certain condition is met. This is a strategy to ensure that cached content is updated in a timely manner.

4. Things to note

  • Choosing cache expiration time : It is very important to set the appropriate cache expiration time. If the setting time is too long, the data may not be updated for a long time; if the setting is too short, the cache may be regenerated frequently, resulting in performance losses.

  • Thread safety : apcu_entry itself is thread-safe, but in a high concurrency environment, it is very critical to ensure that the callback function itself can be executed safely.