Skip to content

Instantly share code, notes, and snippets.

@Script47
Last active September 23, 2023 13:43
Show Gist options
  • Save Script47/a0e189eb5b3a352517ae5185510f9e17 to your computer and use it in GitHub Desktop.
Save Script47/a0e189eb5b3a352517ae5185510f9e17 to your computer and use it in GitHub Desktop.
Macro to get TTL when using Laravel's file store cache driver
<?php
namespace App\Providers;
use Illuminate\Cache\FileStore;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Cache::macro('getTTL', function (string $key): ?int {
$fs = new class extends FileStore {
public function __construct()
{
parent::__construct(App::get('files'), config('cache.stores.file.path'));
}
public function getTTL(string $key): ?int
{
return $this->getPayload($key)['time'] ?? null;
}
};
return $fs->getTTL($key);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment