Prado\Caching\TDbCache::initializeCache PHP Method

initializeCache() protected method

If {@link setAutoCreateCacheTable AutoCreateCacheTableName} is 'true' check existence of cache table and create table if does not exist.
Since: 3.1.5
protected initializeCache ( $force = false ) : void
return void
    protected function initializeCache($force = false)
    {
        if ($this->_cacheInitialized && !$force) {
            return;
        }
        $db = $this->getDbConnection();
        try {
            $key = 'TDbCache:' . $this->_cacheTable . ':created';
            if ($force) {
                $this->_createCheck = false;
            } else {
                $this->_createCheck = $this->getApplication()->getGlobalState($key, 0);
            }
            if ($this->_autoCreate && !$this->_createCheck) {
                Prado::trace(($force ? 'Force initializing: ' : 'Initializing: ') . $this->id . ', ' . $this->_cacheTable, '\\Prado\\Caching\\TDbCache');
                $sql = 'SELECT 1 FROM ' . $this->_cacheTable . ' WHERE 0=1';
                $db->createCommand($sql)->queryScalar();
                $this->_createCheck = true;
                $this->getApplication()->setGlobalState($key, time());
            }
        } catch (\Exception $e) {
            // DB table not exists
            if ($this->_autoCreate) {
                Prado::trace('Autocreate: ' . $this->_cacheTable, '\\Prado\\Caching\\TDbCache');
                $driver = $db->getDriverName();
                if ($driver === 'mysql') {
                    $blob = 'LONGBLOB';
                } else {
                    if ($driver === 'pgsql') {
                        $blob = 'BYTEA';
                    } else {
                        $blob = 'BLOB';
                    }
                }
                $sql = 'CREATE TABLE ' . $this->_cacheTable . " (itemkey CHAR(128) PRIMARY KEY, value {$blob}, expire INTEGER)";
                $db->createCommand($sql)->execute();
                $sql = 'CREATE INDEX IX_expire ON ' . $this->_cacheTable . ' (expire)';
                $db->createCommand($sql)->execute();
                $this->_createCheck = true;
                $this->getApplication()->setGlobalState($key, time());
            } else {
                throw new TConfigurationException('db_cachetable_inexistent', $this->_cacheTable);
            }
        }
        $this->_cacheInitialized = true;
    }