laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.62k stars 11.03k forks source link

Optimize Route Caching with Serialized Routes in Laravel #53578

Closed jbidad closed 2 days ago

jbidad commented 2 days ago

This PR enhances Laravel's route caching by transitioning from a stub-based mechanism to serialized data. This change addresses issues encountered during deployments and high-concurrency environments, improving performance and reliability.

Summary of Changes:

  1. Route Caching Optimization:

    • Updated buildRouteCacheFile to serialize route data instead of generating a PHP stub.
    • Ensured cached route files are now serialized, simplifying the caching process.
    protected function buildRouteCacheFile(RouteCollection $routes)
    {
       return serialize($routes->compile());
    }
  2. Adjustments in RouteServiceProvider:

    • Added getCachedRoutes to safely read and unserialize cached route files.
    • Modified loadCachedRoutes to load routes directly from the deserialized cache using Router::setCompiledRoutes.
  3. Tests Adjustments in RouteCachingTest.php:

    • Modified test cases in RouteCachingTest.php to adapt to the new serialized route caching format.
    • Updated the defineCacheRoutes method used in Orchestra\Testbench\TestCase to work with the serialized route cache instead of relying on PHP stubs.
    • Ensured that the new serialization logic integrates seamlessly with Orchestra\Testbench.

    For instance, the defineCacheRoutes method now verifies that routes can be serialized, cached, and correctly restored without relying on stub-based logic.

  4. Backward Compatibility with Existing Tests:

    • All changes maintain compatibility with existing tests, ensuring smooth migration to the new caching mechanism.
    • Existing tests for route:cache and route:clear commands were enhanced to confirm correct behavior under various scenarios.

Why This Change is Needed:
The previous stub-based caching system was prone to errors during high-load deployment scenarios, such as CI/CD pipelines, due to file generation and inclusion inconsistencies. Serialized route caching addresses this by:

Steps to Test:

  1. Run php artisan route:cache to generate cached routes.
  2. Verify the generated file now contains serialized route data.
  3. Ensure php artisan route:clear removes the cached file as expected.
  4. Validate the routes are loaded correctly in various test cases using Orchestra\Testbench\TestCase.

Backward Compatibility:

Impact:

Additional Context:
This update simplifies route caching logic and integrates well with Orchestra\Testbench workflows, ensuring Laravel applications benefit from a robust and reliable caching mechanism even under high load.

taylorotwell commented 2 days ago

I am closing this pull request because it lacks sufficient explanation, tests, or both. It is difficult for us to merge pull requests without these things because the change may introduce breaking changes to the framework.

Feel free to re-submit your change with a thorough explanation of the feature and tests - integration tests are preferred over unit tests. Please include it's benefit to end users; the reasons it does not break any existing features; how it makes building web applications easier, etc.

Thanks!

jbidad commented 2 days ago

I believe the explanation provided is sufficient. However, as mentioned earlier, this PR has not been validated by the existing tests since they rely on external packages. Additionally, this is related to issue number #53577

Let me know if you need further refinements! @taylorotwell