Created
August 4, 2015 07:12
-
-
Save htuscher/c1e22caf82e79b026d35 to your computer and use it in GitHub Desktop.
TYPO3 Caching Framework nutzen
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
<?php | |
namespace Vendor\MyExtension\Service; | |
class Caching { | |
const DEFAULT_CACHE_EXTENSIONKEY = 'my_cache'; | |
/** | |
* @var \TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend | |
*/ | |
protected $cacheInstance; | |
/** | |
* @var string | |
*/ | |
protected $extensionName; | |
/** | |
* Generate unique cache identifier from string | |
* | |
* @param string $token | |
* | |
* @return string | |
*/ | |
public function calculateCacheIdentifierWithRandom($token) { | |
return sha1($token . rand()); | |
} | |
/** | |
* Initialize caching instance | |
* Beware, if you're not using the standard configuration, you need to implement a new clearCacheHook | |
* | |
* @param string $extensionName | |
*/ | |
public function initializeCache($extensionName) { | |
$this->extensionName = $extensionName; | |
if(!is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$extensionName])) | |
$extensionName = self::DEFAULT_CACHE_EXTENSIONKEY; | |
\TYPO3\CMS\Core\Cache\Cache::initializeCachingFramework(); | |
try { | |
$this->cacheInstance = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager')->getCache($extensionName); | |
} catch (\TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException $e) { | |
$this->cacheInstance = $GLOBALS['typo3CacheFactory']->create( | |
$extensionName, | |
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$extensionName]['frontend'], | |
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$extensionName]['backend'], | |
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$extensionName]['options'] | |
); | |
} | |
} | |
/** | |
* Store the value in the cache | |
* | |
* @param string $cacheIdentifier | |
* @param string $entry | |
* @param array $tags | |
* @param int $lifetime | |
*/ | |
public function set($cacheIdentifier, $entry, array $tags = null, $lifetime = 3600) { | |
$this->cacheInstance->set($this->extensionName.$cacheIdentifier, $entry, $tags, $lifetime); | |
} | |
/** | |
* Return the cached value or FALSE if it doesn't exist | |
* | |
* @param string $cacheIdentifier | |
* | |
* @return string|false | |
*/ | |
public function get($cacheIdentifier) { | |
return $this->cacheInstance->get($this->extensionName.$cacheIdentifier); | |
} | |
/** | |
* Delete entry with $cacheIdentifier | |
* | |
* @param $cacheIdentifier | |
*/ | |
public function remove($cacheIdentifier) { | |
$this->cacheInstance->remove($this->extensionName.$cacheIdentifier); | |
} | |
/** | |
* Clear the entire cache | |
*/ | |
public function flush() { | |
$this->cacheInstance->flush(); | |
} | |
/** | |
* Clear the cache by a tag | |
* | |
* @param string $tag | |
*/ | |
public function flushByTag($tag) { | |
$this->cacheInstance->flushByTag($tag); | |
} | |
} |
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
<?php | |
namespace Vendor\MyExtension\Controller; | |
class SomeController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController | |
{ | |
/** | |
* @var \Vendor\MyExtension\Service\Caching | |
* @inject | |
*/ | |
protected $caching; | |
/** | |
* someRepository | |
* | |
* @var \Vendor\MyExtension\Domain\Repository\SomeRepository | |
* @inject | |
*/ | |
protected $someRepository = NULL; | |
/** | |
* @return void | |
*/ | |
public function initializeAction() | |
{ | |
$this->caching->initializeCache(\TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($this->extensionName)); | |
parent::initializeAction(); | |
} | |
/** | |
* getSome | |
* | |
* @param \Vendor\MyExtension\Domain\Model\Some $args | |
* | |
* @return string | |
*/ | |
protected function getSome($args) | |
{ | |
$cacheIdentifier = 'some' . $args->getUid(); | |
if ( ($output = $this->caching->get($cacheIdentifier)) !== false ) { | |
return $output; | |
} | |
$relatedThings = $this->someRepository->findByRelation($args); | |
$this->view->assign('stuff',$relatedThings); | |
$output = $this->view->render(); | |
$this->caching->set($cacheIdentifier, $output, array('offers', 'market'), 86400); | |
return $output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment