Skip to content

Commit

Permalink
Stop tracking entry queries for “single” sections
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Feb 12, 2024
1 parent 8781d9c commit a5cde6a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 202 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
### Changed

- The Cache Folder Path column now displays aliases in the cached file table in the File Storage utility.
- Entry queries for “single” sections are no longer tracked.
- Improved error handling in a previous migration.
- Made minor optimisations to the file storage driver.
- Expired URIs are now deleted from the database immediately when refreshing expired cache.
Expand Down
30 changes: 30 additions & 0 deletions src/helpers/ElementQueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

namespace putyourlightson\blitz\helpers;

use Craft;
use craft\base\ElementInterface;
use craft\behaviors\CustomFieldBehavior;
use craft\elements\db\ElementQuery;
use craft\elements\db\ElementQueryInterface;
use craft\elements\db\EntryQuery;
use craft\fields\data\MultiOptionsFieldData;
use craft\fields\data\OptionData;
use craft\helpers\ArrayHelper;
use craft\models\Section;
use DateTime;
use ReflectionClass;
use ReflectionProperty;
Expand Down Expand Up @@ -321,6 +324,33 @@ public static function isDraftOrRevisionQuery(ElementQuery $elementQuery): bool
return false;
}

/**
* Returns whether the element query is an entry query for “single” sections.
*/
public static function isEntryQueryForSingleSections(ElementQuery $elementQuery): bool
{
if (!($elementQuery instanceof EntryQuery)) {
return false;
}

$sectionIds = $elementQuery->sectionId;

if (!is_array($sectionIds)) {
$sectionIds = [$sectionIds];
}

$singles = Craft::$app->getSections()->getSectionsByType(Section::TYPE_SINGLE);
$singlesIds = ArrayHelper::getColumn($singles, 'id');

foreach ($sectionIds as $sectionId) {
if (!is_numeric($sectionId) || !in_array($sectionId, $singlesIds)) {
return false;
}
}

return true;
}

/**
* Returns whether the element query is a relation field query.
* For example:
Expand Down
5 changes: 5 additions & 0 deletions src/services/GenerateCacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ public function addElementQuery(ElementQuery $elementQuery): void
return;
}

// Don’t proceed if this is an entry query for “single” sections
if (ElementQueryHelper::isEntryQueryForSingleSections($elementQuery)) {
return;
}

// Don’t proceed if this is a relation field query
if (ElementQueryHelper::isRelationFieldQuery($elementQuery)) {
$this->_addRelatedElementIds($elementQuery);
Expand Down
Loading

0 comments on commit a5cde6a

Please sign in to comment.