Performance Optimization
General performance optimization strategies for the CPR backend.
Production Optimization Commands
# Cache configuration
php artisan config:cache
# Cache routes
php artisan route:cache
# Cache views
php artisan view:cache
# Optimize autoloader
composer install --optimize-autoloader --no-dev
# Run all optimizations
php artisan optimizeWARNING
Always run php artisan config:clear during development. Cached config prevents .env changes from taking effect.
API Response Optimization
Use API Resources
API Resources transform models into JSON responses, letting you control exactly what data is sent:
class PatientResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'pin' => $this->pin,
// Only include what the client needs
'patient' => PatientResource::make($this->whenLoaded('patient')),
];
}
}Pagination
All list endpoints return paginated results. The default page size is 15, configurable via perPage query parameter.
Conditional Relationship Loading
Use whenLoaded() in Resources to avoid serializing unloaded relationships:
'items' => TransactionItemResource::collection($this->whenLoaded('items')),Database Optimization
See the dedicated Query Optimization page for:
- Eager loading to prevent N+1 queries
- Proper indexing
- Using
withCount()instead of loading relations - Database transactions for atomicity
Queue Processing
Use queued jobs for time-consuming operations:
# Development
QUEUE_CONNECTION=sync
# Production
QUEUE_CONNECTION=redisRun the queue worker:
php artisan queue:work --tries=3 --timeout=90The composer dev command automatically starts a queue listener.
Image Handling
The UploadService processes patient photos:
- Generates thumbnails at 52x52 and 100x100 pixels
- Uses Intervention Image for server-side processing
- Stores original and thumbnails in configured filesystem disk
Keep image uploads reasonable in size (max 2MB enforced via validation).
Bcrypt Rounds
# Production - secure but slower
BCRYPT_ROUNDS=12
# Testing - fast hashing for speed
BCRYPT_ROUNDS=4The test configuration (phpunit.xml) uses 4 rounds for faster test execution.