Developed with love by KnpLabs Hire us for your project!
0

OpcacheBundle

by sixdayz

Provides a command line task to clear opcache cache from the console

Provide a command line to clear opcache cache from the console.

The problem with opcache is that it's impossible to clear it from command line.
Because even if you enable opcache for PHP CLI, it's a different instance than,
say, your Apache PHP or PHP-CGI opcache instance.

The trick here is to create a file in the web dir, execute it through HTTP,
then remove it.

Installation

  1. Add it to your composer.json:

      {
          "require": {
              "sixdays/opcache-bundle": "dev-master"
          }
      }
    

    or:

          composer require sixdays/opcache-bundle
          composer update sixdays/opcache-bundle
    
  2. Add this bundle to your application kernel:

      // app/AppKernel.php
      public function registerBundles()
      {
          return array(
              // ...
              new Sixdays\OpcacheBundle\SixdaysOpcacheBundle(),
              // ...
          );
      }
    
  3. Configure sixdays_opcache service:

      # app/config/config.yml
      sixdays_opcache:
          base_url:   http://localhost/ #could also be https://, or http://127.0.0.1:8000/, or any other valid URL
          web_dir:    %kernel.root_dir%/../web
    

Usage

Clear all opcache cache:

      $ php app/console opcache:clear

Capifony usage

To automatically clear opcache cache after each capifony deploy you can define a custom task

namespace :symfony do
  desc "Clear opcache cache"
  task :clear_opcache do
    capifony_pretty_print "--> Clear opcache cache"
    run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} opcache:clear --env=#{symfony_env_prod}'"
    capifony_puts_ok
  end
end

and add this hook

# opcache
after "deploy", "symfony:clear_opcache"

Nginx configuration

If you are using nginx and limiting PHP scripts that you are passing to fpm you need to allow 'opcache' prefixed php files. Otherwise your web server will return the requested PHP file as text and the system won't be able to clear the opcache cache.

Example configuration:
```

Your virtual host

server {
...
location ~ ^/(app|app_dev|opcache-.*).php(/|$) { { # This will allow opcache (opcache-{MD5HASH}.php) files to be processed by fpm
fastcgi_pass 127.0.0.1:9000;
...
```

Copyright (c) 2013 Andrey Goryachev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.