/** * * Initialises a pre-built cache (via {@link CacheableNavigation_Rebuild}) * used by front-end calling logic e.g. via $CachedData blocks in .ss templates * unless build_on_reload is set to false in YML config. * * Called using SilverStripe's extend() method in {@link ContentController}. * * @param Controller $controller * @return void * @see {@link CacheableNavigation_Rebuild}. * @see {@link CacheableNavigation_Clean}. * @todo add queuedjob chunking ala BuildTask to this csche-rebuild logic. * At the moment we attempt to skip it if build_cache_onload is set to false * in YML Config */ public function contentControllerInit($controller) { // Skip if flushing or the project instructs us to do so $skip = self::is_flush($controller) || !self::build_cache_onload(); $service = new CacheableNavigationService(); $currentStage = Versioned::current_stage(); $stage_mode_mapping = array("Stage" => "stage", "Live" => "live"); $service->set_mode($stage_mode_mapping[$currentStage]); $siteConfig = SiteConfig::current_site_config(); if (!$siteConfig->exists()) { $siteConfig = $this->owner->getSiteConfig(); } $service->set_config($siteConfig); if ($_cached_navigation = $service->getCacheableFrontEnd()->load($service->getIdentifier())) { if (!$skip && !$_cached_navigation->get_completed()) { $service->refreshCachedConfig(); if (class_exists('Subsite')) { $pages = DataObject::get("Page", "\"SubsiteID\" = '" . $siteConfig->SubsiteID . "'"); } else { $pages = DataObject::get("Page"); } if ($pages->exists()) { foreach ($pages as $page) { $service->set_model($page); $service->refreshCachedPage(true); } } $service->completeBuild(); $_cached_navigation = $service->getCacheableFrontEnd()->load($service->getIdentifier()); } Config::inst()->update('Cacheable', '_cached_navigation', $_cached_navigation); } }
/** * */ public function testCompleteBuild() { $model = $this->objFromFixture('SiteTree', 'servicetest-page-1'); $service = new CacheableNavigationService('Live', null, $model); // Cache should be empty $cachable = $service->getCacheableFrontEnd()->load($service->getIdentifier()); $this->assertFalse($cachable->get_completed()); // Populate the cache with a page $service->refreshCachedPage(); // Set it to complete and re-test $service->completeBuild(); $cachable = $service->getCacheableFrontEnd()->load($service->getIdentifier()); $this->assertTrue($cachable->get_completed()); }