spatie/laravel-blink
Request-scoped in-memory cache for Laravel. Use the blink() helper or facade to store and retrieve values for the duration of a single request, with helpers like once(), get/put, wildcard key prefixes, increment, forget/flush, plus ArrayAccess and Countable support.
Blink is available via composer require spatie/laravel-blink. Once installed, it’s ready to use immediately—no config changes or service registration needed. The package registers a Blink service and facade, so you can start caching per-request data right away in your controllers, services, or view composers:
use Spatie\Blink\Blink;
// Using the facade
Blink::put('user_preferences', $this->fetchUserPreferences());
// Using the instance via the container (e.g., in a controller)
$blink = app(Blink::class);
$items = $blink->remember('expensive_query', fn () => HeavyModel::with('relations')->get());
The first use case is usually avoiding repeated lookups of expensive or frequently used data within a single request—like config derived from a database row or computed context for a layout.
remember(): Wrap expensive closures to compute once per request. Blink caches the result and returns it on subsequent calls for the same key.
$user = Blink::remember('user_'.$userId, fn () => User::with('roles', 'permissions')->findOrFail($userId));
// In a middleware
Blink::put('current_route_name', Route::currentRouteName());
// Later in a view composer
$view->with('routeName', Blink::get('current_route_name'));
View::composer('*', function ($view) {
$view->with('navItems', Blink::remember('nav_items', fn () => Navigation::build()));
});
null. Cast to string explicitly if needed.forget() or clear() when needed.
Blink::forget('user_'.$userId); // or
Blink::clear(); // wipe entire store mid-request (e.g., after a state-changing action)
Blink::keys() returns all stored keys; Blink::getMany() fetches multiple at once. Use in tinker or dd(Blink::get('key')) for quick inspection.Blink::put(): Unlike Laravel’s Cache facade, put() overwrites existing values without auto-expiry—even if the same key is re-computed. Always use remember() for safe memoization unless you explicitly want to force an update.Blink::clear() in setUp() or tearDown() when tests mutate Blink state.How can I help you explore Laravel packages today?