[12.x] Add typed getters on Cache #58451
Merged
taylorotwell merged 4 commits intolaravel:12.xfrom Jan 30, 2026
Merged
Conversation
Member
|
I like this feature. I feel like this might suffer from the same fundamental type problem as #56481, where differing driver behaviour could cause problems. Hopefully the below demonstrates how Redis<?php
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
Cache::store('redis')->put('count', 1);
dump(
Cache::store('redis')->get('count'),
);
Cache::store('redis')->increment('count');
Cache::store('redis')->increment('count');
dump(
Cache::store('redis')->get('count'),
);
});
Database
<?php
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
Cache::store('database')->put('count', 1);
dump(
Cache::store('database')->get('count'),
);
Cache::store('database')->increment('count');
Cache::store('database')->increment('count');
dump(
Cache::store('database')->get('count'),
);
}); |
Contributor
Author
|
Valid point - thanks! Is Edit: applied this change for both |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


This PR adds typed getter methods to the cache repository, matching the pattern established in the config repository.
When retrieving values from the cache, static analysis tools struggle with the mixed return type, often requiring a mix of verbose annotations and manual type checks to get them to pass:
Even with a default value,
Cache::get()returns mixed to static analysis tools:With typed getters, this becomes:
The same applies to other types - PHPStan requires a redundant type check:
Becomes:
The default value is returned when the key is missing. If the cached value exists but is an unexpected type, an
InvalidArgumentExceptionis thrown.