jord-jd/do-file-cache-psr-6
PSR-6 cache adapter for Jord-JD/DO File Cache. Use it to access DO File Cache through standard PSR-6 CacheItemPoolInterface for framework-agnostic caching with Composer-based installation.
Installation Require the package via Composer:
composer require jord-jd/do-file-cache-psr-6 do/file-cache
do/file-cache dependency is required but not auto-installed. Add it explicitly.Configure Laravel
Update config/cache.php to include the new store:
'stores' => [
'do_file' => [
'driver' => 'do_file',
'path' => env('DO_CACHE_PATH', storage_path('framework/cache/do')),
'prefix' => env('DO_CACHE_PREFIX', 'do_'),
'key' => env('DO_SPACES_KEY'),
'secret' => env('DO_SPACES_SECRET'),
'region' => env('DO_SPACES_REGION', 'nyc3'),
],
],
Add DO-specific environment variables to .env:
DO_SPACES_KEY=your_key
DO_SPACES_SECRET=your_secret
DO_SPACES_REGION=nyc3
DO_CACHE_PATH=storage/framework/cache/do
First Use Case: Replace File Cache Driver Update your cache calls to use the new driver:
// Before (default file cache)
Cache::remember('key', 60, function () { return expensiveOperation(); });
// After (DO File Cache)
Cache::store('do_file')->remember('key', 60, function () { return expensiveOperation(); });
PSR-6 Compliance
Use the package’s CacheItemPoolInterface directly for low-level control:
$cache = app('cache.store.do_file');
$item = $cache->getItem('key');
if (!$item->isHit()) {
$item->set(expensiveOperation());
$item->expiresAfter(3600);
$cache->save($item);
}
Laravel Cache Facade Integration Leverage Laravel’s facade for consistency:
// Get cached data
$data = Cache::store('do_file')->get('key');
// Set cached data
Cache::store('do_file')->put('key', $data, 3600);
// Tagged caching
Cache::store('do_file')->tags(['users'])->put('user:1', $userData);
Fallback Strategy Implement a fallback to another cache driver (e.g., Redis) if DO File Cache fails:
$cache = Cache::store('do_file');
if (!$cache->get('key')) {
$fallbackCache = Cache::store('redis');
$data = $fallbackCache->get('key') ?: expensiveOperation();
$cache->put('key', $data, 3600);
}
Cache Warming Preload critical caches during application boot:
public function boot()
{
$this->app->booted(function () {
Cache::store('do_file')->remember('homepage_data', 3600, function () {
return HomepageData::fetch();
});
});
}
Dynamic Cache Driver Switching Use environment variables to switch between cache drivers:
$driver = env('CACHE_DRIVER', 'do_file');
$cache = Cache::store($driver);
Tag-Based Invalidation Use tags for grouped cache invalidation (e.g., when a user updates their profile):
Cache::store('do_file')->tags(['user:1'])->flush();
Cache Events
Listen for cache events (e.g., Cache::store('do_file')->getEventDispatcher()) to log or react to cache hits/misses:
Cache::store('do_file')->getEventDispatcher()->addListener(
'CacheItemPool::miss',
function ($args) {
Log::debug('Cache miss for key: ' . $args['key']);
}
);
Custom Cache Keys Override the default key prefix to avoid collisions:
Cache::store('do_file')->setPrefix('app_');
Testing
Mock the CacheItemPoolInterface in tests:
$mockCache = Mockery::mock('overload:\Psr\Cache\CacheItemPoolInterface');
$mockCache->shouldReceive('getItem')->andReturnUsing(function ($key) {
return new \Psr\Cache\CacheItem($key, false);
});
$this->app->instance('cache.store.do_file', $mockCache);
DO Infrastructure Dependency
storage_path('framework/cache/do')) if DO-specific features aren’t needed.Concurrency Issues
Cache::lock() for critical sections or implement a retry mechanism:
Cache::store('do_file')->lock('key', 5, function () {
// Critical section
});
Cache Invalidation Delays
Cache::store('do_file')->tags(['users'])->flush();
dispatch(new FlushDoFileCacheTags(['users']));
Credential Management
.env can be risky. Ensure credentials are rotated regularly.Laravel Version Mismatches
composer.json for Laravel version constraints and test thoroughly.Cold Start Latency
php artisan cache:warm
Enable Debug Logging Add debug logging for cache operations:
Cache::store('do_file')->extend('do_file', function ($app) {
return new class($app['cache.do_file.store']) extends \Illuminate\Cache\Repository {
public function get($key, $default = null) {
$result = parent::get($key, $default);
\Log::debug("DO Cache Hit: {$key}", ['hit' => $result !== $default]);
return $result;
}
};
});
Check DO Filesystem Permissions Ensure the DO filesystem (or local path) is writable:
chmod -R 775 storage/framework/cache/do
Validate DO Credentials Test DO API connectivity separately:
use DO\FileCache\Client;
$client = new Client(env('DO_SPACES_KEY'), env('DO_SPACES_SECRET'));
$client->ping(); // Check connectivity
Monitor Cache Hit/Miss Ratios
Use Laravel’s cache:clear logs or a custom middleware to track performance:
Cache::store('do_file')->getEventDispatcher()->addListener(
'CacheItemPool::miss',
function ($args) {
\Log::warning("Cache miss for key: {$args['key']}");
}
);
Use for Read-Heavy Workloads DO File Cache excels at read-heavy scenarios (e.g., API responses, template fragments). Avoid using it for write-heavy operations.
Combine with Other Drivers Use DO File Cache as a fallback or secondary cache:
$primaryCache = Cache::store('redis');
$fallbackCache = Cache::store('do_file');
$data = $primaryCache->get('key') ?: $fallbackCache->get('key');
Optimize Cache Expiry Set appropriate TTLs to balance freshness and performance:
// Short TTL for volatile data
Cache::store('do_file')->put('volatile_data', $data, 60);
// Long TTL for static data
Cache::store('do_file')->put('static_data', $data, 86400);
Leverage DO’s Global Filesystem If using DO Spaces, leverage its global CDN for faster reads:
Cache::store('do_file')->
How can I help you explore Laravel packages today?