Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(perf): cache media storage url #3917

Merged
merged 3 commits into from Apr 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/App/Formatter/Media.php
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down