TYPO3 Extbase custom extension cache

ext_tables.php


#
# TABLE STRUCTURE FOR TABLE 'cf_extkey_cache'
#
CREATE TABLE cf_extkey_cache (
    id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    identifier VARCHAR(250) DEFAULT '' NOT NULL,
    crdate INT(11) UNSIGNED DEFAULT '0' NOT NULL,
    content mediumblob,
    expires INT(11) UNSIGNED DEFAULT '0' NOT NULL,
    PRIMARY KEY (id),
    KEY cache_id (identifier)
) ENGINE=InnoDB;
 
#
# TABLE STRUCTURE FOR TABLE 'cf_extkey_cache_tags'
#
CREATE TABLE cf_extkey_cache_tags (
    id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    identifier VARCHAR(250) DEFAULT '' NOT NULL,
    tag VARCHAR(250) DEFAULT '' NOT NULL,
    PRIMARY KEY (id),
    KEY cache_id (identifier),
    KEY cache_tag (tag)
) ENGINE=InnoDB;




ext_localconf.php



<?php
if (!defined('TYPO3_MODE')) {
    die ('Access denied.');
}
 
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
    'VENDOR.' . $_EXTKEY,
    'Pi1',
    array(
        'Display' => 'show'
    ),
    // non-cacheable actions
    array(
        'Display' => 'show'
    )
);
 
/**
 * register cache for extension
 */
if (!is_array($TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations']['extkey_cache'])) {
  $TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations']['extkey_cache'] = array();
  $TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations']['extkey_cache']['frontend'] = 'TYPO3\\CMS\\Core\\Cache\\Frontend\\VariableFrontend';
  $TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations']['extkey_cache']['backend'] = 'TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend';
  $TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations']['extkey_cache']['options']['compression'] = 1;
}
?>



Classes/Controller/DisplayController.php




<?php
namespace VENDOR\Extkey\Controller;
 
class DisplayController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
 
  /**
   * itemRepository
   *
   * @var \VENDOR\Extkey\Domain\Repository\ItemRepository
   * @inject
   */
  protected $itemRepository;
 
  /**
   * action show
   *
   * @return void
   */
  public function showAction() {
    /*
     * get/set some vars
     */
    $args = $this->request->getArguments();
    $itemUid = intval($args['item']);
 
    /*
     * Try to get the cached content.
     * If there is content in cache, just return it.
     * Otherwise generate the content, save in cache and then return it.
     */
    // calculate the identifier for the cached entry
    $cacheIdentifier = $this->calculateCacheIdentifier(array(
      $GLOBALS["TSFE"]->sys_language_uid, // cache depends on the language
      $GLOBALS["TSFE"]->fe_user->groupData['uid'], // cache depends on the userGroup
      $itemUid, // cache depends on the object to show
    ));
 
    // instantiate the cache
    $cache = $GLOBALS['typo3CacheManager']->getCache('extkey_cache');
 
    // try to find the cached content
    if (($content = $cache->get($cacheIdentifier)) === false) { // the cached content is NOT available
      // generate the content
      $content = $this->getShowActionContent($itemUid);
 
      // save content in cache
      $cache->set($cacheIdentifier, $content, array(
        "showAction_" . $itemUid
      ));
    }
    return $content;
  }
   
  /**
   * Method to get the content for the showAction
   * @param \integer $itemUid
   * @return \string
   */
  public function getShowActionContent($itemUid) {
    /*
     * find the item, set it to view and return the rendered content
     */
    $item = $this->itemRepository->getByUid($itemUid);
    $this->view->assign('item', $item);
    return $this->view->render();
  }
 
  /**
   * Calculates the cache identifier
   * @param \array $arr
   * @return \string
   */
  public static function calculateCacheIdentifier($arr) {
    return sha1(json_encode($arr)); // in average json_encode is four times faster than serialize()
  }
}
?>