Skip to content
Related Articles

Related Articles

How to clear APC cache entries using PHP ?

View Discussion
Improve Article
Save Article
  • Last Updated : 27 Jul, 2020
View Discussion
Improve Article
Save Article

Alternative PHP Cache (APC) stores the bytecode of previous PHP compilations so we do not need to recompile every time thereby saving time. We have to clean the cache entries for a fresh compilation. For this purpose, we can use the PHP function apc_clear_cache() to remove cache entries. This command clears the cache and returns “true” on success.



This command will remove system cache. If you want to clear cache for user, then simply provide “user” as the parameter.


Example: To clear APC cache, copy and paste the following code in your PHP file. Be sure to replace the string ‘Your IP address’ with your actual IP Address. If the cache is cleared successfully, an alert box with a “success” message is shown otherwise “error” alert message is displayed.

PHP Code:


if (in_array(@$_SERVER['REMOTE_ADDR'], 
    array('', '::1', 'YOUR_IP'))) { 
    echo "<script>alert('success!')</script>"
    die('No valid IP'); 

Now lets us run this code for any webpage. Check out the following code. We have created a simple web page and we have added a variable to APC cache. We have created a variable “$name” with value “abc” and saved it to APC cache using the function apcu_add() with parameters where “name” is a unique key, $name is the actual variable and the last argument is TTL (Time To Live) i.e. the time for the variable stored in the cache.
To verify the variable, we have displayed cache information using function apcu_cache_info() and we see our variable is added to cache.

HTML Code:


<!DOCTYPE html>
    <meta name="viewport" content=
        "width=device-width, initial-scale=1">
    <div style="display: flex;">
        <div style="flex: 30%; padding-left: 4%;">
            <h1>Current Cache Info</h1>
                //create sample variable
                $name = "abc";
                //save to APC cache         
                apcu_add ( "name", $name, 5  );
                //print APC Cache to see variable
                // has been added
                print_r(apcu_cache_info()); ?>
                //form to clear cache
            <form action="aboutus.php" method="POST">
                <input type="submit" name="submit" 
                    value="clear cache" />

“aboutus.php” file:

// When form is submited "clear
// cache button pressed" 
if(isset($_POST['submit']) ) {
    // Check IP is Valid      
    if (in_array(@$_SERVER['REMOTE_ADDR'], 
    array('', '::1', ''))) {
        // Clear cache
        // Show success alert
        echo "<script>alert('success!')</script>";
    else {  
        // Error
        die('No valid IP'); 
    // Display updated cache info
    // after clearing
    echo "<h1>New cache info</h1>";


We can see our name variable that has been added to the cache as the total entries are one and in info, we can see our name variable.


Now to clear the cache we press “clear cache” button. The cache is cleared and displayed. We see that the new cache entries are 0 and info is empty thereby showing that cache has been cleared.

We can compare both the caches and check the command executed successfully.

The cache is successfully cleared.

Note: If you are not a developer but a system administrator, you can clear the cache by reloading your server and if it is not work, kill the server signal and restart.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!