From 8abac73aac80c47ec293c87c39c2047bfb302a2b Mon Sep 17 00:00:00 2001 From: David Losada Date: Tue, 7 Apr 2020 19:30:05 +0200 Subject: [PATCH] fix(perf): cache media storage url --- src/App/Formatter/Media.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/App/Formatter/Media.php b/src/App/Formatter/Media.php index 6620f99f4a..07d5d723e0 100644 --- a/src/App/Formatter/Media.php +++ b/src/App/Formatter/Media.php @@ -14,9 +14,13 @@ use Ushahidi\Core\Entity; use Ushahidi\Core\Traits\FormatterAuthorizerMetadata; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Facades\Cache; class Media extends API { + // This is rather long, because what we cache here rarely (if ever) changes + const CACHE_LIFETIME = 24 * 3600 ; # in seconds + use FormatterAuthorizerMetadata; protected function addMetadata(array $data, Entity $media) @@ -60,6 +64,17 @@ protected function formatOFilename($value) array_push($url_path, $filename); $path = implode("/", $url_path); + return Cache::remember( + 'Ushahidi\App\Formatter\Media.publicUrl[' . $path . ']', + self::CACHE_LIFETIME, + function () use ($path) { + return $this->getStorageObjectPublicUrl($path); + } + ); + } + + protected function getStorageObjectPublicUrl(string $path) : string + { $adapter = Storage::getAdapter(); // Special handling for RS to get SSL URLs if ($adapter instanceof \League\Flysystem\Rackspace\RackspaceAdapter) {