Rebuild the system with metadata, database and cache clearing

@return bool
166436
public function rebuild($entityList = null)
    {
        $this->populateConfigParameters();

        $result = $this->clearCache();

        $result &= $this->rebuildMetadata();

        $result &= $this->rebuildDatabase($entityList);

        $this->rebuildScheduledJobs();

        return $result;
    }
743767
protected static function bootRememberable()
    {
        self::created(function ($model) {
            $model->cachePut();
        });

        self::updated(function ($model) {
            $model->cachePut();
        });

        self::deleted(function ($model) {
            $model->cacheForget();
        });
    }
997429
public function updateCache() {
		parent::updateCache();
		$bcache = \Brilliant\BFactory::getCache();
		if (empty($bcache)) {
			return false;
			}
		$parentid = $this->{$this->parentKeyName};
		if (empty($parentid)) {
			return true;
			}
		$cachekey = $this->tableName . ':itemid:' . $parentid;
		$bcache->delete($cachekey);
		//Update some lists
		$params = array();
		$params['group'] = $this->getGroupId();
		$params['parentisnull'] = true;
		$collname = $this->collectionName;
		$bitems = $collname::getInstance();
		$cachekey = $bitems->itemsFilterHash($params);
		$bcache->delete($cachekey);
		//All is ok
		return true;
	}
489786
private function bootCacher()
    {
        /** @var Cacher $cacher */
        $cacher = Cacher::getInstance();

        // Default cacher if FileCache, so we do not check it
        if (MemcachedCache::itWorks()) { // Distributed cache new version
            $cacher->setDefaultCacher('MemcachedCache');
        } elseif (MemcacheCache::itWorks()) { // Distributed cache old version
            $cacher->setDefaultCacher('MemcacheCache');
        } elseif (RedisCache::itWorks()) { // No-sql database
            $cacher->setDefaultCacher('RedisCache');
        }
    }
1146091
private function reindexResources() {
        if (count($this->resToReindex) == 0) {
            return;
        }

        $this->begin();
        foreach (array_unique($this->resToReindex) as $i) {
            try {
                $res = $this->getResourceByUri($i);
                $res->setMetadata($res->getMetadata());
                $res->updateMetadata();
            } catch (Deleted $e) {
                
            } catch (NotFound $e) {
                
            }
        }
        $this->resToReindex = [];
        $this->commit();
    }
332047
public function clean()
    {
        foreach ($this->cache as $key => $value) {
            list($expires, $value) = $value;
            if ($expires && ($expires < microtime(true))) {
                unset($this->cache[$key]);
            }
        }
        return true;
    }
101560
public function refresh()
    {
        /* @var $cache CacheInterface */
        $cache = is_string($this->db->schemaCache) ? Zilf::$app->get($this->db->schemaCache, false) : $this->db->schemaCache;
        if ($this->db->enableSchemaCache && $cache instanceof CacheInterface) {
            TagDependency::invalidate($cache, $this->getCacheTag());
        }
        $this->_tableNames = [];
        $this->_tableMetadata = [];
    }
1273358
private function rebuildPathStore()
    {
        $this->pathStore = $this->pathNodes($this->keyStore);

        $this->replaceTokens($this->pathStore);
    }
1281629
public function rebuildCache()
    {
        $themes = $this->scanJsonFiles();
        // file_put_contents($this->cachePath, json_encode($themes, JSON_PRETTY_PRINT));

        $stub = file_get_contents(__DIR__ . '/stubs/cache.stub');
        $contents = str_replace('[CACHE]', var_export($themes, true), $stub);
        file_put_contents($this->cachePath, $contents);
    }
32520
protected function canBuild()
    {
        $rebuild = false;

        if ($this->isBuilt()) {
            $rebuild = $this->askConfirmQuestion(
                'Project has already been built, do you want to rebuild?',
                false
            );

            if (!$rebuild) {
                return static::BUILD_ABORT;
            }
        }

        return !$rebuild ? static::BUILD_FRESH : static::BUILD_DIRTY;
    }
96969
public function rebuild()
	{
		JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		$extension = $this->input->get('extension');
		$this->setRedirect(JRoute::_('index.php?option=com_categories&view=categories&extension=' . $extension, false));

		/** @var CategoriesModelCategory $model */
		$model = $this->getModel();

		if ($model->rebuild())
		{
			// Rebuild succeeded.
			$this->setMessage(JText::_('COM_CATEGORIES_REBUILD_SUCCESS'));

			return true;
		}

		// Rebuild failed.
		$this->setMessage(JText::_('COM_CATEGORIES_REBUILD_FAILURE'));

		return false;
	}
