One possible solution is to use a cache layer to store the generated URLs and serve them from cache instead of generating a new URL every time. This can be achieved using Laravel's Cache facade. Here's an example code snippet:
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
$url = 'path/to/file';
$expires = now()->addMinutes(15);
// Check if the URL is already cached
if (Cache::has($url)) {
$temporaryUrl = Cache::get($url);
} else {
// Generate a new temporary URL and cache it
$temporaryUrl = Storage::temporaryUrl($url, $expires);
Cache::put($url, $temporaryUrl, );
}
// Use the cached or newly generated URL
echo $temporaryUrl;
This code checks if the URL is already cached using Laravel's Cache facade. If it is, it retrieves the cached URL and uses it. If not, it generates a new temporary URL using Laravel's Storage facade and caches it for 15 minutes using the Cache facade. This way, subsequent requests for the same URL will use the cached URL instead of generating a new one every time.