Clear APC cache in PHP

Mattias Geniar, Sunday, November 16, 2014 - last modified: Saturday, August 8, 2015

How do you clear the APC cache? There are basically two methods: as a PHP developer, you can use the built-in PHP functions -- or as a SysAdmin, you can restart the necessary services to flush the APC cache.

As a PHP developer

You can call the apc_clear_cache() function to clear the cache. To clear the user cache (key/value), you can use apc_cache_clear('user'). To clear the system cache, the one that holds the byte-code of the PHP files (the so called "opcode" cache), just call apc_cache_clear() without options.

As a System Administrator

This depends on the way you run your PHP application.

With Apache + mod_php

As PHP is run as an Apache module, it's sufficient to reload the Apache service. A restart is not needed (but it will also work), a reload is sufficient.

$ service httpd reload
$ service apache2 reload

With PHP-FPM

This can be used with Nginx, Lighttpd or even Apache. If your PHP server is running as a daemon via PHP-FPM, you can reload PHP-FPM.

$ service php-fpm reload

If this is how you're running your PHP stack, you may consider using multiple PHP-FPM masters as outlined in "A better way to run PHP-FPM", as it gives you an APC cache per PHP-FPM pool you are running.

As the built-in PHP server

If you start your PHP daemon via the built-in server, as php -S 127.0.0.1:80, you need to restart your daemon. That probably means sending a kill signal to the PHP process and starting it anew.

PHP as FastCGI

Not to be confused with running PHP as the PHP-FPM daemon, in the older days PHP was run as a FastCGI script. There's no need to clear the APC cache, as it gets invalidated on every new request -- each request starts a new process. In fact, you're better of disabling APC altogether, as it produces overhead that never gives you the benefits.

Want to flush the Opcache instead of APC? Check out this article: How to clear PHP's Opcache.



Hi! My name is Mattias Geniar. I'm a Support Manager at Nucleus Hosting in Belgium, a general web geek & public speaker. Currently working on DNS Spy & Oh Dear!. Follow me on Twitter as @mattiasgeniar.

Share this post

Did you like this post? Will you help me share it on social media? Thanks!

Comments

kim ausloos Tuesday, November 18, 2014 at 09:43 - Reply

There is a nice tool/library to clear apc as well: http://gordalina.github.io/cachetool/

Lets you check usage, see cached keys, clear it, …


Tit Petrič Thursday, January 5, 2017 at 16:16 - Reply

I’m pretty sure that APC works on FastCGI also (as I’m running an instance of one), and FastCGI doesn’t start a new process but (spawn-fcgi for example) maintains a process pool, alas differently than fpm. Children get restarted every 1000 requests or something (a configuration value). What you may be thinking of is CGI, which does what you say: spawns a new process per each request. I haven’t seen php used as CGI in over 10 years now.


Leave a Reply

Your email address will not be published. Required fields are marked *

Inbound links