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

AcceleratorCacheBundle

by Smart-Core

Provide a command to clear PHP Accelerator cache from CLI

Provide a command line to clear PHP Accelerator cache from the console.

The problem with Accelerator cache (like APC, Wincache, Opcache) is that it's impossible to clear it from command line.
Because even if you enable APC for PHP CLI, it's a different instance than,
say, your Apache PHP or PHP-CGI APC instance.

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

Prerequisite

If you want to clear Apache part of APC, you will need to enable allow_url_fopen in php.ini to allow opening of URL
object-like files, or set the curl option.

Installation

  1. Add it to your composer.json:

    {
      "require": {
          "smart-core/accelerator-cache-bundle": "dev-master"
      }
    }
    

    or:

      composer require smart-core/accelerator-cache-bundle
      composer update smart-core/accelerator-cache-bundle
    
  2. Add this bundle to your application kernel:

     // app/AppKernel.php
     public function registerBundles()
     {
         return array(
             // ...
             new SmartCore\Bundle\AcceleratorCacheBundle\AcceleratorCacheBundle(),
             // ...
         );
     }
    
  3. Configure accelerator_cache service:

    # app/config/config.yml
    accelerator_cache:
        host: http://example.com
        web_dir: %kernel.root_dir%/../web
    
  4. If you want to use curl rather than fopen set the following option:

    # app/config/config.yml
    accelerator_cache:
        ...
        mode: curl
    #   additional options can be passed to the command
    #   curl_opts:
    #       CURLOPT_*: custom_value
    
    

Usage

Clear all Accelerator cache (opcode+user):

$ php app/console cache:accelerator:clear

Clear only opcode cache:

$ php app/console cache:accelerator:clear --opcode

Clear only user cache:

$ php app/console cache:accelerator:clear --user

Clear the CLI cache (opcode+user):

$ php app/console cache:accelerator:clear --cli

Composer usage
+==============

To automatically clear accelerator cache after each composer install / composer update, you can add a script handler to your project's composer.json :

        "post-install-cmd": [
            "SmartCore\\Bundle\\AcceleratorCacheBundle\\Composer\\ScriptHandler::clearCache"
        ],
        "post-update-cmd": [
            "SmartCore\\Bundle\\AcceleratorCacheBundle\\Composer\\ScriptHandler::clearCache"
        ]

+You can specify command arguments in the extra section:

  • --opcode (to clean only opcode cache):
        "extra": {
          "accelerator-cache-opcode": "yes"
        }
  • --user (to clean only user cache):
        "extra": {
          "accelerator-cache-user": "yes"
        }
  • --cli (to only clear cache via the CLI):
        "extra": {
          "accelerator-cache-cli": "yes"
        }
  • --auth (HTTP authentification):
        "extra": {
          "accelerator-cache-auth": "username:password"
        }

Capifony usage

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

namespace :symfony do
  desc "Clear accelerator cache"
  task :clear_accelerator_cache do
    capifony_pretty_print "--> Clear accelerator cache"
    run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} cache:accelerator:clear #{console_options}'"
    capifony_puts_ok
  end
end

and add these hooks

# clear accelerator cache
after "deploy", "symfony:clear_accelerator_cache"
after "deploy:rollback:cleanup", "symfony:clear_accelerator_cache"

Nginx configuration

If you are using nginx and limiting PHP scripts that you are passing to fpm you need to allow 'apc' 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 accelerator cache.

Example configuration:
```

Your virtual host

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

Copyright (c) 2012 Thibault Duplessis

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.
accelerator_cache:
host: false
web_dir: ~ # Required
mode: fopen
curl_opts:

# Prototype
name: []
  • Merge pull request #1 from giosh94mhz/allow_custom_curl_options
    By Smart-Core, 2 years ago
  • Allow custom CURL option in configuration
    By giosh94mhz, 2 years ago
  • rename README.md
    By Smart-Core, 2 years ago
  • typo fix
    By Smart-Core, 2 years ago
  • fix
    By Smart-Core, 2 years ago
  • fix
    By Smart-Core, 2 years ago
  • Refactor and stuff
    By Smart-Core, 2 years ago
  • Smoothly processing unsuccesful status
    By Smart-Core, 2 years ago
  • Merge pull request #40 from Nyholm/patch-1
    By shieldo, 2 years ago
  • Using latest_release instead of current_path
    By Nyholm, 2 years ago
  • Merge pull request #37 from giosh94mhz/fix_regression_configuration_interface
    By shieldo, 2 years ago
  • Fix regression introduced by PR #36 (ConfigurationInterface)
    By giosh94mhz, 2 years ago
  • Merge pull request #36 from giosh94mhz/configuration_interface
    By shieldo, 2 years ago
  • Changed configuration to implement ConfigurationInterface
    By giosh94mhz, 2 years ago
  • Merge pull request #33 from tjoelsson/master
    By shieldo, 3 years ago
  • Add support for HTTP basic auth
    By tjoelsson, 3 years ago
  • add `cap deploy:rollback` hook
    By shieldo, 3 years ago
  • Merge pull request #29 from Nyholm/master
    By shieldo, 3 years ago
  • [Bugfix] Hide warnings
    By Nyholm, 3 years ago
  • Merge branch 'master' of https://github.com/digisec/ApcBundle
    By shieldo, 3 years ago
  • Merge pull request #28 from garak/patch-1
    By shieldo, 3 years ago
  • do not enforce dev-master in composer.json
    By garak, 3 years ago
  • update cli section of console command to compatibility with php 5.5
    By shieldo, 4 years ago
  • Merge pull request #26 from evillemez/no-web
    By shieldo, 4 years ago
  • Merge pull request #27 from eymengunay/apcu-support
    By shieldo, 4 years ago
  • [Minor] Typo fixed
    By eymengunay, 4 years ago
  • APCu and OPcache support added
    By eymengunay, 4 years ago
  • added --cli option to apc:clear command
    By evillemez, 4 years ago
  • corrected method call
    By shieldo, 4 years ago
  • Merge pull request #22 from eymengunay/router-request-context
    By shieldo, 4 years ago