Skip to content

Commit

Permalink
removed db cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Wagner committed Jun 20, 2018
2 parents 5226786 + ee47336 commit 97eae62
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 104 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

[![](https://img.shields.io/packagist/v/heimrichhannot/contao-list-bundle.svg)](https://packagist.org/packages/heimrichhannot/contao-list-bundle)
[![](https://img.shields.io/packagist/dt/heimrichhannot/contao-list-bundle.svg)](https://packagist.org/packages/heimrichhannot/contao-list-bundle)
[![](https://img.shields.io/travis/heimrichhannot/contao-list-bundle/master.svg)](https://travis-ci.org/heimrichhannot/contao-list-bundle/)
[![](https://img.shields.io/coveralls/heimrichhannot/contao-list-bundle/master.svg)](https://coveralls.io/github/heimrichhannot/contao-list-bundle)
[![Build Status](https://travis-ci.org/heimrichhannot/contao-list-bundle.svg?branch=master)](https://travis-ci.org/heimrichhannot/contao-list-bundle)
[![Coverage Status](https://coveralls.io/repos/github/heimrichhannot/contao-list-bundle/badge.svg?branch=master)](https://coveralls.io/github/heimrichhannot/contao-list-bundle?branch=master)

This bundle offers a generic list module to use with arbitrary contao entities containing standard list handling like pagination, sorting, and filtering.

Expand Down
8 changes: 6 additions & 2 deletions src/Backend/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@
use Contao\DataContainer;
use Contao\ModuleModel;
use Contao\System;
use HeimrichHannot\ListBundle\Module\ModuleList;

class Module
{
/**
* @deprecated use ModuleList::TYPE instead
*/
const MODULE_LIST = 'huhlist';

public function getAllListModules()
{
$listModules = [];
/** @var \Contao\ModuleModel $adapter */
$modules = \Contao\ModuleModel::findBy('type', static::MODULE_LIST);
/** @var ModuleModel $adapter */
$modules = ModuleModel::findBy('type', ModuleList::TYPE);

if (null === $modules) {
return $listModules;
Expand Down
8 changes: 3 additions & 5 deletions src/Item/DefaultItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,11 @@ public function parse(string $cssClass = '', int $count = 0): string

$twig = $this->_manager->getTwig();

$templateName = $listConfig->itemTemplate;
$templateData = $this->jsonSerialize();

$event = $this->_dispatcher->dispatch(ListBeforeRenderItemEvent::NAME, new ListBeforeRenderItemEvent($templateName, $templateData, $this));
$event = $this->_dispatcher->dispatch(ListBeforeRenderItemEvent::NAME, new ListBeforeRenderItemEvent($listConfig->itemTemplate, $this->jsonSerialize(), $this));
$templateName = $this->_manager->getItemTemplateByName($event->getTemplateName() ?: 'default');

return $twig->render(
$this->_manager->getItemTemplateByName($event->getTemplateName() ?: 'default'),
$templateName,
$event->getTemplateData()
);
}
Expand Down
77 changes: 16 additions & 61 deletions src/Lists/DefaultList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace HeimrichHannot\ListBundle\Lists;

use Contao\Config;
use Contao\Controller;
use Contao\Database;
use Contao\FrontendTemplate;
use Contao\StringUtil;
Expand Down Expand Up @@ -158,8 +159,7 @@ public function parse(string $listTemplate = null, string $itemTemplate = null,
// initial results
$this->setShowInitialResults($listConfig->showInitialResults);
if ($isSubmitted || $listConfig->showInitialResults) {
$queryBuilder->select($fields);
$totalCount = $this->getTotalCountByQuery($queryBuilder);
$totalCount = $queryBuilder->select($fields)->execute()->rowCount();
}

// item count text
Expand All @@ -177,7 +177,20 @@ public function parse(string $listTemplate = null, string $itemTemplate = null,
$this->_dispatcher->dispatch(ListModifyQueryBuilderEvent::NAME, new ListModifyQueryBuilderEvent($queryBuilder, $this, $listConfig));

if ($isSubmitted || $listConfig->showInitialResults) {
$items = $this->getItemsByQuery($queryBuilder, $fields, $filter);
$items = $queryBuilder->execute()->fetchAll();

// add fields without sql key in DCA (could have a value by load_callback)
Controller::loadDataContainer($filter->dataContainer);

foreach ($items as &$item) {
$itemFields = array_keys($item);

foreach (array_keys($GLOBALS['TL_DCA'][$filter->dataContainer]['fields']) as $field) {
if (!in_array($field, $itemFields, true)) {
$item[$field] = null;
}
}
}

$this->setItems($this->parseItems($items, $itemTemplate));
}
Expand Down Expand Up @@ -822,62 +835,4 @@ public function getPage(): int
{
return $this->_page;
}

/**
* @param FilterQueryBuilder $queryBuilder
*
* @return int|mixed
*/
protected function getTotalCountByQuery(FilterQueryBuilder $queryBuilder)
{
if (!Config::get('activateDbCache') || System::getContainer()->get('kernel')->isDebug()) {
return $queryBuilder->execute()->rowCount();
}

$utilsCacheDb = System::getContainer()->get('huh.utils.cache.database');
$utilsUrl = System::getContainer()->get('huh.utils.url');

if ($utilsCacheDb->keyExists($utilsUrl->getCurrentUrl(['skipParams' => true]).'_count')) {
$totalCount = $utilsCacheDb->getValue($utilsUrl->getCurrentUrl(['skipParams' => true]).'_count');
} else {
$totalCount = $queryBuilder->execute()->rowCount();
$utilsCacheDb->cacheValue($utilsUrl->getCurrentUrl(['skipParams' => true]).'_count', $totalCount);
}

return $totalCount;
}

/**
* @param FilterQueryBuilder $queryBuilder
* @param $fields
* @param $filter
*
* @return array
*/
protected function getItemsByQuery(FilterQueryBuilder $queryBuilder, $fields, $filter)
{
if (!Config::get('activateDbCache') || System::getContainer()->get('kernel')->isDebug()) {
return $queryBuilder->execute()->fetchAll();
}

$utilsCacheDb = System::getContainer()->get('huh.utils.cache.database');
$utilsUrl = System::getContainer()->get('huh.utils.url');
$orderBy = $queryBuilder->getQueryPart('orderBy');

if ($utilsCacheDb->keyExists($utilsUrl->getCurrentUrl([]).str_replace(' ', '', $orderBy[0]))) {
$ids = $utilsCacheDb->getValue($utilsUrl->getCurrentUrl([]).str_replace(' ', '', $orderBy[0]));
$ids = implode(',', StringUtil::deserialize($ids, true));
$items = System::getContainer()->get('contao.framework')->createInstance(Database::class)->execute("SELECT $fields FROM $filter->dataContainer WHERE $filter->dataContainer.id IN ($ids) ORDER BY $orderBy[0]")->fetchAllAssoc();
} else {
$items = $queryBuilder->execute()->fetchAll();
$queryBuilder->select('id');
$ids = $queryBuilder->execute()->fetchAll();
foreach ($ids as $i => $id) {
$ids[$i] = $id['id'];
}
$utilsCacheDb->cacheValue($utilsUrl->getCurrentUrl([]).str_replace(' ', '', $orderBy[0]), $ids);
}

return $items;
}
}
19 changes: 18 additions & 1 deletion src/Model/ListConfigElementModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,24 @@

namespace HeimrichHannot\ListBundle\Model;

class ListConfigElementModel extends \Model
use Contao\Model;

/**
* Class ListConfigElementModel.
*
* @property int $id
* @property int $pid
* @property int $tstamp
* @property int $dateAdded
* @property string $title
* @property string $type
* @property string $imageSelectorField
* @property string $imgSize
* @property string $placeholderImage
* @property string $placeholderImageFemale
* @property string $genderField
*/
class ListConfigElementModel extends Model
{
protected static $strTable = 'tl_list_config_element';
}
5 changes: 4 additions & 1 deletion src/Model/ListConfigModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace HeimrichHannot\ListBundle\Model;

use Contao\Model;

/**
* @property int $id
* @property int $tstamp
Expand All @@ -16,6 +18,7 @@
* @property int $numberOfItems
* @property int $perPage
* @property int $skipFirst
* @property bool $doNotRenderEmpty
* @property bool $showItemCount
* @property string $itemCountText
* @property bool $showNoItemsText
Expand Down Expand Up @@ -51,7 +54,7 @@
* @property string $manager
* @property string $item
*/
class ListConfigModel extends \Model
class ListConfigModel extends Model
{
protected static $strTable = 'tl_list_config';
}
24 changes: 21 additions & 3 deletions src/Module/ModuleList.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Contao\System;
use HeimrichHannot\FilterBundle\Config\FilterConfig;
use HeimrichHannot\FilterBundle\Manager\FilterManager;
use HeimrichHannot\FilterBundle\QueryBuilder\FilterQueryBuilder;
use HeimrichHannot\ListBundle\Event\ListCompileEvent;
use HeimrichHannot\ListBundle\Lists\ListInterface;
use HeimrichHannot\ListBundle\Manager\ListManagerInterface;
Expand All @@ -25,6 +26,8 @@

class ModuleList extends Module
{
const TYPE = 'huhlist';

protected $strTemplate = 'mod_list';

/**
Expand Down Expand Up @@ -135,6 +138,15 @@ public function generate()

$this->manager->setList(new $listClass($this->manager));
}
if (true === (bool) $this->manager->getListConfig()->doNotRenderEmpty && empty($this->manager->getList()->getItems())) {
/** @var FilterQueryBuilder $queryBuilder */
$queryBuilder = $this->manager->getFilterManager()->getQueryBuilder($this->filter->id);
$fields = $this->filter->dataContainer.'.* ';

if ($totalCount = $queryBuilder->select($fields)->execute()->rowCount() < 1) {
return '';
}
}

return parent::generate();
}
Expand All @@ -146,9 +158,7 @@ public function getListConfig(int $listConfigId): ?ListConfigModel
}

// compute list config respecting the inheritance hierarchy
$listConfig = $this->listConfigRegistry->computeListConfig(
$listConfigId
);
$listConfig = $this->listConfigRegistry->computeListConfig($listConfigId);

return $listConfig;
}
Expand All @@ -161,6 +171,14 @@ public function getFilterConfig(): FilterConfig
return $this->filterConfig;
}

/**
* @return ListManagerInterface
*/
public function getManager(): ListManagerInterface
{
return $this->manager;
}

protected function compile()
{
$this->manager->getList()->handleShare();
Expand Down
7 changes: 6 additions & 1 deletion src/Registry/ListConfigElementRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ public function findBy($column, $value, array $options = [])
* @param mixed $value
* @param array $options
*
* @return \Contao\Model\Collection|ListConfigElementModel|null
* @return ListConfigElementModel|null
*/
public function findOneBy($column, $value, array $options = [])
{
$options = array_merge(
['limit' => 1, 'return' => 'Model'],
$options
);

return System::getContainer()->get('huh.utils.model')->findModelInstancesBy(
'tl_list_config_element', $column, $value, $options);
}
Expand Down
Loading

0 comments on commit 97eae62

Please sign in to comment.