-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Asset] Add CachedVersionStrategy to decorate any Asset version strategy #36371
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add CachedVersionStrategy to decorate any Asset version strategy
framework: assets: json_manifest_path: 'https://cdn.example.com/manifest.json' cache_version: true
- Loading branch information
commit dc90706bc90463cd78b053fc3ba3fd95d2af18b3
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
54 changes: 54 additions & 0 deletions
54
src/Symfony/Component/Asset/Tests/VersionStrategy/CachedVersionStrategyTest.php
This file contains hidden or 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,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Asset\Tests\VersionStrategy; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Asset\VersionStrategy\CachedVersionStrategy; | ||
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface; | ||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
|
||
class CachedVersionStrategyTest extends TestCase | ||
{ | ||
public function testGetVersion() | ||
{ | ||
$value = '1.0.0'; | ||
$strategy = $this->getMockBuilder(VersionStrategyInterface::class)->getMock(); | ||
$strategy | ||
->expects($this->once()) | ||
->method('getVersion') | ||
->willReturn($value); | ||
|
||
$cache = new ArrayAdapter(); | ||
$cachedVersionStrategy = new CachedVersionStrategy($strategy, $cache); | ||
$path = 'test-path'; | ||
|
||
$this->assertSame($value, $cachedVersionStrategy->getVersion($path)); | ||
$this->assertSame($value, $cachedVersionStrategy->getVersion($path), '2nd call is cached'); | ||
} | ||
|
||
public function testApplyVersion() | ||
{ | ||
$value = 'test/path/1.0.0'; | ||
$strategy = $this->getMockBuilder(VersionStrategyInterface::class)->getMock(); | ||
$strategy | ||
->expects($this->once()) | ||
->method('applyVersion') | ||
->willReturn($value); | ||
|
||
$cache = new ArrayAdapter(); | ||
$cachedVersionStrategy = new CachedVersionStrategy($strategy, $cache); | ||
$path = 'test/path'; | ||
|
||
$this->assertSame($value, $cachedVersionStrategy->applyVersion($path)); | ||
$this->assertSame($value, $cachedVersionStrategy->applyVersion($path), '2nd call is cached'); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/Symfony/Component/Asset/VersionStrategy/CachedVersionStrategy.php
This file contains hidden or 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,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Asset\VersionStrategy; | ||
|
||
use Symfony\Contracts\Cache\CacheInterface; | ||
|
||
/** | ||
* Cache generated versions and path of any version strategy. | ||
* | ||
* @author Jérôme TAMARELLE <jerome@tamarelle.net> | ||
*/ | ||
class CachedVersionStrategy implements VersionStrategyInterface | ||
{ | ||
private $strategy; | ||
|
||
private $cache; | ||
|
||
public function __construct(VersionStrategyInterface $strategy, CacheInterface $cache) | ||
{ | ||
$this->strategy = $strategy; | ||
$this->cache = $cache; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getVersion(string $path) | ||
{ | ||
return $this->cache->get('v_'.rawurlencode($path), function () use ($path) { | ||
return $this->strategy->getVersion($path); | ||
}); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function applyVersion(string $path) | ||
{ | ||
return $this->cache->get('p_'.rawurlencode($path), function () use ($path) { | ||
return $this->strategy->applyVersion($path); | ||
}); | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not
defaultNull
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've kept the boolean and removed options.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use_cache
=> true/false/string => when string, it's the name of the cache pool to use?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(or just
cache
btw)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't get the name
cache_version
cache: true/false
looks better to me. Do I miss something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've renamed to
cache
.I wanted to avoid confusion. This cache the result of the URL generation, a developer might think this is the HTTP cache when the browser requests the URL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cache_json_manifest
then?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or
json_manifest_cache
might even be better?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can cache any version strategy, not only the manifest.
We could also remove this option and always enable the cache on remote json manifests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we? How would this work with env vars?
Or we might just always enable the cache, even for local files.
For thoughts :)