spatie/laravel-partialcache
Abandoned package that adds a Blade @cache directive to cache rendered partial HTML in Laravel (5.1+). Supports passing view data, setting cache duration, custom keys, and cache tags, with optional facade/config publishing.
Installation:
composer require spatie/laravel-partialcache
For Laravel <5.5, manually register Spatie\PartialCache\PartialCacheServiceProvider in config/app.php.
First Use Case: Cache a reusable Blade partial (e.g., a sidebar or header) with:
@partialCache('sidebar', '1 hour')
@include('partials.sidebar')
@endPartialCache
'sidebar' (arbitrary string, used for cache tagging).'1 hour' (supports any valid cache duration string, e.g., '5 minutes', '1440 minutes').Verify Cache:
Check storage/framework/cache for generated files (e.g., sidebar_<hash>.html).
Reusable Components: Cache frequently rendered, static partials (e.g., footers, navigation) to reduce Blade rendering overhead.
@partialCache('user_dashboard_header', '24 hours')
@include('dashboard.header', ['user' => $user])
@endPartialCache
Dynamic Keys: Use dynamic keys for partials with variable content (e.g., user-specific widgets):
@partialCache("user_{$user->id}_widget", '10 minutes')
@include('widgets.user', ['user' => $user])
@endPartialCache
Cache Invalidation: Clear specific partials via the facade:
use Spatie\PartialCache\Facades\PartialCache;
// Clear by key
PartialCache::clear('sidebar');
// Clear all partials
PartialCache::clearAll();
Integration with Events: Invalidate caches on data changes (e.g., after updating a user):
event(new UserUpdated($user));
// In listener:
PartialCache::clear("user_{$user->id}_*");
'5 minutes') for partials with frequent updates.@cache: For full-page caching, layer @partialCache inside @cache directives.config/cache.php to test uncached behavior:
'default' => env('CACHE_DRIVER', 'array'), // Use 'array' to disable file caching
Cache Driver Dependency:
redis or database in config/cache.php for distributed environments:
'default' => env('CACHE_DRIVER', 'redis'),
Key Collisions:
user_1_widget, user_1_widgets) may accidentally invalidate each other. Use unique separators:
@partialCache("user_{$user->id}__widget", '10 minutes') <!-- Double underscore -->
Blade Compilation:
bootstrap/cache if Blade changes break rendering:
php artisan cache:clear
php artisan view:clear
Abandoned Package Risks:
Check Cache Files:
Inspect storage/framework/cache for generated files. Delete them manually to force regeneration:
rm -rf storage/framework/cache/*
Log Cache Hits/Misses:
Add debug logging to app/Providers/AppServiceProvider.php:
use Spatie\PartialCache\PartialCache;
public function boot()
{
PartialCache::setLogger(function ($key, $hit) {
\Log::debug("PartialCache [$key]: " . ($hit ? 'HIT' : 'MISS'));
});
}
Test with @if Fallbacks:
Safeguard partials with fallback logic:
@partialCache('sidebar', '1 hour')
@includeIf(file_exists(resource_path('views/partials/sidebar.blade.php')), 'partials.sidebar')
@endPartialCache
Custom Cache Path:
Override the cache directory in config/partialcache.php (if present) or publish the config:
php artisan vendor:publish --provider="Spatie\PartialCache\PartialCacheServiceProvider"
Cache Versioning: Add a version suffix to keys to invalidate all cached partials during deployments:
@partialCache("v{$app->version}_sidebar", '1 hour')
3. **Conditional Caching**:
Use Blade `@if` to conditionally cache partials:
```blade
@if(auth()->check())
@partialCache("user_{$user->id}_dashboard", '1 hour')
@include('dashboard.content')
@endPartialCache
@endif
How can I help you explore Laravel packages today?