modules:cache command generates a cache file that stores module metadata, significantly improving module discovery and loading performance in your Laravel application.
Usage
What It Does
- Clears existing cache - Removes any stale module cache by calling
modules:clear - Scans all modules - Discovers and processes all registered modules
- Writes cache file - Stores module metadata in a cache file for quick retrieval
- Confirms success - Displays a success message
Example Output
The first line appears because
modules:cache automatically calls modules:clear before caching to ensure fresh data.Performance Benefits
Caching modules provides significant performance improvements:- Faster application boot time - Module metadata is loaded from cache instead of filesystem scanning
- Reduced I/O operations - Single cache file read vs. multiple directory traversals
- Optimized for production - Essential for deployment environments
- Improved service provider discovery - Autoload configuration is pre-processed
Before vs After Caching
Without cache (development):- Scans modules directory on every request
- Reads multiple
composer.jsonfiles - Processes autoload configuration dynamically
- Reads single cached file
- Module data is immediately available
- No filesystem scanning required
When to Use
Runmodules:cache in these scenarios:
During Deployment
Include in your deployment script:After Module Changes
Recache when you:- Add a new module
- Remove a module
- Modify module configuration
- Update module
composer.jsonfiles
Production Optimization
Always cache modules in production environments for optimal performance.Cache Location
The cache file is stored in your Laravel application’s bootstrap cache directory:config.php and routes.php.
Cache Invalidation
The cache should be cleared and regenerated when:- Module added or removed - Registry has changed
- Module configuration updated - Namespaces or paths modified
- Autoload changes - PSR-4 configuration in module’s
composer.jsonupdated - Deployment - Ensure production has latest module structure
Development vs Production
Development Environment
In development, you typically don’t cache modules to allow:- Automatic module discovery
- Immediate reflection of changes
- Easier debugging
Production Environment
In production, always cache modules as part of deployment:Integration with Other Cache Commands
Clear All Caches
When clearing application caches, include module cache:Cache Everything
When caching for production, include module cache:Automated Deployment
Laravel Forge
Add to your deployment script:Laravel Vapor
Include invapor.yml:
GitHub Actions
Troubleshooting
Cache not improving performance?
-
Verify cache file exists:
-
Check file permissions:
-
Ensure directory is writable:
Cache contains stale data?
Clear and regenerate:Related Commands
- modules:clear - Remove the module cache
- modules:list - View all cached modules
- modules:sync - Sync configuration after module changes