Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Opcache Laravel Package

appstract/laravel-opcache

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require appstract/laravel-opcache
    

    Publish the config (optional, for customization):

    php artisan vendor:publish --provider="Appstract\Opcache\OpcacheServiceProvider" --tag="config"
    
  2. First Use Case: Run the opcache:status command to verify OPcache is active and check current stats:

    php artisan opcache:status
    

    This provides immediate visibility into OPcache’s memory usage, hit/miss ratios, and script coverage.

Where to Look First

  • Artisan Commands: The package adds three core commands (opcache:clear, opcache:config, opcache:status) under the php artisan opcache: namespace.
  • Config File: Located at config/opcache.php (if published). Key settings include:
    • url: Override the default route URL (e.g., for load balancers).
    • php.ini_path: Specify a custom php.ini path if OPcache is configured non-standardly.

Implementation Patterns

Usage Patterns

  1. Post-Deployment Workflow:

    • After deploying code changes, run:
      php artisan opcache:clear
      
      This ensures stale OPcache entries are purged, preventing cached old code from executing.
    • Pro Tip: Add this to your deployment script (e.g., deploy.php or Makefile) as a post-hook.
  2. Monitoring OPcache Performance:

    • Schedule opcache:status in a cron job or CI pipeline to track:
      • memory_consumption: Detect memory bloat.
      • hit_rate: Identify underperforming scripts (aim for >90%).
    • Example cron entry (run daily):
      0 3 * * * cd /path/to/project && php artisan opcache:status >> /var/log/opcache_metrics.log
      
  3. Debugging Stale Caches:

    • Use opcache:config to verify settings like opcache.revalidate_freq (default: 2 seconds) align with your deployment frequency.
    • For long-running processes (e.g., queues), set opcache.max_accelerated_files higher to avoid cache eviction.

Integration Tips

  • Laravel Forge/Cpanel: Add the opcache:clear command to your post-deploy hooks via the "Run custom commands" field.
  • CI/CD Pipelines: Include php artisan opcache:clear in your test suite’s after_script to simulate a clean environment:
    # .github/workflows/test.yml
    jobs:
      test:
        runs-on: ubuntu-latest
        steps:
          - run: php artisan opcache:clear
    
  • Custom Routes: If using OPCACHE_URL, ensure your load balancer forwards requests to the correct backend (e.g., OPCACHE_URL=/opcache).

Gotchas and Tips

Pitfalls

  1. False Positives in opcache:status:

    • The script_name field may show unknown if OPcache is compiled as a ZTS (Thread-Safe) module. Ignore this unless scripts are missing entirely.
    • Fix: Rebuild PHP with --enable-opcache and --disable-zts if needed.
  2. Permission Issues:

    • opcache:clear may fail if PHP-FPM’s user (e.g., www-data) lacks write access to OPcache’s storage.
    • Fix: Set opcache.file_cache to a writable directory (e.g., /dev/shm/opcache) in php.ini or adjust permissions:
      sudo chown -R www-data:www-data /path/to/opcache/storage
      
  3. Laravel Queue Workers:

    • Workers may continue using stale OPcache entries after opcache:clear. Restart workers post-clear:
      php artisan queue:restart
      

Debugging

  • Check OPcache Logs: Enable opcache.log_verbosity_level in php.ini (e.g., 1 for errors) and monitor /var/log/php_opcache.log.
  • Validate Config: Run php artisan opcache:config and cross-reference with php -i | grep opcache to spot discrepancies.

Extension Points

  1. Custom Commands: Extend the package by creating a new Artisan command that uses the underlying OpcacheManager service:

    use Appstract\Opcache\OpcacheManager;
    
    class CustomOpcacheCommand extends Command {
        protected $opcache;
    
        public function __construct(OpcacheManager $opcache) {
            $this->opcache = $opcache;
            parent::__construct();
        }
    
        public function handle() {
            $stats = $this->opcache->getStatus();
            // Custom logic (e.g., alert if hit_rate < 80%)
        }
    }
    

    Register it in AppServiceProvider@boot():

    $this->commands([CustomOpcacheCommand::class]);
    
  2. Event Listeners: Trigger OPcache clearing on specific events (e.g., Deployed):

    use Appstract\Opcache\Facades\Opcache;
    
    event(new Deployed($release));
    Opcache::clear();
    
  3. Middleware: Add OPcache headers to responses for debugging:

    public function handle($request, Closure $next) {
        $response = $next($request);
        $response->header('X-Opcache-Status', Opcache::getStatus()['status']);
        return $response;
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware