jord-jd/do-file-cache
Laravel file cache driver that stores cache entries as PHP “.do” files. Simple, fast, git-friendly caching for local/dev use, with easy setup and predictable files you can inspect, commit, or clear without a database.
Installation
composer require jord-jd/do-file-cache
Register the service provider in config/app.php:
'providers' => [
// ...
JordJd\DoFileCache\FileCacheServiceProvider::class,
],
Basic Usage
use JordJd\DoFileCache\Facades\FileCache;
// Cache a value
FileCache::put('key', 'value', 3600); // 1 hour TTL
// Retrieve a value
$value = FileCache::get('key');
// Check if key exists
if (FileCache::has('key')) {
// ...
}
First Use Case Cache API responses or database queries in a Laravel controller:
public function getData()
{
return FileCache::remember('api_data', 300, function () {
return Http::get('https://api.example.com/data')->json();
});
}
Cache-as-View
// Cache a Blade view for 10 minutes
FileCache::remember('dashboard_view', 600, function () {
return view('dashboard')->render();
});
Tag-Based Invalidation
// Cache with tags for granular invalidation
FileCache::tags(['users', 'profile'])->put('user_profile', $userData, 3600);
// Later, invalidate by tag
FileCache::tags(['users'])->forget();
Dependency Injection
use JordJd\DoFileCache\Contracts\CacheInterface;
public function __construct(CacheInterface $cache) {
$this->cache = $cache;
}
Laravel Cache Store
Register as a Laravel cache driver in config/cache.php:
'stores' => [
'file_cache' => [
'driver' => 'jord-jd-file-cache',
'path' => storage_path('framework/cache'),
],
],
Then use via Laravel’s cache facade:
Cache::store('file_cache')->put('key', 'value', 3600);
Cache Events
Listen for cache events (e.g., CacheHit, CacheMiss) via Laravel’s event system:
event(new CacheHit($key, $value));
Fallback Logic Combine with Laravel’s cache:
$value = Cache::remember('key', 3600, function () {
return FileCache::get('fallback_key') ?? Http::get('...')->json();
});
File Permissions
Ensure the cache directory (storage/framework/cache by default) is writable:
chmod -R 775 storage/framework/cache
Debugging: Check for PermissionDeniedException if files aren’t writable.
TTL Granularity The package uses seconds for TTL, not minutes (unlike some Laravel helpers).
// ❌ Wrong (caches for 1 minute)
FileCache::put('key', 'value', 1);
// ✅ Correct (caches for 60 seconds)
FileCache::put('key', 'value', 60);
Tag Invalidation Race Conditions
Tag-based invalidation (forget()) may not immediately clear all cached items due to file system latency.
Workaround: Use FileCache::flush() for a full cache wipe if needed.
Memory vs. Disk Tradeoff
Large cached objects (e.g., serialized Blade views) may bloat disk storage.
Tip: Use FileCache::rememberForever() sparingly for static assets.
Cache Directory Inspection
Manually check storage/framework/cache for stale files or corrupted entries.
ls -la storage/framework/cache
Logging
Enable debug mode in config/file-cache.php:
'debug' => env('APP_DEBUG', false),
Logs cache hits/misses to storage/logs/laravel.log.
Manual Cache Flush Clear the cache directory programmatically:
FileCache::flush();
Custom Cache Path Override the default path in config:
'path' => storage_path('app/cache/custom'),
File Naming Strategy
Extend the FileCache class to customize key-to-file hashing:
use JordJd\DoFileCache\Contracts\CacheInterface;
class CustomCache implements CacheInterface {
public function getFileName(string $key): string {
return md5($key . '_custom_salt') . '.cache';
}
// ...
}
Compression Enable GZIP compression for large cached files:
FileCache::compress(true)->put('large_data', $data, 3600);
Event Hooks
Subscribe to cache events in EventServiceProvider:
protected $listen = [
'JordJd\DoFileCache\Events\CacheHit' => [
'App\Listeners\LogCacheHit',
],
];
How can I help you explore Laravel packages today?