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

Cache Process Bundle Laravel Package

cleverage/cache-process-bundle

View on GitHub
Deep Wiki
Context7
## Getting Started
This package, **Cache Process Bundle**, simplifies process management and caching in Laravel/Symfony applications. To get started:
1. **Installation**: Require via Composer:
   ```bash
   composer require cleverage/cache-process-bundle
  1. Configuration: Publish the config file (if needed) and bind the service in your Laravel/Symfony container.
  2. First Use Case: Use the bundle to cache long-running processes (e.g., background jobs, CLI commands) to avoid redundant execution. Example:
    use Cleverage\CacheProcessBundle\CacheProcess;
    
    $cacheProcess = new CacheProcess('my_process_key', 3600); // Cache for 1 hour
    $result = $cacheProcess->run(function() {
        return Process::execute('php artisan my:long-running-task');
    });
    

Implementation Patterns

Process Caching

Leverage the bundle to cache the output of expensive processes:

$cacheProcess = new CacheProcess('report_generation', 86400); // Daily cache
$report = $cacheProcess->run(function() {
    return ReportGenerator::generate();
});

Integration with Laravel Queues

Useful for caching results of queued jobs to avoid reprocessing:

public function handle()
{
    $cacheProcess = new CacheProcess('job_' . $this->jobId, 3600);
    $result = $cacheProcess->run(function() {
        return $this->executeJobLogic();
    });
}

Symfony CLI Integration

For Symfony applications, integrate with console commands:

use Symfony\Component\Console\Command\Command;
use Cleverage\CacheProcessBundle\CacheProcess;

class MyCommand extends Command
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $cacheProcess = new CacheProcess('command_result', 3600);
        $result = $cacheProcess->run(function() {
            return $this->runCommandLogic();
        });
    }
}

Tag-Based Invalidation

Use tags to invalidate caches programmatically:

$cacheProcess = new CacheProcess('data_fetch', 3600, ['user_data']);
$cacheProcess->invalidateTags(['user_data']); // Invalidate all tagged caches

Gotchas and Tips

PHP/Symfony Version Compatibility

  • New: Supports PHP 8.5 and Symfony 8 (v2.0).
  • Breaking: Drops support for PHP 8.1 and Symfony 7.3. Ensure your environment aligns with these requirements.
    • Fix: Update your php and symfony/* packages in composer.json:
      "require": {
          "php": "^8.2 || ^8.3 || ^8.4 || ^8.5",
          "symfony/*": "^7.4 || ^8.0"
      }
      

Cache Key Collisions

  • Avoid generic keys (e.g., 'process'). Use unique identifiers (e.g., 'user_123_report').
  • Tip: Combine static strings with dynamic data (e.g., user IDs, timestamps).

Debugging Cache Issues

  • Check if the cache driver is properly configured in .env (e.g., CACHE_DRIVER=redis).
  • Use CacheProcess::isCached() to verify if a process is being served from cache:
    if ($cacheProcess->isCached()) {
        logger("Serving cached result");
    }
    

Performance Considerations

  • TTL (Time-to-Live): Set appropriate TTL values (e.g., 3600 for hourly caches). Avoid overly long TTLs for frequently changing data.
  • Tip: Use shorter TTLs for development and longer for production.

Extension Points

  • Custom Cache Stores: Extend the bundle by implementing a custom cache store (e.g., for distributed caching).
  • Event Listeners: Hook into cache hits/misses via Symfony events (e.g., CacheProcessEvent).

Testing

  • Mock the CacheProcess class in unit tests to simulate cached/missed scenarios:
    $mockCacheProcess = $this->createMock(CacheProcess::class);
    $mockCacheProcess->method('run')->willReturn('cached_result');
    

NO_UPDATE_NEEDED would **not** apply here due to the breaking changes and new features.
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