Implement cache for reseller data exports

This commit is contained in:
Björn Fromme
2019-09-11 12:35:19 +02:00
parent 42b2444a2c
commit d014f7c161
2 changed files with 57 additions and 23 deletions
@@ -35,6 +35,8 @@ use EP\EpProducts\Service\ResellerDataService;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException;
@@ -61,13 +63,23 @@ class ResellerController extends ActionController
*/
protected $resellerDataService;
/**
* @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
*/
protected $cache;
/**
* @param ResellerDataService $resellerDataService
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
*/
public function __construct(ResellerDataService $resellerDataService)
{
parent::__construct();
$this->resellerDataService = $resellerDataService;
/** @var CacheManager $cacheManager */
$cacheManager = GeneralUtility::makeInstance(CacheManager::class);
$this->cache = $cacheManager->getCache('ep_products_reseller');
}
/**
@@ -103,7 +115,11 @@ class ResellerController extends ActionController
);
}
$productData = $this->resellerDataService->preprocessSingle($product, $reseller);
$cacheKey = sprintf('reseller_export_cp_%d_%d', $product->getUid(), $reseller->getUid());
if (($productData = $this->cache->get($cacheKey)) === false) {
$productData = $this->resellerDataService->preprocessSingle($product, $reseller);
$this->cache->set($cacheKey, $productData, [], 3600);
}
$this->view->assign('reseller', $reseller);
$this->view->assign('productData', $productData);
@@ -111,16 +127,25 @@ class ResellerController extends ActionController
/**
* @param Reseller $reseller
* @return bool
* @return void
* @throws \Doctrine\DBAL\DBALException
*/
public function xmlAction(Reseller $reseller)
{
$data = $this->resellerDataService->preprocessAll($reseller);
$cacheKey = sprintf('reseller_export_xml_full_%d', $reseller->getUid());
$encoders = [new XmlEncoder('products')];
$normalizers = [new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);
if (($xml = $this->cache->get($cacheKey)) === false) {
$data = $this->resellerDataService->preprocessAll($reseller);
$encoders = [new XmlEncoder('products')];
$normalizers = [new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);
$xml = $serializer->serialize(
$data,
'xml',
[ 'xml_encoding' => 'utf-8', 'xml_format_output' => true ]
);
$this->cache->set($cacheKey, $xml, [], 3600);
}
$filename = sprintf('export_%s_%s.xml', $reseller->getCode(), date('Y-m-d'));
@@ -131,11 +156,7 @@ class ResellerController extends ActionController
header('Expires: 0');
header('Pragma: public');
echo $serializer->serialize(
$data,
'xml',
[ 'xml_encoding' => 'utf-8', 'xml_format_output' => true ]
);
echo $xml;
exit();
}
@@ -152,11 +173,21 @@ class ResellerController extends ActionController
throw new InvalidArgumentValueException();
}
$data = $this->resellerDataService->preprocessAll(
$reseller,
static::$imageDimensions[$type],
$type
);
$cacheKey = sprintf('reseller_export_csv_full_%d', $reseller->getUid());
if (($csv = $this->cache->get($cacheKey)) === false) {
$data = $this->resellerDataService->preprocessAll(
$reseller,
static::$imageDimensions[$type],
$type
);
if ($type === 'google') {
$csv = GoogleBusinessFeedCsvFormatter::format($data);
} else {
$csv = FacebookCatalogCsvFormatter::format($data);
}
$this->cache->set($cacheKey, $csv, [], 3600);
}
$filename = sprintf('export_%s_%s.csv', $reseller->getCode(), date('Y-m-d'));
@@ -169,12 +200,6 @@ class ResellerController extends ActionController
$fileHandle = fopen('php://output', 'wb');
if ($type === 'google') {
$csv = GoogleBusinessFeedCsvFormatter::format($data);
} else {
$csv = FacebookCatalogCsvFormatter::format($data);
}
foreach ($csv as $row) {
fputcsv($fileHandle, $row);
}
@@ -1,4 +1,5 @@
<?php
if (!defined('TYPO3_MODE')) {
die('Access denied.');
}
@@ -369,3 +370,11 @@ $GLOBALS['TYPO3_CONF_VARS']['LOG']['EP']['EpProducts']['Controller']['writerConf
];
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['teamSlug'] = \EP\EpProducts\Updates\TeamSlugUpdater::class;
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['ep_products_reseller'])) {
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['ep_products_reseller'] = [];
}
if (!isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['ep_products_reseller']['backend'])) {
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['ep_products_reseller']['backend'] = \TYPO3\CMS\Core\Cache\Backend\SimpleFileBackend::class;
}