# Performance Optimization - Quick Start Guide

## What Changed?

Your app now uses **Stale-While-Revalidate** caching pattern for instant page loads.

## How It Works

### First Visit (Cache Miss)
```
User → Request Page → Fetch APIs (2s) → Show Data → Cache It
```
⏱️ **Time: ~2 seconds** (same as before)

### Subsequent Visits (Cache Hit)
```
User → Request Page → Return Cached Data → Show Instantly
                    ↘ Background: Check if cache expired
                                ↘ If expired: Refresh in background
```
⏱️ **Time: ~50ms** (97.5% faster!)

### Cache Expired
```
User → Request Page → Return Stale Cache → Show Instantly
                    ↘ Background: Fetch Fresh APIs → Update Cache
Next User → Fresh Data (still instant!)
```
⏱️ **Time: ~50ms** (user never waits!)

## Testing

### 1. First Load (Populates Cache)
Visit your homepage:
```
https://your-app.test
```
Should take ~2 seconds (normal)

### 2. Second Load (From Cache)
Refresh the page:
```
https://your-app.test
```
Should be **INSTANT** (~50ms)

### 3. Force Refresh (Bypass Cache)
Add `?refresh=1` to URL:
```
https://your-app.test/?refresh=1
https://your-app.test/popular?refresh=1
https://your-app.test/latest?refresh=1
```
Will fetch fresh data and update cache

## Pages Optimized

✅ **HomePage** - Latest + Popular dramas  
✅ **PopularPage** - Popular dramas with pagination  
✅ **LatestPage** - Latest dramas with pagination  
✅ **RelatedDramas** - Related dramas search  

## Cache TTLs

- **Latest dramas:** 24 hours
- **Popular dramas:** 6 hours  
- **Search/Related:** 7 days

## Monitoring

Check logs for cache activity:
```bash
php artisan pail --filter="ConditionalCache"
```

Look for:
- ✅ `ConditionalCache background refresh successful`
- ⚠️ `ConditionalCache background refresh failed`
- ℹ️ `ConditionalCache fresh data invalid, serving cached value`

## Troubleshooting

### Clear All Caches
```bash
php artisan cache:clear
php artisan config:clear
```

### Disable Caching (for debugging)
Edit `config/drama.php`:
```php
'cache' => [
    'enabled' => false, // Set to false
    // ...
],
```

### Revert to Old Method
If issues occur, change in component files:
```php
// From this:
Cache::staleWhileRevalidate($key, $fetch, $ttl, [], $forceRefresh)

// To this:
DataTransformationService::cacheComposite($key, $ttl, $fetch, [], $forceRefresh)
```

## Expected Results

### Performance Metrics
- **First load:** Same as before (~2s)
- **Cached loads:** 97.5% faster (~50ms)
- **Cache refresh:** Invisible to users (background)

### User Experience
- ✨ Pages load instantly
- ✨ No waiting for API calls
- ✨ Fresh content updated automatically
- ✨ Graceful handling of API failures

## Next Steps

### Enable True Parallelism (Future Enhancement)

**Option 1: Install ReactPHP**
```bash
composer require react/promise
```

Then update `DataTransformationService::executeParallel()` to use promises.

**Option 2: Use Guzzle Concurrent Requests**
```php
use Illuminate\Support\Facades\Http;

$responses = Http::pool(fn ($pool) => [
    $pool->as('melolo')->get('...'),
    $pool->as('dramabox')->post('...'),
    $pool->as('dramadash')->get('...'),
    $pool->as('netshort')->get('...'),
]);
```

This would reduce first-load time from 2s to ~500ms!

## Questions?

See full documentation: `PERFORMANCE_OPTIMIZATION.md`
