-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Incenteev/cache
Implement caching of the asset hashes for prod
- Loading branch information
Showing
21 changed files
with
406 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Incenteev\HashedAssetBundle\CacheWarmer; | ||
|
||
use Symfony\Component\Finder\Finder; | ||
use Symfony\Component\Finder\SplFileInfo; | ||
|
||
class AssetFinder | ||
{ | ||
private $webRoot; | ||
|
||
public function __construct(string $webRoot) | ||
{ | ||
$this->webRoot = $webRoot; | ||
} | ||
|
||
public function getAssetPaths(): \Traversable | ||
{ | ||
$finder = (new Finder())->files() | ||
->in($this->webRoot); | ||
|
||
/** @var SplFileInfo $file */ | ||
foreach ($finder as $file) { | ||
yield $file->getRelativePathname(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Incenteev\HashedAssetBundle\CacheWarmer; | ||
|
||
use Incenteev\HashedAssetBundle\Hashing\AssetHasherInterface; | ||
use Incenteev\HashedAssetBundle\Hashing\CachedHasher; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Symfony\Component\Cache\Adapter\AdapterInterface; | ||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Component\Cache\Adapter\PhpArrayAdapter; | ||
use Symfony\Component\Cache\Adapter\ProxyAdapter; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
|
||
final class HashCacheWarmer implements CacheWarmerInterface | ||
{ | ||
private $assetFinder; | ||
private $cacheFile; | ||
private $hasher; | ||
private $fallbackPool; | ||
|
||
public function __construct(AssetFinder $assetFinder, string $cacheFile, AssetHasherInterface $hasher, CacheItemPoolInterface $fallbackPool) | ||
{ | ||
$this->assetFinder = $assetFinder; | ||
$this->cacheFile = $cacheFile; | ||
$this->hasher = $hasher; | ||
|
||
if (!$fallbackPool instanceof AdapterInterface) { | ||
$fallbackPool = new ProxyAdapter($fallbackPool); | ||
} | ||
|
||
$this->fallbackPool = $fallbackPool; | ||
} | ||
|
||
public function warmUp($cacheDir) | ||
{ | ||
$phpArrayPool = new PhpArrayAdapter($this->cacheFile, $this->fallbackPool); | ||
$arrayPool = new ArrayAdapter(0, false); | ||
|
||
$hasher = new CachedHasher($this->hasher, $arrayPool); | ||
|
||
foreach ($this->assetFinder->getAssetPaths() as $path) { | ||
$hasher->computeHash($path); | ||
} | ||
|
||
$values = $arrayPool->getValues(); | ||
$phpArrayPool->warmUp($values); | ||
|
||
foreach ($values as $k => $v) { | ||
$item = $this->fallbackPool->getItem($k); | ||
$this->fallbackPool->saveDeferred($item->set($v)); | ||
} | ||
$this->fallbackPool->commit(); | ||
} | ||
|
||
public function isOptional() | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Incenteev\HashedAssetBundle\Hashing; | ||
|
||
interface AssetHasherInterface | ||
{ | ||
public function computeHash(string $path): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Incenteev\HashedAssetBundle\Hashing; | ||
|
||
use Psr\Cache\CacheItemPoolInterface; | ||
|
||
final class CachedHasher implements AssetHasherInterface | ||
{ | ||
private $hasher; | ||
private $cache; | ||
|
||
public function __construct(AssetHasherInterface $hasher, CacheItemPoolInterface $cache) | ||
{ | ||
$this->hasher = $hasher; | ||
$this->cache = $cache; | ||
} | ||
|
||
public function computeHash(string $path): string | ||
{ | ||
// The hashing implementation does not care about leading slashes in the path, so share cache keys for them | ||
$item = $this->cache->getItem(base64_encode(ltrim($path, '/'))); | ||
|
||
if ($item->isHit()) { | ||
return $item->get(); | ||
} | ||
|
||
$hash = $this->hasher->computeHash($path); | ||
|
||
$item->set($hash); | ||
$this->cache->save($item); | ||
|
||
return $hash; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Incenteev\HashedAssetBundle\Hashing; | ||
|
||
final class FileHasher implements AssetHasherInterface | ||
{ | ||
private $webRoot; | ||
|
||
public function __construct(string $webRoot) | ||
{ | ||
$this->webRoot = $webRoot; | ||
} | ||
public function computeHash(string $path): string | ||
{ | ||
$fullPath = $this->webRoot.'/'.ltrim($path, '/'); | ||
|
||
if (!is_file($fullPath)) { | ||
return ''; | ||
} | ||
|
||
return substr(sha1_file($fullPath), 0, 7); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<parameters> | ||
<parameter key="incenteev_hashed_asset.cache.file">%kernel.cache_dir%/incenteev_asset_hashes.php</parameter> | ||
</parameters> | ||
|
||
<services> | ||
<service id="incenteev_hashed_asset.cached_hasher" class="Incenteev\HashedAssetBundle\Hashing\CachedHasher" public="false" decorates="incenteev_hashed_asset.asset_hasher"> | ||
<argument type="service" id="incenteev_hashed_asset.cached_hasher.inner" /> | ||
<argument type="service"> | ||
<service class="Symfony\Component\Cache\Adapter\PhpArrayAdapter"> | ||
<factory class="Symfony\Component\Cache\Adapter\PhpArrayAdapter" method="create" /> | ||
<argument>%incenteev_hashed_asset.cache.file%</argument> | ||
<argument type="service" id="cache.incenteev_hashed_asset" /> | ||
</service> | ||
</argument> | ||
</service> | ||
|
||
<service id="incenteev_hashed_asset.cache_warmer" class="Incenteev\HashedAssetBundle\CacheWarmer\HashCacheWarmer" public="false"> | ||
<argument type="service" id="incenteev_hashed_asset.asset_finder" /> | ||
<argument>%incenteev_hashed_asset.cache.file%</argument> | ||
<argument type="service" id="incenteev_hashed_asset.file_hasher" /> | ||
<argument type="service" id="cache.incenteev_hashed_asset" /> | ||
<tag name="kernel.cache_warmer" /> | ||
</service> | ||
|
||
<service id="incenteev_hashed_asset.asset_finder" class="Incenteev\HashedAssetBundle\CacheWarmer\AssetFinder" public="false"> | ||
<argument /> | ||
</service> | ||
|
||
<service id="cache.incenteev_hashed_asset" parent="cache.system" public="false" /> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.