# Performance Optimization
Best practices and techniques for optimizing Core PHP Framework applications.
## Database Optimization
### Eager Loading
Prevent N+1 queries with eager loading:
```php
// ❌ Bad - N+1 queries
$posts = Post::all();
foreach ($posts as $post) {
echo $post->author->name; // Query per post
echo $post->category->name; // Another query per post
}
// ✅ Good - 3 queries total
$posts = Post::with(['author', 'category'])->get();
foreach ($posts as $post) {
echo $post->author->name;
echo $post->category->name;
}
```
### Query Optimization
```php
// ❌ Bad - fetches all columns
$posts = Post::all();
// ✅ Good - only needed columns
$posts = Post::select(['id', 'title', 'created_at'])->get();
// ✅ Good - count instead of loading all
$count = Post::count();
// ❌ Bad
$count = Post::all()->count();
// ✅ Good - exists check
$exists = Post::where('status', 'published')->exists();
// ❌ Bad
$exists = Post::where('status', 'published')->count() > 0;
```
### Chunking Large Datasets
```php
// ❌ Bad - loads everything into memory
$posts = Post::all();
foreach ($posts as $post) {
$this->process($post);
}
// ✅ Good - process in chunks
Post::chunk(1000, function ($posts) {
foreach ($posts as $post) {
$this->process($post);
}
});
// ✅ Better - lazy collection
Post::lazy()->each(function ($post) {
$this->process($post);
});
```
### Database Indexes
```php
// Migration
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('slug')->unique(); // Index for lookups
$table->string('status')->index(); // Index for filtering
$table->foreignId('workspace_id')->constrained(); // Foreign key index
// Composite index for common query
$table->index(['workspace_id', 'status', 'created_at']);
});
```
## Caching Strategies
### Model Caching
```php
use Illuminate\Support\Facades\Cache;
class Post extends Model
{
public static function findCached(int $id): ?self
{
return Cache::remember(
"posts.{$id}",
now()->addHour(),
fn () => self::find($id)
);
}
protected static function booted(): void
{
// Invalidate cache on update
static::updated(fn ($post) => Cache::forget("posts.{$post->id}"));
static::deleted(fn ($post) => Cache::forget("posts.{$post->id}"));
}
}
```
### Query Result Caching
```php
// ❌ Bad - no caching
public function getPopularPosts()
{
return Post::where('views', '>', 1000)
->orderByDesc('views')
->limit(10)
->get();
}
// ✅ Good - cached for 1 hour
public function getPopularPosts()
{
return Cache::remember('posts.popular', 3600, function () {
return Post::where('views', '>', 1000)
->orderByDesc('views')
->limit(10)
->get();
});
}
```
### Cache Tags
```php
// Tag cache for easy invalidation
Cache::tags(['posts', 'popular'])->put('popular-posts', $posts, 3600);
// Clear all posts cache
Cache::tags('posts')->flush();
```
### Redis Caching
```php
// config/cache.php
'default' => env('CACHE_DRIVER', 'redis'),
'stores' => [
'redis' => [
'driver' => 'redis',
'connection' => 'cache',
'lock_connection' => 'default',
],
],
```
## Asset Optimization
### CDN Integration
```php
// Use CDN helper
// With transformations
```
### Image Optimization
```php
use Core\Media\Image\ImageOptimizer;
$optimizer = app(ImageOptimizer::class);
// Automatic optimization
$optimizer->optimize($imagePath, [
'quality' => 85,
'max_width' => 1920,
'strip_exif' => true,
'convert_to_webp' => true,
]);
```
### Lazy Loading
```blade
{{-- Lazy load images --}}
{{-- Lazy load thumbnails --}}
```
## Code Optimization
### Lazy Loading Modules
Modules only load when their events fire:
```php
// Module Boot.php
public static array $listens = [
WebRoutesRegistering::class => 'onWebRoutes',
];
// Only loads when WebRoutesRegistering fires
// Saves memory and boot time
```
### Deferred Service Providers
```php
app->singleton(AnalyticsService::class);
}
public function provides(): array
{
return [AnalyticsService::class];
}
}
```
### Configuration Caching
```bash
# Cache configuration
php artisan config:cache
# Clear config cache
php artisan config:clear
```
### Route Caching
```bash
# Cache routes
php artisan route:cache
# Clear route cache
php artisan route:clear
```
## Queue Optimization
### Queue Heavy Operations
```php
// ❌ Bad - slow request
public function store(Request $request)
{
$post = Post::create($request->validated());
// Slow operations in request cycle
$this->generateThumbnails($post);
$this->generateOgImage($post);
$this->notifySubscribers($post);
return redirect()->route('posts.show', $post);
}
// ✅ Good - queued
public function store(Request $request)
{
$post = Post::create($request->validated());
// Queue heavy operations
GenerateThumbnails::dispatch($post);
GenerateOgImage::dispatch($post);
NotifySubscribers::dispatch($post);
return redirect()->route('posts.show', $post);
}
```
### Job Batching
```php
use Illuminate\Bus\Batch;
use Illuminate\Support\Facades\Bus;
Bus::batch([
new ProcessPost($post1),
new ProcessPost($post2),
new ProcessPost($post3),
])->then(function (Batch $batch) {
// All jobs completed successfully
})->catch(function (Batch $batch, Throwable $e) {
// First batch job failure
})->finally(function (Batch $batch) {
// Batch finished
})->dispatch();
```
## Livewire Optimization
### Lazy Loading Components
```blade
{{-- Load component when visible --}}