Exemplo n.º 1
0
 public function queryStart($queryText, $queryType = null)
 {
     if (!$this->_enabled) {
         return null;
     }
     $this->_count++;
     Kwf_Benchmark::countLog('dbqueries');
     return null;
 }
Exemplo n.º 2
0
 public function renderMaster($component)
 {
     static $benchmarkEnabled;
     if (!isset($benchmarkEnabled)) {
         $benchmarkEnabled = Kwf_Benchmark::isEnabled();
     }
     $content = false;
     if ($this->_enableCache) {
         $content = Kwf_Component_Cache::getInstance()->load($component->componentId, $this->_getRendererName(), 'fullPage');
         $this->_minLifetime = null;
     }
     Kwf_Benchmark::checkpoint('load fullPage cache');
     $statType = null;
     if (!$content) {
         if ($benchmarkEnabled) {
             $startTime = microtime(true);
         }
         if (!$this->_enableCache || ($content = Kwf_Component_Cache::getInstance()->load($component, $this->_getRendererName(), 'page')) === null) {
             $masterHelper = new Kwf_Component_View_Helper_Master();
             $masterHelper->setRenderer($this);
             $content = $masterHelper->master($component);
             if ($this->_enableCache) {
                 Kwf_Component_Cache::getInstance()->save($component, $content, $this->_getRendererName(), 'page', '', '', null);
                 $statType = 'miss';
             } else {
                 $statType = 'noviewcache';
             }
         } else {
             $statType = 'hit';
         }
         if ($statType) {
             Kwf_Benchmark::count("rendered {$statType}", $component->componentId . ': page');
         }
         Kwf_Benchmark::countLog('render-' . $statType);
         if ($benchmarkEnabled) {
             Kwf_Benchmark::subCheckpoint($component->componentId . ' page', microtime(true) - $startTime);
         }
         Kwf_Benchmark::checkpoint('render page');
         $pass1Cacheable = true;
         $content = $this->_renderPass1($content, $pass1Cacheable);
         Kwf_Benchmark::checkpoint('render pass 1');
         if ($this->_enableCache && $pass1Cacheable) {
             Kwf_Component_Cache::getInstance()->save($component, $content, $this->_getRendererName(), 'fullPage', '', '', $this->_minLifetime);
         }
         Kwf_Benchmark::count("rendered miss", $component->componentId . ': fullPage');
         Kwf_Benchmark::countLog('fullpage-miss');
     } else {
         Kwf_Benchmark::count("rendered hit", $component->componentId . ': fullPage');
         Kwf_Benchmark::countLog('fullpage-hit');
     }
     $content = $this->_renderPass2($content);
     Kwf_Benchmark::checkpoint('render pass 2');
     Kwf_Component_Cache::getInstance()->writeBuffer();
     return $content;
 }
Exemplo n.º 3
0
 public function queryStart($queryText, $queryType = null)
 {
     if (!$this->_enabled) {
         return null;
     }
     Kwf_Benchmark::countLog('dbqueries');
     // make sure we have a query type
     if (null === $queryType) {
         switch (strtolower(substr($queryText, 0, 6))) {
             case 'insert':
                 $queryType = self::INSERT;
                 break;
             case 'update':
                 $queryType = self::UPDATE;
                 break;
             case 'delete':
                 $queryType = self::DELETE;
                 break;
             case 'select':
                 $queryType = self::SELECT;
                 break;
             default:
                 $queryType = self::QUERY;
                 break;
         }
     }
     $this->_lastQuery = new Zend_Db_Profiler_Query($queryText, $queryType);
     $this->_count++;
     self::$_staticCount++;
     if ($this->_logger) {
         $this->_logger->info($this->_count . ' ----------------------');
         $this->_logger->debug($queryText);
         //$this->_logger->debug(btString());
     }
     return $this->_count;
 }
