Git Product home page Git Product logo

acceleratorcachebundle's Introduction

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
  1. Add this bundle to your application kernel:
// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new SmartCore\Bundle\AcceleratorCacheBundle\AcceleratorCacheBundle(),
        // ...
    );
}
  1. Configure accelerator_cache service:
# app/config/config.yml
accelerator_cache:
    host: http://example.com
    web_dir: %kernel.project_dir%/web
  1. 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;
    ...

acceleratorcachebundle's People

Contributors

antonbabenko avatar artemdigi avatar bocharsky-bw avatar boshurik avatar cordoval avatar dmecke avatar eymengunay avatar garak avatar giosh94mhz avatar helmer avatar iwankgb avatar jkm9000 avatar kbond avatar nyholm avatar ondrowan avatar ornicar avatar pdias avatar polem avatar shieldo avatar shtumi avatar sivolobov avatar tjoelsson avatar toaotc avatar tomadam avatar zubis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

acceleratorcachebundle's Issues

Symfony 3 structure issue

Since Symfony 3, a new directory structure has been proposed, which puts the console inside the bin\ folder.
This causes the SmartCore\Bundle\AcceleratorCacheBundle\Composer\ScriptHandler::clearCache script to crash with Could not open input file: app/console.

Since you use the DistributionBundle, it should not be an issue.
But this line is faulty: $appDir = $options['symfony-app-dir'];.
You rather need to look for "symfony-bin-dir" option and use it when available, as configured in composer.json:
"extra": {
"symfony-app-dir": "app",
"symfony-bin-dir": "bin"

Exception with empty message

Getting exception with empty message:

$ ./bin/console cache:accelerator:clear -vv

[RuntimeException]

Exception trace:
() at /home/cdn/www/vendor/smart-core/accelerator-cache-bundle/Command/AcceleratorCacheClearCommand.php:46
SmartCore\Bundle\AcceleratorCacheBundle\Command\AcceleratorCacheClearCommand->execute() at /home/cdn/www/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:255
Symfony\Component\Console\Command\Command->run() at /home/cdn/www/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:846
Symfony\Component\Console\Application->doRunCommand() at /home/cdn/www/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:191
Symfony\Component\Console\Application->doRun() at /home/cdn/www/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:80
Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /home/cdn/www/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:122
Symfony\Component\Console\Application->run() at /home/cdn/www/bin/console:29

Using:

  • php7
  • Symfony 3.2

Error cache:accelerator:clear --no-debug

[RuntimeException]                                                           

  Unable to read "https://website.com/apc-0c2b1755d3b4379133e84  

  565110c077d.php", does the host locally resolve?    

file .php does not create itself in public folder

Capifony tasks hang

Hi,

having problem with hanging capifony task. Apache is configured properly (can run the apc clear task via browser). Fails both with curl and fopen. Any ideas?

Add support for environment variables

Given this configuration:

accelerator_cache:
    host: "%env(ACCELERATOR_CACHE_HOST)%"
    web_dir: "%kernel.root_dir%/../web"

and this .env file:

ACCELERATOR_CACHE_HOST=https://www.domain.tld

The command will not work because it will try to fetch the http://https://www.domain.tld/apc-*.php URL. Actual problem lies here:

https://github.com/Smart-Core/AcceleratorCacheBundle/blob/master/DependencyInjection/Configuration.php#L23

The moment a bundle configuration is processed, the $v does not yet contain the environment variable value (i.e. https://www.domain.tld), but rather a variable temporary name (e.g. env_dc3b1098b7dfc6db_ACCELERATOR_CACHE_HOST_93ccaa97cef7fc766f177ccffea2b95b), thus always receiving the http:// prefix.

Compatibility with sensio/distribution-bundle (v5.0.6)

Hi,

Edit : oups didn't see this pull request : #19

I ran into an issue while updating my project with composer.
Updating only sensio/distribution-bundle from v5.0.5 to v5.0.6.

The error :

[ErrorException]
Declaration of SmartCore\Bundle\AcceleratorCacheBundle\Composer\ScriptHandler::clearCache() should be compatible with Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache(Composer\Script\Event $event)

The hotfix I made : Replace "CommandEvent" by "Event" (in the method declaration and in the use part)

Thanks,

Array to string conversion with Symfony 3.4

Upgrading to Symfony 3.4 throws an error when clearing the cache:

bin/console cache:accelerator:clear -vvv

In CacheClearerService.php line 29:

[Symfony\Component\Debug\Exception\ContextErrorException]
Notice: Array to string conversion

Exception trace:
SmartCore\Bundle\AcceleratorCacheBundle\CacheClearerService->__construct() at /var/www/survos/platform/var/cache/dev/ContainerIugi7nx/getAcceleratorCache_ClearerService.php:11
require() at /var/www/survos/platform/var/cache/dev/ContainerIugi7nx/Container.php:2218
ContainerIugi7nx->load() at /var/www/survos/platform/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:299
Symfony\Component\DependencyInjection\Container->get() at /var/www/survos/platform/vendor/smart-core/accelerator-cache-bundle/Command/AcceleratorCacheClearCommand.php:41
SmartCore\Bundle\AcceleratorCacheBundle\Command\AcceleratorCacheClearCommand->execute() at /var/www/survos/platform/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:276
Symfony\Component\Console\Command\Command->run() at /var/www/survos/platform/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:951
Symfony\Component\Console\Application->doRunCommand() at /var/www/survos/platform/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:93
Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /var/www/survos/platform/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:231
Symfony\Component\Console\Application->doRun() at /var/www/survos/platform/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:81
Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /var/www/survos/platform/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:132
Symfony\Component\Console\Application->run() at /var/www/survos/platform/bin/console:27

cache:accelerator:clear [--opcode] [--user] [--cli] [--auth AUTH] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--]

PHP 5.6 and HTTPS

If you use this bundle with this combination don't work because SSL streams have been changed in PHP 5.6. You can read more in http://php.net/manual/en/migration56.openssl.php. In the bottom of this page you have the solution: disable verify peer.

<?php
            $streamContext = stream_context_create([
                'ssl' => [
                    'verify_peer'      => false,
                    'verify_peer_name' => false
                ]
            ]);
            $contents = file_get_contents('https://url', false, $streamContext);
?>

If you like I can create a pull request adding this to CacheClearerService, but not just now because I don't have time

Time for a new release?

Sorry for the issue, I know it's boring when someone asks. Anyway, there are some features/fixes pending and the last relase is from January.

Anything that prevent a new release? May I help in this case?

File is not writable

I think that this commit:

1edc3f5

has broken command script. There's a check if notexisting file is writable, but it's created afterwards and there's an error.

How to set multi hosts ?

Hi

I would like to know if it is possible to set multi-hosts. Indeed, I got two dev servers with two different host names.
It's possible to use AcceleratorCacheBundle in that way ?

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.