Session Cache in Laravel 12.29
- Introducing Session Cache in Laravel 12.29
- What is Session Cache?
- When Should You Use It?
- How to Use Session Cache
Introducing Session Cache in Laravel 12.29
Laravel 12.29 introduces a handy new feature called Session Cache, making it easier than ever to cache user-specific data that only needs to live as long as the user’s session.
This feature was added by João Lopes in PR #56887.
What is Session Cache?
The Session Cache provides a per-session scoped cache for your application. Unlike the global application cache, the data stored in session cache is:
- Isolated to each individual session
- Automatically cleaned up when the session expires or is destroyed
- Able to use all familiar Laravel cache methods, such as get, put, remember, and forget
This makes it a perfect fit for storing ephemeral data that only matters during the lifetime of a session.
When Should You Use It?
Session Cache is ideal for temporary, user-specific data that doesn’t need to live permanently. Some examples include:
- Form data saved temporarily during a multi-step process.
- Short-lived API responses tied to one user.
- Calculations that only matter for the session.
- Any ephemeral data tied to a user session.
How to Use Session Cache
Accessing the session cache is simple using the cache method on the session instance.
// Retrieve a cached value from the session$discount = $request->session()->cache()->get('discount'); // Store a value in the session cache with a 5-minute expiration$request->session()->cache()->put( 'discount', 10, now()->addMinutes(5));
The API will feel familiar to anyone who’s used Laravel’s Cache helper or facade before, with the key difference being the scope.
👉 Check out the full implementation in the pull request.
Stay Updated.
I'll you email you as soon as new, fresh content is published.