Exemplo n.º 4
0
 public function deleteViewCache($select, $progressBarAdapter = null)
 {
     $microtime = $this->_getMicrotime();
     $select->whereEquals('deleted', false);
     $model = $this->getModel();
     $log = Kwf_Events_Log::getInstance();
     $cacheIds = array();
     $options = array('columns' => array('component_id', 'renderer', 'type', 'value'));
     $partialIds = array();
     $deleteIds = array();
     $checkIncludeIds = array();
     $rows = $model->export(Kwf_Model_Abstract::FORMAT_ARRAY, $select, $options);
     $progress = null;
     if ($progressBarAdapter) {
         $count = count($rows);
         $steps = (int) (count($rows) * 2 / 100 + 3);
         $step = 0;
         $progress = new Zend_ProgressBar($progressBarAdapter, 0, $steps);
     }
     foreach ($rows as $key => $row) {
         if ($progress && $key % 100 == 0) {
             $step += 100;
             $progress->next(1, "viewcache {$step} / {$count}");
         }
         $cacheIds[] = $this->_getCacheId($row['component_id'], $row['renderer'], $row['type'], $row['value']);
         Kwf_Benchmark::countLog('viewcache-delete-' . $row['type']);
         if ($row['type'] != 'fullPage' && !isset($checkIncludeIds[$row['component_id']])) {
             $checkIncludeIds[$row['component_id']] = true;
         }
         if ($log) {
             $log->log("delete view cache {$row['component_id']} {$row['renderer']} {$row['type']} {$row['value']}", Zend_Log::INFO);
         }
         $type = $row['type'];
         $value = $row['value'];
         $cId = $row['component_id'];
         if ($type == 'partial' && $value != '') {
             if (!isset($partialIds[$cId])) {
                 $partialIds[$cId] = array();
             }
             $partialIds[$cId][] = $value;
         } else {
             if ($value == '') {
                 if (!isset($deleteIds[$type])) {
                     $deleteIds[$type] = array();
                 }
                 $deleteIds[$type][] = $cId;
             } else {
                 throw new Kwf_Exception('Should not happen.');
             }
         }
     }
     // Memcache
     $this->_beforeMemcacheDelete($select);
     // For unit testing - DO NOT DELETE!
     if ($progress) {
         $step = 0;
     }
     foreach ($cacheIds as $key => $cacheId) {
         if ($progress && $key % 100 == 0) {
             $step += 100;
             $progress->next(1, "memcache {$step} / {$count}");
         }
         Kwf_Component_Cache_Memory::getInstance()->remove($cacheId, $microtime);
     }
     $this->_afterMemcacheDelete($select);
     // For unit testing - DO NOT DELETE!
     // FullPage
     if ($progress) {
         $progress->next(1, "fullPage");
     }
     $s = new Kwf_Model_Select();
     $s->whereEquals('type', 'fullPage');
     if ($checkIncludeIds) {
         $ids = array_keys($this->_fetchIncludesTree(array_keys($checkIncludeIds)));
         if ($ids) {
             foreach ($ids as &$id) {
                 $id = (string) $id;
             }
             unset($id);
             $s->whereEquals('component_id', $ids);
             if ($log) {
                 foreach ($ids as $id) {
                     $log->log("type=fullPage component_id={$id}", Zend_Log::INFO);
                 }
             }
             $this->deleteViewCache($s);
         }
     }
     // Database
     $this->_beforeDatabaseDelete($select);
     // For unit testing - DO NOT DELETE!
     if ($progress) {
         $progress->next(1, "partialIds");
     }
     foreach ($partialIds as $componentId => $values) {
         $select = $model->select();
         $select->where(new Kwf_Model_Select_Expr_And(array(new Kwf_Model_Select_Expr_Equals('component_id', (string) $componentId), new Kwf_Model_Select_Expr_Equals('type', 'partial'), new Kwf_Model_Select_Expr_Equals('value', $values), new Kwf_Model_Select_Expr_LowerEqual('microtime', $microtime))));
         $model->updateRows(array('deleted' => true), $select);
     }
     if ($progress) {
         $progress->next(1, "deleteIds");
     }
     foreach ($deleteIds as $type => $componentIds) {
         $select = $model->select();
         $select->where(new Kwf_Model_Select_Expr_And(array(new Kwf_Model_Select_Expr_Equals('component_id', $componentIds), new Kwf_Model_Select_Expr_Equals('type', $type), new Kwf_Model_Select_Expr_LowerEqual('microtime', $microtime))));
         $model->updateRows(array('deleted' => true), $select);
     }
     $this->_afterDatabaseDelete($select);
     // For unit testing - DO NOT DELETE!
     if ($progress) {
         $progress->finish();
     }
     file_put_contents('log/clear-view-cache', date('Y-m-d H:i:s') . ' ' . round(microtime(true) - Kwf_Benchmark::$startTime, 2) . 's; ' . Kwf_Events_Dispatcher::$eventsCount . ' events; ' . count($deleteIds) . ' view cache entries deleted; ' . (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '') . "\n", FILE_APPEND);
     return count($cacheIds);
 }
