Skip to content

Commit

Permalink
Add static variable to cache the asset map in memory
Browse files Browse the repository at this point in the history
This change makes it go from a constant ~2ms per call to essentially zero (0.000ms avg on 100 000 iterations)
  • Loading branch information
caendesilva committed Jul 25, 2024
1 parent 75fc296 commit f0e0064
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ protected static function routeMap(): array
}

/**
* @todo Consider adding caching, since this is not a singleton like the routes.
*
* @return array<string, \Hyde\Support\Filesystem\MediaFile>
*/
protected static function assetMap(): array
{
$assetMap = [];
static $assetMap = null;

if ($assetMap !== null) {
return $assetMap;
}

foreach (MediaFile::all() as $mediaFile) {
$assetMap[$mediaFile->getPath()] = $mediaFile;
Expand Down

0 comments on commit f0e0064

Please sign in to comment.