FIX Cache duplicate embeds separately

This commit is contained in:
Steve Boyd 2021-06-29 12:17:07 +12:00
parent e812999632
commit 0b979dc345
2 changed files with 32 additions and 8 deletions

View File

@ -61,9 +61,13 @@ class EmbedShortcodeProvider implements ShortcodeHandler
return ''; return '';
} }
$class = $arguments['class'] ?? '';
$width = $arguments['width'] ?? '';
$height = $arguments['height'] ?? '';
// Try to use cached result // Try to use cached result
$cache = static::getCache(); $cache = static::getCache();
$key = static::deriveCacheKey($serviceURL); $key = static::deriveCacheKey($serviceURL, $class, $width, $height);
try { try {
if ($cache->has($key)) { if ($cache->has($key)) {
return $cache->get($key); return $cache->get($key);
@ -273,11 +277,14 @@ class EmbedShortcodeProvider implements ShortcodeHandler
if (!isset($tag['open']) || $tag['open'] != 'embed') { if (!isset($tag['open']) || $tag['open'] != 'embed') {
continue; continue;
} }
$url = $tag['content'] ?? $tag['attrs']['url'] ?? null; $url = $tag['content'] ?? $tag['attrs']['url'] ?? '';
$class = $tag['attrs']['class'] ?? '';
$width = $tag['attrs']['width'] ?? '';
$height = $tag['attrs']['height'] ?? '';
if (!$url) { if (!$url) {
continue; continue;
} }
$key = static::deriveCacheKey($url); $key = static::deriveCacheKey($url, $class, $width, $height);
try { try {
if (!$cache->has($key)) { if (!$cache->has($key)) {
continue; continue;
@ -301,9 +308,23 @@ class EmbedShortcodeProvider implements ShortcodeHandler
* @param string $url * @param string $url
* @return string * @return string
*/ */
private static function deriveCacheKey(string $url): string private static function deriveCacheKey(string $url, string $class, string $width, string $height): string
{ {
$key = 'embed-shortcode-' . preg_replace('/[^a-zA-Z0-9\-]/', '', $url); return implode('-', array_filter([
return $key; 'embed-shortcode',
self::cleanKeySegment($url),
self::cleanKeySegment($class),
self::cleanKeySegment($width),
self::cleanKeySegment($height)
]));
}
/**
* @param string $str
* @return string
*/
private static function cleanKeySegment(string $str): string
{
return preg_replace('/[^a-zA-Z0-9\-]/', '', $str);
} }
} }

View File

@ -139,10 +139,13 @@ EOS
$cache = $method->invokeArgs($provider, []); $cache = $method->invokeArgs($provider, []);
$method = $reflector->getMethod('deriveCacheKey'); $method = $reflector->getMethod('deriveCacheKey');
$method->setAccessible(true); $method->setAccessible(true);
$key = $method->invokeArgs($provider, [$url]); $class = 'leftAlone ss-htmleditorfield-file embed';
$width = '480';
$height = '270';
$key = $method->invokeArgs($provider, [$url, $class, $width, $height]);
// assertions // assertions
$this->assertEquals('embed-shortcode-httpwwwtest-servicecomabc123', $key); $this->assertEquals('embed-shortcode-httpwwwtest-servicecomabc123-leftAloness-htmleditorfield-fileembed-480-270', $key);
$cache->set($key, $embedHtml); $cache->set($key, $embedHtml);
$this->assertTrue($cache->has($key)); $this->assertTrue($cache->has($key));
EmbedShortcodeProvider::flushCachedShortcodes($parser, $content); EmbedShortcodeProvider::flushCachedShortcodes($parser, $content);