Exemplo n.º 5
0
 /**
  * Render components (ie. expand <kwc ...>)
  *
  * Pass 1 for content that can be stored in fullPage cache,
  * 2 for everything else. 2 includes 1, so calling just with 2 also works
  * @param string render content
  */
 protected function _renderPass2($ret, &$hasDynamicParts = false)
 {
     //execute all plugins that where added in pass 1
     $ret = $this->_findAndExecutePlugins($ret, self::PLUGIN_TYPE_USECACHE, $hasDynamicParts);
     $ret = $this->_findAndExecutePlugins($ret, self::PLUGIN_TYPE_BEFORE, $hasDynamicParts);
     $ret = $this->_findAndExecutePlugins($ret, self::PLUGIN_TYPE_REPLACE, $hasDynamicParts);
     $ret = $this->_findAndExecuteUseCacheDynamic($ret, $hasDynamicParts);
     static $benchmarkEnabled;
     if (!isset($benchmarkEnabled)) {
         $benchmarkEnabled = Kwf_Benchmark::isEnabled();
     }
     $offset = 0;
     while ($target = $this->_getNextRenderTarget($ret, 2, $offset)) {
         if ($benchmarkEnabled) {
             $startTime = microtime(true);
         }
         if ($target['type'] == 'dynamic' && $target['config']['class'] == 'Kwf_Component_Dynamic_SessionToken' && !Kwf_Setup::hasAuthedUser()) {
             $hasDynamicParts = true;
             //yes, this is cheating, but a very common case that's worth optimizing using this hack
             $ret = substr($ret, 0, $target['start']) . '' . substr($ret, $target['end'] + 1);
             continue;
         }
         $helper = $this->_getHelper($target['type']);
         $statType = null;
         $content = null;
         if ($this->_enableCache && $target['isCacheable']) {
             $content = $this->_cacheLoad($target['componentId'], $target['type'], $target['value']);
         }
         if (!is_null($content)) {
             //cache hit
             $statType = 'hit';
             //look for UseViewCache plugin in $content
             if ($p = $this->_findSinglePlugin(self::PLUGIN_TYPE_USECACHE, $content)) {
                 $hasDynamicParts = true;
                 if (!$p['plugin']->useViewCache($this)) {
                     //re-render, without <pluginC
                     $content = $this->_renderUncached($target['componentId'], $target['type'], $target['config']);
                 } else {
                     //continue with content
                     $content = $p['content'];
                 }
             } else {
                 //execute replace and before plugin
                 if ($p = $this->_findSinglePlugin(self::PLUGIN_TYPE_REPLACE, $content)) {
                     $hasDynamicParts = true;
                     $r = $p['plugin']->replaceOutput($this);
                     if ($r !== false) {
                         $content = $r;
                     } else {
                         $content = $p['content'];
                     }
                 }
                 $content = $this->_findAndExecutePlugins($content, self::PLUGIN_TYPE_BEFORE, $hasDynamicParts);
             }
             $content = $this->_findAndExecuteUseCacheDynamic($content, $hasDynamicParts);
         } else {
             if ($this->_enableCache && $target['isCacheable']) {
                 //cache miss
                 $statType = 'miss';
                 $content = $this->_renderAndCache($target['componentId'], $target['type'], $target['value'], $target['config'], false);
             } else {
                 $hasDynamicParts = true;
                 //view cache disabled
                 $statType = 'noviewcache';
                 $content = $this->_renderUncached($target['componentId'], $target['type'], $target['config']);
             }
         }
         $content = $helper->renderCached($content, $target['componentId'], $target['config']);
         $ret = substr($ret, 0, $target['start']) . $content . substr($ret, $target['end'] + 1);
         if ($statType) {
             if ($benchmarkEnabled) {
                 Kwf_Benchmark::count("rendered {$statType}", $target['statId']);
             }
             Kwf_Benchmark::countLog('render-' . $statType);
         }
         if ($benchmarkEnabled) {
             Kwf_Benchmark::subCheckpoint($target['componentId'] . ' ' . $target['type'], microtime(true) - $startTime);
         }
     }
     //execute Render Cached Dynamic, used eg for callback link modifier in componentLink
     while (($start = strpos($ret, '<rcd ')) !== false) {
         $hasDynamicParts = true;
         $startEnd = strpos($ret, '>', $start);
         $args = explode(' ', substr($ret, $start + 5, $startEnd - $start - 5));
         $end = strpos($ret, '</rcd ' . $args[0] . '>');
         $content = substr($ret, $startEnd + 1, $end - $startEnd - 1);
         if ($benchmarkEnabled) {
             $startTime = microtime(true);
         }
         $componentId = $args[0];
         $type = $args[1];
         $settings = json_decode($args[2], true);
         $content = $this->_getHelper($type)->renderCachedDynamic($content, $componentId, $settings);
         if ($benchmarkEnabled) {
             Kwf_Benchmark::subCheckpoint("renderCachedDynamic {$type} {$componentId}", microtime(true) - $startTime);
         }
         $ret = substr($ret, 0, $start) . $content . substr($ret, $end + 7 + strlen($args[0]));
     }
     $ret = $this->_findAndExecutePlugins($ret, self::PLUGIN_TYPE_AFTER, $hasDynamicParts);
     return $ret;
 }