765097
public function clear()
    {
        try {
            $this->_repository
                    ->createQueryBuilder('c')
                    ->delete()
                    ->where('1 = 1')
                    ->getQuery()
                    ->execute();
            $this->resetCacheEntity();
        } catch (\Exception $e) {
            $this->log('warning', sprintf('Enable to clear cache : %s', $e->getMessage()));

            return false;
        }

        return true;
    }
232004
public function saveCacheListing() : bool
    {
        if (md5(json_encode($this->entryMetaData)) === $this->md5Sum) {
            return false;
        }

        return apcu_store(self::KEY_CACHE_LIST, $this->entryMetaData);
    }
1268914
protected function __reconstruct()
    {
        // Modules default app
        if (null !== ($defaultApp = config('app'))) {
            if (false !== ($defaultModule = modules()->getApp($defaultApp))) {
                // Register Domain App Module Namespace
                loader()->addNamespace($defaultModule->getNamespace(), $defaultModule->getRealPath());

                // Push Domain App Module
                modules()->push($defaultModule);
            } elseif (false !== ($defaultModule = modules()->getModule($defaultApp))) {
                // Register Path Module Namespace
                loader()->addNamespace($defaultModule->getNamespace(), $defaultModule->getRealPath());

                // Push Path Module
                modules()->push($defaultModule);
            }
        }

        if (profiler() !== false) {
            profiler()->watch('Calling Hooks Service: Post System');
        }
        hooks()->callEvent(Framework\Services\Hooks::POST_SYSTEM);

        if (is_cli()) {
            $this->cliHandler();
        } else {
            $this->httpHandler();
        }
    }
797338
public static function reset_database() {
        global $DB;

        if (!is_null(self::$lastdbwrites) and self::$lastdbwrites == $DB->perf_get_writes()) {
            return false;
        }

        if (!parent::reset_database()) {
            return false;
        }

        self::$lastdbwrites = $DB->perf_get_writes();

        return true;
    }
1086196
public function rebuildtree(){
		//Rebuild categories ads trigger count.

		//Rebuild nested set - get cat
		BLog::addToLog('[Items] rebuilding nested sets...');
		//
		$bcache=\Brilliant\BFactory::getCache();
		if($bcache){
			$bcache->invalidate();
			}
		//
		$catslist=$this->itemsFilter(array());
		BLog::addToLog('[Items] Total categories count:'.count($catslist).'...');

		$rootcats=array();
		//
		foreach($catslist as $cat){
			$cat->level=0;
			$cat->lft=0;
			$cat->rgt=0;
			if(empty($cat->{$this->parentKeyName})){
				$rootcats[]=$cat;
				}
			}
		//Sort root categories.
		$n=count($rootcats);
		BLog::addToLog('[Items] Root categories count:'.$n.'...');
		for($i=0; $i<$n; $i++){
			$m=$i;
			for($j=$i+1; $j<$n; $j++){
				if($rootcats[$j]->ordering < $rootcats[$m]->ordering){
					$m=$j;
					}
				}
			if($m!=$i){
				$t=$rootcats[$i];
				$rootcats[$i]=$rootcats[$m];
				$rootcats[$m]=$t;
				}
			}

		//Foreach by root categories...
		foreach($rootcats as $rcat){
			BLog::addToLog('[Items] Processing root category ['.$rcat->id.']');

			$rcat->level=1;
			$lft=1; $rgt=2;
			$this->rebuildtree_recursive($rcat,$lft,$rgt);
			$rcat->lft=1;
			$rcat->rgt=$rgt;
			}
		$db=\Brilliant\BFactory::getDBO();
		if(empty($db)){
			return false;
			}
		BLog::addToLog('[Items] Updating nested set...');
		foreach($catslist as $ct){
			$qr='UPDATE `'.$this->tableName.'` set `'.$this->leftKeyName.'`='.$ct->lft.', `'.$this->rightKeyName.'`='.$ct->rgt.', `'.$this->levelKeyName.'`='.$ct->level.' WHERE `'.$this->primaryKeyName.'`='.$ct->id;
			$q=$db->query($qr);
			if(empty($q)){
				return false;
				}
			}
		//Invalidate all cache.
		$bcache=\Brilliant\BFactory::getCache();
		if($bcache){
			$bcache->invalidate();
			}
		return true;
		}
814762
public function engineRebuild()
    {
        $this->executeCommandHook(__FUNCTION__, 'before');
        $this->executeExistingCommand('env:rebuild');
        $this->executeCommandHook(__FUNCTION__, 'after');

        return $this;
    }