Cookbook and Process Flows#
This page shows practical end-to-end usage patterns so you can wire CacheLayer into real application flows quickly and safely.
Flow 1: File Adapter (Single Host)#
Use this when your app runs on one machine (or shared filesystem) and you want zero external dependencies.
Process flow:
Create the cache pool with a stable namespace and directory.
Read through cache using
remember().Tag related entries so updates can invalidate groups.
Export metrics for visibility.
use Infocyph\CacheLayer\Cache\Cache;
$cache = Cache::file('shop', __DIR__ . '/storage/cache');
// Read-through cache on miss with stampede protection.
$product = $cache->remember('product:42', function ($item) {
$item->expiresAfter(300);
return loadProductFromDatabase(42);
}, tags: ['products', 'product:42']);
// On product update, invalidate only related cache.
$cache->invalidateTags(['products', 'product:42']);
// Optional: inspect adapter-level metrics.
$metrics = $cache->exportMetrics();
// ['file' => ['hit' => ..., 'miss' => ..., 'set' => ...]]
Flow 2: PDO Adapter (SQLite Default, MySQL/PostgreSQL Ready)#
Use this when you want SQL-backed caching and easy portability across SQLite/MySQL/MariaDB/PostgreSQL via PDO.
Process flow:
Start local/dev with default SQLite behavior (no DSN required).
Move to MySQL/PostgreSQL in staging/production by changing DSN only.
Keep the same cache API and tagging flow.
Keep stampede protection enabled via the auto-configured PDO lock provider.
use Infocyph\CacheLayer\Cache\Cache;
// Development: defaults to sqlite:<temp-file>
$cache = Cache::pdo('billing');
// Production example (PostgreSQL):
// $cache = Cache::pdo(
// 'billing',
// 'pgsql:host=127.0.0.1;port=5432;dbname=app',
// 'postgres',
// 'secret',
// );
$invoice = $cache->remember('invoice:2026-1001', function ($item) {
$item->expiresAfter(180);
return buildInvoicePayload(1001);
}, tags: ['invoices', 'customer:77']);
// Invalidate by business scope when source data changes.
$cache->invalidateTag('customer:77');
Recommended Rollout Pattern#
Start with
Cache::local()orCache::file().Add tags to all business-domain cache keys.
Replace direct
get()+set()misses withremember().Watch
exportMetrics()and tune TTL values.Switch adapter backend only when scaling needs change.