Skip to content
Snippets Groups Projects
Commit 8267e963 authored by Kevin von Spiczak's avatar Kevin von Spiczak
Browse files

[TASK] switch from registry to caching framework

parent a4a6beb9
No related branches found
Tags 2.0.3
1 merge request!5[TASK] switch from registry to caching framework
......@@ -21,6 +21,7 @@
namespace SGalinski\SgVimeo\Controller;
use SGalinski\SgVimeo\Service\VimeoService;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Resource\FileRepository;
......@@ -32,6 +33,18 @@ use TYPO3\CMS\Extbase\Service\ImageService;
* The Vimeo Controller
*/
class VimeoController extends ActionController {
/**
* @var FrontendInterface
*/
protected $cache;
/**
* @param FrontendInterface $cache
*/
public function __construct(FrontendInterface $cache) {
$this->cache = $cache;
}
/**
* Renders the Vimeo video view
*
......@@ -56,7 +69,8 @@ class VimeoController extends ActionController {
VimeoService::class,
$clientId,
$clientSecret,
$personalAccessToken
$personalAccessToken,
$this->cache
);
try {
$response = $vimeoService->getVimeoData($id, $maxResultsWithFilters);
......
......@@ -28,6 +28,7 @@ namespace SGalinski\SgVimeo\Service;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Registry;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use Vimeo\Exceptions\VimeoRequestException;
......@@ -48,6 +49,13 @@ class VimeoService implements LoggerAwareInterface {
*/
protected const SCOPE = 'public';
public const CACHE_LIFETIME_IN_SECONDS = 86400;
/**
* @var FrontendInterface
*/
protected $cache;
/**
* @var Vimeo
*/
......@@ -78,7 +86,9 @@ class VimeoService implements LoggerAwareInterface {
* @param string $clientSecret
* @param string $personalAccessToken
*/
public function __construct(string $clientId, string $clientSecret, string $personalAccessToken) {
public function __construct(
string $clientId, string $clientSecret, string $personalAccessToken, FrontendInterface $cache
) {
$this->vimeoApiClient = new Vimeo($clientId, $clientSecret, $personalAccessToken);
// We only need to request an unauthenticated token, if there is no personal access token provided already.
// An authenticated access token with the public scope is identical to an unauthenticated access token,
......@@ -88,6 +98,7 @@ class VimeoService implements LoggerAwareInterface {
if ($personalAccessToken === '') {
$this->requestAccessToken();
}
$this->cache = $cache;
}
/**
......@@ -98,20 +109,12 @@ class VimeoService implements LoggerAwareInterface {
public function getVimeoData(string $vimeoId, int $maxResults): ?array {
$response = [];
$this->maxResultsPerPage = $maxResults;
/** @var Registry $registry */
$registry = GeneralUtility::makeInstance(Registry::class);
$currentDay = date('Y-m-d', $GLOBALS['EXEC_TIME']);
$cacheKey = sha1($vimeoId . $maxResults);
$cacheKey = 'sg_vimeo' . sha1($vimeoId . $maxResults);
$disableVimeoCache = (bool) GeneralUtility::_GP('disableVimeoCache');
if (!$disableVimeoCache) {
$cachedResult = $registry->get('sg_vimeo', $cacheKey);
$cachedResult = $this->cache->get($cacheKey);
if ($cachedResult) {
if ($cachedResult['CACHE_DATE'] === $currentDay) {
return $cachedResult;
}
$registry->remove('sg_vimeo', $cacheKey);
return $cachedResult;
}
}
......@@ -129,8 +132,7 @@ class VimeoService implements LoggerAwareInterface {
}
if (!$disableVimeoCache) {
$response['CACHE_DATE'] = $currentDay;
$registry->set('sg_vimeo', $cacheKey, $response);
$this->cache->set($cacheKey, $response, [], self::CACHE_LIFETIME_IN_SECONDS);
}
return $response;
......
......@@ -6,6 +6,15 @@ services:
SGalinski\SgVimeo\:
resource: '../Classes/*'
SGalinski\SgVimeo\Preview\PreviewRenderer:
public: true
\ No newline at end of file
public: true
cache.sgvimeo_cache:
class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
factory: [ '@TYPO3\CMS\Core\Cache\CacheManager', 'getCache' ]
arguments: [ 'sgvimeo_cache' ]
SGalinski\SgVimeo\Controller\VimeoController:
arguments:
$cache: '@cache.sgvimeo_cache'
......@@ -48,6 +48,9 @@ $iconRegistry->registerIcon(
['source' => 'EXT:sg_vimeo/Resources/Public/Icons/vimeo.svg']
);
// Caching
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['sgvimeo_cache']
??= [];
// Hooks
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem']['sg_vimeo']
= \SGalinski\SgVimeo\Hooks\PageLayoutView\PluginRenderer::class;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment