Caching
Data Helpers uses an intelligent persistent cache system to dramatically improve SimpleDto performance. This guide explains the different cache invalidation strategies and their performance characteristics.
Quick Start
Section titled “Quick Start”use event4u\DataHelpers\Enums\CacheInvalidation;
return [    'cache' => [        'invalidation' => CacheInvalidation::MANUAL, // Recommended for production    ],];# Warm cache in production deploymentphp bin/warm-cache.php src/Dtos
# Clear cache when neededphp bin/clear-cache.phpCache Invalidation Strategies
Section titled “Cache Invalidation Strategies”Data Helpers supports four cache invalidation strategies, each with different trade-offs between performance and convenience:
MANUAL (Recommended for Production)
Section titled “MANUAL (Recommended for Production)”No automatic validation - cache is only invalidated by running the clear-cache command.
use event4u\DataHelpers\Enums\CacheInvalidation;use event4u\DataHelpers\Helpers\ConfigHelper;
ConfigHelper::getInstance()->set('cache.invalidation', CacheInvalidation::MANUAL);Characteristics:
- ✅ Best performance - no validation overhead at runtime
- ✅ Like Spatie Laravel Data - cache is trusted completely
- ✅ Production-ready - use with cache warming in deployment pipeline
- ⚠️ Requires manual cache clearing after code changes
Use Cases:
- Production environments with deployment pipelines
- High-throughput applications
- When you control deployments and can warm cache
MTIME (Recommended for Development)
Section titled “MTIME (Recommended for Development)”Automatic validation using file modification time (filemtime()).
use event4u\DataHelpers\Enums\CacheInvalidation;use event4u\DataHelpers\Helpers\ConfigHelper;
ConfigHelper::getInstance()->set('cache.invalidation', CacheInvalidation::MTIME);Characteristics:
- ✅ Automatic cache invalidation when files change
- ✅ Fast validation - only checks file modification time
- ✅ Good for development - no manual cache clearing needed
- ⚠️ ~13% slower than MANUAL mode
Use Cases:
- Development environments
- When you frequently modify DTO classes
- When you want automatic cache invalidation
HASH (Maximum Accuracy)
Section titled “HASH (Maximum Accuracy)”Automatic validation using file content hash (hash_file()).
use event4u\DataHelpers\Enums\CacheInvalidation;use event4u\DataHelpers\Helpers\ConfigHelper;
ConfigHelper::getInstance()->set('cache.invalidation', CacheInvalidation::HASH);Characteristics:
- ✅ Most accurate - detects content changes even if mtime is unchanged
- ✅ Automatic cache invalidation when file content changes
- ⚠️ ~18% slower than MANUAL mode
- ⚠️ Slower than MTIME - reads entire file to compute hash
Use Cases:
- When you use git checkout(mtime may not change)
- When you need maximum accuracy
- When performance is less critical than correctness
BOTH (Maximum Safety)
Section titled “BOTH (Maximum Safety)”Automatic validation using both filemtime() and hash_file().
use event4u\DataHelpers\Enums\CacheInvalidation;use event4u\DataHelpers\Helpers\ConfigHelper;
ConfigHelper::getInstance()->set('cache.invalidation', CacheInvalidation::BOTH);Characteristics:
- ✅ Maximum safety - validates both mtime and content hash
- ⚠️ Slowest option - combines overhead of both methods
- ⚠️ Rarely needed - HASH alone is usually sufficient
Use Cases:
- When you need absolute certainty
- Debugging cache issues
- Rarely needed in practice
Performance Benchmarks
Section titled “Performance Benchmarks”The following benchmarks show the performance impact of each cache invalidation strategy:
Cache Invalidation Modes (50,000 iterations, warm cache):- MANUAL (no validation):     2.44 μs- MTIME (auto-validation):    2.41 μs- HASH (auto-validation):     2.35 μsConfiguration
Section titled “Configuration”Environment Variables
Section titled “Environment Variables”You can configure cache invalidation via environment variables:
DATA_HELPERS_CACHE_INVALIDATION=manual  # or mtime, hash, bothProgrammatic Configuration
Section titled “Programmatic Configuration”use event4u\DataHelpers\Helpers\ConfigHelper;use event4u\DataHelpers\Enums\CacheInvalidation;
$config = ConfigHelper::getInstance();$config->set('cache.invalidation', CacheInvalidation::MANUAL);Cache Warming
Section titled “Cache Warming”For production deployments, warm the cache during deployment:
# In your deployment scriptphp bin/warm-cache.php src/DtosThis pre-generates cache for all DTOs, ensuring zero cold-start penalty.
How It Works
Section titled “How It Works”MANUAL Mode (Spatie-Style)
Section titled “MANUAL Mode (Spatie-Style)”- First request: Reflection generates metadata → stored in persistent cache
- Subsequent requests: Metadata loaded from cache without validation
- Cache invalidation: Only via bin/clear-cache.phpcommand
MTIME/HASH/BOTH Modes
Section titled “MTIME/HASH/BOTH Modes”- First request: Reflection generates metadata → stored in persistent cache
- Subsequent requests:
- Load metadata from cache
- Validate using filemtime()and/orhash_file()
- If invalid: Regenerate metadata via reflection
- If valid: Use cached metadata
 
Best Practices
Section titled “Best Practices”Production
Section titled “Production”return [    'cache' => [        'driver' => CacheDriver::AUTO,        // Auto-detect Laravel/Symfony        'invalidation' => CacheInvalidation::MANUAL, // Best performance    ],];# In deployment pipelinephp bin/clear-cache.phpphp bin/warm-cache.php src/DtosDevelopment
Section titled “Development”return [    'cache' => [        'driver' => CacheDriver::AUTO,        'invalidation' => CacheInvalidation::MTIME, // Auto-invalidation    ],];No manual cache clearing needed - cache updates automatically when files change.
Troubleshooting
Section titled “Troubleshooting”Cache Not Updating
Section titled “Cache Not Updating”If you’re using MANUAL mode and cache isn’t updating after code changes:
# Clear cache manuallyphp bin/clear-cache.php
# Or switch to MTIME for development# config/data-helpers.php'invalidation' => CacheInvalidation::MTIME,Performance Issues
Section titled “Performance Issues”If you’re experiencing slow performance:
- 
Check your cache invalidation mode: Terminal window # Should be MANUAL in productionecho $DATA_HELPERS_CACHE_INVALIDATION
- 
Warm the cache: Terminal window php bin/warm-cache.php src/Dtos
- 
Verify cache is working: Terminal window # Should show cached filesls -la storage/cache/data-helpers/
See Also
Section titled “See Also”- Cache Warming Guide - Detailed cache warming documentation
- Cache Generation Guide - Manual cache generation instructions
- Performance Benchmarks - Complete performance comparison
- Configuration - All configuration options
