phpfastcache/phpfastcache-devtools
Internal development tools used by Phpfastcache and its extensions. Provides utilities to support building, testing, and maintaining Phpfastcache-related packages and workflows.
Installation:
composer require phpfastcache/phpfastcache-devtools --dev
Add to composer.json under require-dev to avoid production bloat.
Basic Usage:
use PHPSocialNetwork\Phpfastcache\DevTools\CacheAnalyzer;
$analyzer = new CacheAnalyzer();
$stats = $analyzer->analyzeCache('phpFastCache'); // Default driver
dump($stats);
First Use Case:
$analyzer->enableMonitoring();
// ... trigger cache operations ...
$analyzer->getMonitoringData();
$size = $analyzer->getCacheSize('phpFastCache');
CacheAnalyzer class: Core for stats and monitoring.CacheMonitor class: Real-time hit/miss tracking.CacheCleaner class: Tools for cache maintenance.src/ for usage.Pre-Deployment Cache Audit:
$analyzer = new CacheAnalyzer();
$report = $analyzer->generateReport(['phpFastCache']);
// Log or email $report before deploy.
Performance Profiling:
$monitor = new CacheMonitor();
$monitor->start();
// ... critical cache operations ...
$stats = $monitor->stop();
if ($stats['miss_rate'] > 0.2) {
// Trigger cache warming or optimization.
}
Automated Cache Cleanup:
$cleaner = new CacheCleaner('phpFastCache');
$cleaner->clearExpired(); // Remove stale entries.
$cleaner->trimToSize(100); // Enforce size limit.
Laravel Service Provider: Bind the analyzer to the container for global access:
$this->app->singleton(CacheAnalyzer::class, function ($app) {
return new CacheAnalyzer();
});
Then inject CacheAnalyzer into controllers/services.
Artisan Commands: Create a custom command for CLI access:
use PHPSocialNetwork\Phpfastcache\DevTools\CacheAnalyzer;
class CacheReportCommand extends Command {
protected function handle() {
$analyzer = new CacheAnalyzer();
$this->info($analyzer->generateReport());
}
}
Middleware for Monitoring: Wrap cache-heavy routes to log performance:
$monitor = new CacheMonitor();
$monitor->start();
// Route logic...
$monitor->stop();
$this->logMonitoringData($monitor);
Event Listeners:
Trigger cache cleanup on cache:clear events or cron jobs:
event(new CacheCleanupEvent());
// Listener:
public function handle(CacheCleanupEvent $event) {
(new CacheCleaner('phpFastCache'))->clearExpired();
}
Driver-Specific Quirks:
files, apcu) support all methods. Test with your target driver.getCacheSize() may return null for apcu (use apcu_cache_info() instead).Monitoring Overhead:
CacheMonitor) adds microsecond latency. Disable in production if unused:
$monitor->enable(); // Default: true
$monitor->disable();
Race Conditions:
CacheMonitor sparingly in high-traffic areas.Dev-Only Methods:
generateReport() are heavy; avoid in production. Use only in staging/dev.Verify Driver Configuration:
Ensure phpFastCache is properly configured in config/cache.php before using dev tools.
'default' => env('CACHE_DRIVER', 'phpFastCache'),
'stores' => [
'phpFastCache' => [
'driver' => 'phpFastCache',
'items' => [
'path' => storage_path('framework/cache'),
],
],
],
Check for Deprecated Methods: The package is new; some methods may change. Check the GitHub issues for updates.
Log Raw Data: For complex debugging, log raw stats:
file_put_contents(
storage_path('logs/cache_debug.log'),
print_r($analyzer->analyzeCache(), true)
);
Custom Metrics:
Extend CacheAnalyzer to add domain-specific metrics:
class ExtendedAnalyzer extends CacheAnalyzer {
public function getCustomMetric() {
return $this->getMissRate() * $this->getAvgItemSize();
}
}
Driver-Specific Adapters: Create adapters for unsupported drivers (e.g., Redis):
class RedisCacheAnalyzer extends CacheAnalyzer {
public function getCacheSize() {
return redis()->dbsize();
}
}
Event-Driven Cleanup: Subscribe to Laravel events to auto-clean cache:
event(new CacheCleanupEvent());
// Listener:
public function handle() {
(new CacheCleaner('phpFastCache'))->trimToSize(50);
}
Visualization: Integrate with tools like Grafana by exposing stats via an API:
Route::get('/cache/stats', function () {
return response()->json((new CacheAnalyzer())->analyzeCache());
});
How can I help you explore Laravel packages today?