-
Notifications
You must be signed in to change notification settings - Fork 16
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 #53 from nlx-lars/feature/shorten-tags
FEATURE: Add option to use short md5 for cache tags
- Loading branch information
Showing
6 changed files
with
99 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace MOC\Varnish\Service; | ||
|
||
use Neos\Flow\Annotations as Flow; | ||
|
||
/** | ||
* @Flow\Scope("singleton") | ||
*/ | ||
class CacheTagService | ||
{ | ||
|
||
/** | ||
* @var int | ||
* @Flow\InjectConfiguration(path="cacheHeaders") | ||
*/ | ||
protected $cacheHeaderConfiguration; | ||
|
||
/** | ||
* @see \Neos\Fusion\Core\Cache\ContentCache::sanitizeTags() | ||
* | ||
* @param array $tags | ||
* @return array | ||
*/ | ||
public function sanitizeTags(array $tags): array | ||
{ | ||
return array_map(function (string $tag) { | ||
return strtr($tag, '.:', '_-'); | ||
}, $tags); | ||
} | ||
|
||
/** | ||
* Generate short md5 for cache tags if enabled | ||
* | ||
* See these two configuration options: | ||
* * MOC.Varnish.cacheHeaders.shortenCacheTags | ||
* * MOC.Varnish.cacheHeaders.cacheTagLength | ||
* | ||
* @param array $tags | ||
* @return array | ||
*/ | ||
public function shortenTags(array $tags = []): array | ||
{ | ||
if (!$this->shouldShortenTags()) { | ||
return $tags; | ||
} | ||
|
||
return array_map(function (string $tag) { | ||
return substr(md5($tag), 0, (int)$this->cacheHeaderConfiguration['cacheTagLength'] ?? 8); | ||
}, $tags); | ||
} | ||
|
||
protected function shouldShortenTags(): bool | ||
{ | ||
return (bool)$this->cacheHeaderConfiguration['shortenCacheTags'] ?? false; | ||
} | ||
|
||
} |
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