1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 16:26:59 +02:00

Add support for runtime page cache groups. This enables pages to be cached as a group, or more importantly, uncached as a group. It was added primarily to add efficiency to $pages->findMany(), so that it can cache supporting pages (like parents of pages returned by findMany). Previously, it would have to load a fresh copy of each supporting page used by findMany(), for every returned page, since findMany() used no in-memory caching, otherwise you could run out of memory on large results. So if you iterated a $pages->findMany() result and output the URL of each page (which loads parents), then it would have to reload all those parents for each iteration. Now it can cache them for each chunk of 250 pages, offering a significant potential performance improvement in many cases.

This commit is contained in:
Ryan Cramer
2022-04-14 08:11:19 -04:00
parent f05943baaa
commit 047ffb1c20
4 changed files with 108 additions and 23 deletions

View File

@@ -74,6 +74,12 @@ class PageArrayIterator extends Wire implements \Iterator {
*
*/
protected $chunkSize = 250;
/**
* @var string
*
*/
protected $cacheGroup = '';
/**
* Construct
@@ -92,24 +98,26 @@ class PageArrayIterator extends Wire implements \Iterator {
*
*/
protected function loadChunk() {
$this->chunkSize = (int) $this->wire('config')->lazyPageChunkSize;
$this->chunkSize = (int) $this->wire()->config->lazyPageChunkSize;
$this->pagesPosition = 0;
$start = $this->currentChunk++ * $this->chunkSize;
$pages = $this->wire()->pages;
if($this->cacheGroup) {
$this->wire()->pages->cacher()->uncacheGroup($this->cacheGroup);
}
// If the starting position exceeds the amount of placeholder objects, we just issue an empty
// PageArray, which causes the loop to stop (because valid() will return false)
if(!isset($this->lazypages[$start])) {
$this->pages = $this->wire('pages')->newPageArray();
$this->pages = $pages->newPageArray();
} else {
// Check if the user gave options for the loading
$options = isset($this->options['loadOptions']) ? $this->options['loadOptions'] : array();
// Always disable the cache
$options['cache'] = false;
// Here we retrieve a chunk of Page objects and loop over them to retrieve the IDs of the Pages.
$lazypages = array_slice($this->lazypages, $start, $this->chunkSize);
$ids = array();
@@ -120,8 +128,10 @@ class PageArrayIterator extends Wire implements \Iterator {
// of real Page-objects from Pages::getById()
$ids[] = $page->id;
}
$this->cacheGroup = 'lazy' . md5(implode(',', $ids));
$options['cache'] = $this->cacheGroup;
$pages = $this->wire('pages');
$debug = $pages->debug();
if($debug) $pages->debug(false);
$this->pages = $pages->getById($ids, $options);