How to clear APC cache entries using PHP ?
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.
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.
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.