Skip to content

Commit

Permalink
Improve integrity hash performance by caching
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-alrek committed Feb 12, 2021
1 parent 2c61ff2 commit 56197cd
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/View/Components/MixBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ abstract class MixBase extends Component
/** @var Collection */
protected $files;

/** @var string|null */
/** @var Collection */
public $integrity;

/** @var string|null */
public $crossorigin;

/**
* The cache of integrity hashes.
*
* @var array
*/
protected static $integrityCache = [];

public function __construct($manifestDirectory = '', $integrity = null, $crossorigin = null)
{
if ($manifestDirectory && !Str::startsWith($manifestDirectory, '/')) {
Expand Down Expand Up @@ -56,6 +63,10 @@ public function __construct($manifestDirectory = '', $integrity = null, $crossor

public function integrityKey($file)
{
if (isset(static::$integrityCache[$file])) {
return static::$integrityCache[$file];
}

if ($this->integrity->count()) {
$files = $this->files->flatten();
if ($file = $files->first(function ($path) use ($file) {
Expand All @@ -66,7 +77,7 @@ public function integrityKey($file)
$hashes = $this->integrity->map(function ($algorithm) use ($content) {
return $algorithm . '-' . base64_encode(hash($algorithm, $content, true));
});
return $hashes->join(' ');
return static::$integrityCache[$file] = $hashes->join(' ');
}
}
}
Expand Down

0 comments on commit 56197cd

Please sign in to comment.