nao-pon/flysystem-cached-extra
TemporaryFile, FileLocking, and Streaming, enabling micro-optimizations for:
Storage::put('temp/job-123.json', $data)).FileLockingTrait prevents race conditions in multi-process environments (e.g., Horizon workers).StreamingTrait reduces memory usage for large files (e.g., video processing pipelines).Storage facade and CachedAdapter, requiring minimal refactoring.storage:linked) and queue workers (e.g., delayed jobs with locked files).Filesystem. Best used as a layered extension.public/ assets).league/flysystem (≥3.0) and league/flysystem-cached-adapter (≥1.1).Storage facade or Flysystem-compatible setup.CachedAdapter with traits via DI or facade overrides.Storage::extend('cached_temp', function ($app) {
$adapter = new CachedAdapter(
Storage::disk('s3')->getAdapter(),
new TemporaryFileTrait($app['cache.store'])
);
return new Filesystem($adapter);
});
InMemoryAdapter).php-cs-fixer parallel runs) and cache eviction policies.| Risk Area | Mitigation Strategy |
|---|---|
| Cache Invalidation | Implement Cache::forget() hooks for file deletions or use TemporaryFile::expire(). |
| Race Conditions | Test with php-cs-fixer parallel runs or Laravel’s sync:work for file locks. |
| Memory Leaks | Monitor memory_get_usage() in long-running processes; set cacheTTL conservatively. |
| Vendor Lock-in | Traits are composable; can swap CachedAdapter for Symfony’s CacheAdapter if needed. |
| Lock Contention | Use FileLockingTrait::$timeout to avoid deadlocks; consider RedisLock for distributed systems. |
FileLockingTrait’s flock-based locking sufficient, or needed Redis/Database-backed locks?clear() calls.)Storage::disk('s3'))?CacheException) be surfaced (Sentry, logs, Telescope)?file driver) become a bottleneck under load?local/s3 disks with cached_temp for ephemeral or high-frequency files.TemporaryFileTrait for job payloads (e.g., Storage::put('temp/job-123.json', $data)).FileLockingTrait usage in delayed jobs.flysystem-compatible app (e.g., Symfony, Lumen) via direct adapter injection.Storage facade.local) with cached_temp for a non-critical endpoint (e.g., user uploads).Storage::disk()->getAdapter()->getCache()->stats()).copy(), delete()).telescope to log storage events and laravel-debugbar to inspect adapters.TemporaryFileTrait.
Storage::put('uploads/temp-'.$jobId.'.json', $data) with cached_temp disk.FileLockingTrait for critical sections (e.g., invoice generation).
php-cs-fixer --parallel to simulate concurrency.StreamingTrait for large file uploads (e.g., video processing).CachedAdapter usages with flysystem-cached-extra traits.Redis-backed file locks).league/flysystem-cached-adapter (same method signatures).flysystem’s semantic versioning. Test against flysystem 4.0+ if upgrading.StreamingTrait config options or deprecate flock-based locking in favor of distributed locks.league/flysystem and league/flysystem-cached-adapter to compatible versions.composer.json:
"require": {
"league/flysystem": "^3.0",
"league/flysystem-cached-adapter": "^1.1",
"nao-pon/flysystem-cached-extra": "^1.0",
"symfony/cache": "^5.0" // For Psr6CacheStorage
}
filesystems.php:
'disks' => [
'cached_temp' => [
'driver' => 'custom',
'adapter' => NaoPon\CachedExtra\CachedAdapter::class,
'source' => 's3',
'cache' => env('CACHE_DRIVER', 'file'), // 'redis', 'array', or 'file'
'traits' => [
NaoPon\CachedExtra\Traits\TemporaryFileTrait::class,
NaoPon\CachedExtra\Traits\FileLockingTrait::class,
],
'options' => [
'cacheTTL' => 3600, // 1 hour
'lockTimeout' => 30, // seconds
],
],
],
AppServiceProvider:
public function register()
{
Storage::extend('cached_temp', function ($app) {
$adapter = new CachedAdapter(
Storage::disk('s3')->getAdapter(),
new Psr6CacheStorage($app['cache.store'])
);
$adapter->useTrait(new TemporaryFileTrait());
$adapter->useTrait(new FileLockingTrait());
return new Filesystem($adapter);
});
How can I help you explore Laravel packages today?