1. Go to this page and download the library: Download iazaran/smart-cache 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/ */
iazaran / smart-cache example snippets
use SmartCache\Facades\SmartCache;
// Same API you already know
SmartCache::put('users', $users, 3600);
$users = SmartCache::get('users');
// Remember pattern — with automatic compression & cost tracking
$users = SmartCache::remember('users', 3600, fn() => User::all());
// Helper function
smart_cache(['products' => $products], 3600);
$products = smart_cache('products');
// Each store preserves all SmartCache optimizations
SmartCache::store('redis')->put('key', $value, 3600);
SmartCache::store('memcached')->remember('users', 3600, fn() => User::all());
// Bypass SmartCache when needed
SmartCache::repository('redis')->put('key', $value, 3600);
// Due refreshes run synchronously before this method returns stale data
$data = SmartCache::swr('github_repos', fn() => Http::get('...')->json(), 300, 900);
// Extended stale serving (1 h fresh, 24 h stale)
$config = SmartCache::stale('site_config', fn() => Config::fromDatabase(), 3600, 86400);
// Proactive refresh before expiry
$analytics = SmartCache::refreshAhead('daily_analytics', fn() => Analytics::generateReport(), 1800, 300);
// Queue-based background refresh — use a serializable invokable class
$data = SmartCache::asyncSwr('dashboard_stats', RefreshDashboardStats::class, 300, 900, 'cache-refresh');
// Single-flight regeneration — on a miss, only one process runs the callback
$report = SmartCache::rememberWithLock('daily_report', 3600, fn() => Report::build());
// XFetch algorithm — probabilistic early refresh
$data = SmartCache::rememberWithStampedeProtection('key', 3600, fn() => expensiveQuery());
// Rate-limited regeneration
SmartCache::throttle('api_call', 10, 60, fn() => expensiveApiCall());
// TTL jitter — prevents thundering herd on expiry
SmartCache::withJitter(0.1)->put('popular_data', $data, 3600);
// Actual TTL: 3240–3960 s (±10 %)
// Explicit per-call jitter — applies regardless of the global jitter.enabled flag
SmartCache::putWithJitter('popular_data', $data, 3600, 0.2);
SmartCache::rememberWithJitter('report', 3600, 0.1, fn() => Analytics::generate());
SmartCache::put('app_config', Config::all(), 3600);
// Same content, and the stored entry already covers this window → rewrite skipped
SmartCache::put('app_config', Config::all(), 60);
// Same content, but this asks the entry to live longer → written, so the TTL is honoured
SmartCache::put('app_config', Config::all(), 7200);
SmartCache::remember('analytics', 3600, fn() => AnalyticsService::generateReport());
SmartCache::getCacheValueReport(); // all entries ranked by value
SmartCache::suggestEvictions(5); // lowest-value entries to remove
// With the circuit breaker enabled (circuit_breaker.enabled or withCircuitBreaker()),
// a failing or open circuit returns the fallback; a Closure fallback is called.
$data = SmartCache::withFallback(
fn() => SmartCache::get('key'),
fn() => $this->fallbackSource()
);
$memo = SmartCache::memo();
$users = $memo->remember('users', 3600, fn() => User::all());
$users = $memo->get('users'); // instant — served from memory
config(['smart-cache.strategies.compression.mode' => 'adaptive']);
// Hot data → fast compression (level 3–4), cold data → high compression (level 7–9)
config(['smart-cache.strategies.chunking.lazy_loading' => true]);
$dataset = SmartCache::get('100k_records'); // LazyChunkedCollection
foreach ($dataset as $record) { /* max 3 chunks in memory */ }