PHP code example of skaisser / laravel-cache-cascade
1. Go to this page and download the library: Download skaisser/laravel-cache-cascade library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Skaisser\CacheCascade\Facades\CacheCascade;
// Get data with automatic fallback
$data = CacheCascade::get('settings', []);
// Get with custom options
$faqs = CacheCascade::get('faqs', [], [
'ttl' => 3600, // 1 hour
'transform' => fn($data) => collect($data)->sortBy('order')
]);
// Set data (updates all layers)
CacheCascade::set('settings', $data);
// Clear specific cache (both methods work)
CacheCascade::clearCache('settings');
CacheCascade::forget('settings'); // Laravel-style alias
// Using the global helper
$settings = cache_cascade('settings', []); // Get with default
$value = cache_cascade('key', function() { // Remember pattern
return expensive_operation();
});
// Cache data with a callback (original method)
$users = CacheCascade::remember('active-users', function() {
return User::where('active', true)->get();
}, 3600); // Cache for 1 hour
// Laravel-compatible signature with rememberFor()
$posts = CacheCascade::rememberFor('recent-posts', 3600, function() {
return Post::recent()->limit(10)->get();
});
// Using the helper function
$data = cache_cascade('expensive-data', function() {
return expensive_computation();
});
// Enable for specific cache
$userData = CacheCascade::get('user-settings', [], [
'visitor_isolation' => true
]);
// Or use remember with isolation
$userDashboard = CacheCascade::remember('dashboard', function() {
return $this->generateDashboard();
}, 3600, true); // Last parameter enables visitor isolation
// If 'faqs' table is empty, it will run FaqSeeder automatically
$faqs = CacheCascade::get('faqs');
// The package will look for:
// 1. Cache key 'cascade:faqs'
// 2. File at 'config/dynamic/faqs.php'
// 3. App\Models\Faq::orderBy('order')->get()
// 4. Database\Seeders\FaqSeeder (if auto_seed is enabled)
use Skaisser\CacheCascade\Facades\CacheCascade;
// Invalidate all cache layers (cache + file)
CacheCascade::invalidate('settings');
// Refresh from database and update all layers
$freshData = CacheCascade::refresh('settings');
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Skaisser\CacheCascade\Traits\CascadeInvalidation;
class Faq extends Model
{
use CascadeInvalidation;
protected $fillable = ['question', 'answer', 'order'];
// Optional: Customize the cache key (defaults to table name)
public function getCascadeCacheKey(): ?string
{
return 'faqs'; // This will be the cache key
}
// Optional: Customize what data gets cached
public function scopeForCascadeCache($query)
{
return $query->where('active', true)->orderBy('order');
}
}
// This will automatically invalidate cache and file, then refresh from database
$faq = Faq::find(1);
$faq->update(['answer' => 'Updated answer']);
// Cache has been automatically refreshed!
$cachedFaqs = CacheCascade::get('faqs'); // Fresh data from database
// Manually refresh cache for a model
$faq = Faq::first();
$faq->refreshCascadeCache();
// Enable tags in config
'use_tags' => true,
'cache_tag' => 'my-app-cache',
// Clear all cascade caches
CacheCascade::clearAllCache();
use Skaisser\CacheCascade\Facades\CacheCascade;
// Get store configuration with fallback
$storeConfig = CacheCascade::get('store-config', [
'currency' => 'USD',
'tax_rate' => 0.08
]);
// Remember computed values with Laravel-compatible syntax
$shippingRates = CacheCascade::rememberFor('shipping-rates', 3600, function() {
return ShippingProvider::calculateRates();
});
// Clear cache when admin updates settings
CacheCascade::forget('store-config');
CacheCascade::forget('shipping-rates');
// Enable visitor isolation for tenant-specific data
$tenantSettings = CacheCascade::remember('tenant-settings', function() {
return Tenant::current()->settings;
}, 86400, true); // true enables visitor isolation
// Or use the global helper
$features = cache_cascade('tenant-features', function() {
return Feature::forTenant(tenant())->get();
});
// Model with automatic cache invalidation
class Page extends Model
{
use CascadeInvalidation;
public function getCascadeCacheKey(): ?string
{
return 'pages';
}
}
// Usage in controllers
$pages = CacheCascade::rememberFor('pages', 7200, function() {
return Page::published()->with('author')->get();
});
// When a page is updated, cache automatically refreshes!
$page->update(['title' => 'New Title']); // Triggers cache invalidation
use Skaisser\CacheCascade\Helpers\ConfigCacheHelper;
// Get all static config files (excluding cascade files)
$staticFiles = ConfigCacheHelper::getStaticConfigFiles();
// Get all cascade cache keys from file storage
$cascadeKeys = ConfigCacheHelper::getFileStorageKeys();