Skip to content

Commit

Permalink
Merge branch '2.2' into 2
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed May 4, 2024
2 parents ff5e8a7 + 41378dc commit f0e4875
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
31 changes: 23 additions & 8 deletions src/Shortcodes/FileShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,32 @@ public static function handle_shortcode($arguments, $content, $parser, $shortcod
*/
protected static function getCachedMarkup($cache, $cacheKey, $arguments): string
{
// Retrieve the cache markup
$item = $cache->get($cacheKey);
if (empty($item)) {
// This is a cache miss
return '';
}

// Destructure our cache entry
$markup = $item['markup'] ?? '';
$filename = $item['filename'] ?? '';
$hash = $item['hash'] ?? '';
$assetStore = Injector::inst()->get(AssetStore::class);
if ($item && $item['markup'] && !empty($item['filename'])) {
// Initiate a protected asset grant if necessary
$allowSessionGrant = static::getGrant(null, $arguments);
if ($allowSessionGrant && $assetStore->exists($item['filename'], $item['hash'])) {
$assetStore->grant($item['filename'], $item['hash']);
return $item['markup'];
}

// Validate the cache entry
if (empty($markup) || empty($filename) || empty($hash) || !$assetStore->exists($filename, $hash)) {
// Cache entry is wrong, delete it
$cache->delete($cacheKey);
return '';
}
return '';

// Shortcode can be configured to allow session grant to files even if the file is protected
if (static::getGrant(null, $arguments)) {
$assetStore->grant($filename, $hash);
}

return $markup;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Shortcodes/ImageShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use SilverStripe\Assets\Image;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\View\HTML;
use SilverStripe\View\Parsers\ShortcodeHandler;
use SilverStripe\View\Parsers\ShortcodeParser;

Expand Down
28 changes: 28 additions & 0 deletions tests/php/Shortcodes/FileShortcodeProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ protected function setUp(): void
Config::inst()->set(SiteTree::class, 'create_default_pages', true);
ErrorPage::singleton()->requireDefaultRecords();
}

FileShortcodeProvider::getCache()->clear();
}

protected function tearDown(): void
Expand Down Expand Up @@ -179,4 +181,30 @@ public function testMarkupHasStringValue()
'Test that shortcode with invalid file ID is not parsed.'
);
}

public function testCacheHit()
{
$testFile = $this->objFromFixture(File::class, 'asdf');
$testFileID = $testFile->ID;
$tuple = $testFile->File->getValue();

// Prewarm cache with nonsense value
$markup = '<img src="cacheHit">';
$key = FileShortcodeProvider::getCacheKey(['id' => (string)$testFileID], '');
FileShortcodeProvider::getCache()->set($key, [
'markup' => $markup,
'filename' => $tuple['Filename'],
'hash' => $tuple['Hash']
]);

// Try to retrieve short code ... which should hit the cache
$parser = new ShortcodeParser();
$parser->register('file_link', [FileShortcodeProvider::class, 'handle_shortcode']);
$fileShortcode = sprintf('[file_link,id=%d]', $testFileID);
$this->assertEquals(
$markup,
$parser->parse(sprintf('[file_link,id=%d]', $testFileID)),
'Value Stored in the FileShortcodeProvider cache is returned by the shortcode.'
);
}
}

0 comments on commit f0e4875

Please sign in to comment.