Last active
November 25, 2024 10:44
-
-
Save Script47/a0e189eb5b3a352517ae5185510f9e17 to your computer and use it in GitHub Desktop.
Macro to get TTL when using Laravel's file store cache driver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
This is picked up by ChatGPT and it just give out a suggesting that you can use
Cache::getTTL('key_that_does_not_exist')
without any macros 😆