Open In App

How to clear APC cache entries using PHP ?

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Syntax:

apc_clear_cache();

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

apc_clear_cache('user');

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: 

PHP




<?php
 
if (in_array(@$_SERVER['REMOTE_ADDR'],
    array('127.0.0.1', '::1', 'YOUR_IP'))) {
    apcu_clear_cache();
    echo "<script>alert('success!')</script>";
}
else {
    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: 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <meta name="viewport" content=
        "width=device-width, initial-scale=1">
</head>
 
<body>
    <div style="display: flex;">
        <div style="flex: 30%; padding-left: 4%;">
            <h1>Current Cache Info</h1>
            <?php
                //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" />
            </form>
        </div>
    </div>
</body>
 
</html>


“aboutus.php” file: 

php




<?php
 
// When form is submitted "clear
// cache button pressed"
if(isset($_POST['submit']) ) {
     
    // Check IP is Valid     
    if (in_array(@$_SERVER['REMOTE_ADDR'],
    array('127.0.0.1', '::1', '192.168.10.7'))) {
    
        // Clear cache
        apcu_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>";
    print_r(apcu_cache_info());
}
?>


Output:

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.

Output:

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.



Last Updated : 29 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads