Index: branches/5.1.x/core/units/helpers/mod_rewrite_helper.php =================================================================== diff -u -r13159 -r13168 --- branches/5.1.x/core/units/helpers/mod_rewrite_helper.php (.../mod_rewrite_helper.php) (revision 13159) +++ branches/5.1.x/core/units/helpers/mod_rewrite_helper.php (.../mod_rewrite_helper.php) (revision 13168) @@ -1,6 +1,6 @@ Conn->GetRow($sql); - if (false && $cache && $cache['Cached'] > 0) { - // not used for now - $cache = unserialize($cache['Data']); - $vars = $cache['vars']; - $passed = $cache['passed']; + $cached = $this->_getCachedUrl($url); + + if ($cached !== false) { + $vars = $cached['vars']; + $passed = $cached['passed']; $restored = true; } else { $vars = $this->parseRewriteURL($url); $passed = $vars['pass']; // also used in bottom of this method unset($vars['pass']); - $cache = Array ('vars' => $vars, 'passed' => $passed); + $this->_setCachedUrl($url, Array ('vars' => $vars, 'passed' => $passed)); - $fields_hash = Array ( - 'VarName' => 'mod_rw_' . md5($url), - 'Data' => serialize($cache), - 'Cached' => adodb_mktime(), - ); - $this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'Cache', 'REPLACE'); - if (array_key_exists('t', $this->HTTPQuery->Post) && $this->HTTPQuery->Post['t']) { // template from POST overrides template from URL. $vars['t'] = $this->HTTPQuery->Post['t']; @@ -111,19 +100,90 @@ $this->HTTPQuery->finalizeParsing($passed); } - function parseRewriteURL($url) + function _getCachedUrl($url) { - $sql = 'SELECT Data, Cached - FROM ' . TABLE_PREFIX . 'Cache - WHERE VarName = "mod_rw_' . md5($url) . '"'; - $vars = $this->Conn->GetRow($sql); + if (!$url) { + return false; + } - if (false && $vars && $vars['Cached'] > 0) { - // not used for now - $vars = unserialize($menu['Data']); - return $vars; + $sql = 'SELECT * + FROM ' . TABLE_PREFIX . 'CachedUrls + WHERE Hash = ' . crc32($url); + $data = $this->Conn->GetRow($sql); + + if ($data) { + $lifetime = (int)$data['LifeTime']; // in seconds + if (($lifetime > 0) && ($data['Cached'] + $lifetime < adodb_mktime())) { + // delete expired + $sql = 'DELETE FROM ' . TABLE_PREFIX . 'CachedUrls + WHERE UrlId = ' . $data['UrlId']; + $this->Conn->Query($sql); + + return false; + } + + return unserialize($data['ParsedVars']); } + return false; + } + + function _setCachedUrl($url, $data) + { + if (!$url) { + return ; + } + + $vars = $data['vars']; + $passed = $data['passed']; + sort($passed); + + // get expiration + if ($vars['m_cat_id'] > 0) { + $sql = 'SELECT PageExpiration + FROM ' . TABLE_PREFIX . 'Category + WHERE CategoryId = ' . $vars['m_cat_id']; + $expiration = $this->Conn->GetOne($sql); + } + + // get prefixes + $prefixes = Array (); + $m_index = array_search('m', $passed); + + if ($m_index !== false) { + unset($passed[$m_index]); + + if ($vars['m_cat_id'] > 0) { + $prefixes[] = 'c:' . $vars['m_cat_id']; + } + + $prefixes[] = 'lang:' . $vars['m_lang']; + $prefixes[] = 'theme:' . $vars['m_theme']; + } + + foreach ($passed as $prefix) { + if (array_key_exists($prefix . '_id', $vars) && is_numeric($vars[$prefix . '_id'])) { + $prefixes[] = $prefix . ':' . $vars[$prefix . '_id']; + } + else { + $prefixes[] = $prefix; + } + } + + $fields_hash = Array ( + 'Url' => $url, + 'Hash' => crc32($url), + 'Prefixes' => $prefixes ? '|' . implode('|', $prefixes) . '|' : '', + 'ParsedVars' => serialize($data), + 'Cached' => adodb_mktime(), + 'LifeTime' => isset($expiration) && is_numeric($expiration) ? $expiration : -1 + ); + + $this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'CachedUrls'); + } + + function parseRewriteURL($url) + { $vars = Array ('pass' => Array ('m')); $url_parts = $url ? explode('/', trim(mb_strtolower($url, 'UTF-8'), '/')) : Array (); @@ -329,14 +389,14 @@ // add language $default_language_id = $this->Application->GetDefaultLanguageId(); if ($processed_params['m_lang'] && ($processed_params['m_lang'] != $default_language_id)) { - $language_name = $this->Application->getCache('language_names', $processed_params['m_lang']); + $language_name = $this->Application->getCache('language_names[%LangIDSerial:' . $processed_params['m_lang'] . '%]'); if ($language_name === false) { $sql = 'SELECT PackName FROM ' . TABLE_PREFIX . 'Language WHERE LanguageId = ' . $processed_params['m_lang']; $language_name = $this->Conn->GetOne($sql); - $this->Application->setCache('language_names', $processed_params['m_lang'], $language_name); + $this->Application->setCache('language_names[%LangIDSerial:' . $processed_params['m_lang'] . '%]', $language_name); } $ret .= $language_name . '/'; @@ -345,14 +405,14 @@ // add theme $default_theme_id = $this->Application->GetDefaultThemeId(true); if ($processed_params['m_theme'] && ($processed_params['m_theme'] != $default_theme_id)) { - $theme_name = $this->Application->getCache('theme_names', $processed_params['m_theme']); + $theme_name = $this->Application->getCache('theme_names[%ThemeIDSerial:' . $processed_params['m_theme'] . '%]'); if ($theme_name === false) { $sql = 'SELECT Name FROM ' . TABLE_PREFIX . 'Theme WHERE ThemeId = ' . $processed_params['m_theme']; $theme_name = $this->Conn->GetOne($sql); - $this->Application->setCache('theme_names', $processed_params['m_theme'], $theme_name); + $this->Application->setCache('theme_names[%ThemeIDSerial:' . $processed_params['m_theme'] . '%]', $theme_name); } @@ -366,7 +426,7 @@ // add category if ($processed_params['m_cat_id'] > 0 && $params['pass_category']) { - $category_filename = $this->Application->getFilename('c', $processed_params['m_cat_id']); + $category_filename = $this->Application->getCategoryCache($processed_params['m_cat_id'], 'filenames'); preg_match('/^Content\/(.*)/i', $category_filename, $regs); @@ -401,7 +461,7 @@ } $template = array_key_exists('t', $params) ? $params['t'] : false; - $category_template = ($processed_params['m_cat_id'] > 0) && $params['pass_category'] ? $this->Application->getCache('category_designs', $processed_params['m_cat_id']) : ''; + $category_template = ($processed_params['m_cat_id'] > 0) && $params['pass_category'] ? $this->Application->getCategoryCache($processed_params['m_cat_id'], 'category_designs') : ''; if ((strtolower($template) == '__default__') && ($processed_params['m_cat_id'] == 0)) { // for "Home" category set template to index when not set @@ -696,13 +756,10 @@ } if ($processed_params[$prefix_special . '_id']) { - // this allows to fill 3 cache records with one query (see this method for details) $category_id = array_key_exists('m_cat_id', $params) ? $params['m_cat_id'] : $this->Application->GetVar('m_cat_id'); - $category_filename = $this->Application->getFilename('c', $category_id); // if template is also item template of category, then remove template $template = array_key_exists('t', $params) ? $params['t'] : false; - $item_template = $this->GetItemTemplate($category_id, $prefix); if ($template == $item_template || strtolower($template) == '__default__') { @@ -887,9 +944,10 @@ */ function GetItemTemplate($category, $module_prefix) { - $cache_key = serialize($category) . '_' . $module_prefix; + $category_id = is_array($category) ? $category['CategoryId'] : $category; + $cache_key = __CLASS__ . '::' . __FUNCTION__ . '[%CIDSerial:' . $category_id . '%]:' . $module_prefix; - $cached_value = $this->Application->getCache(__CLASS__ . __FUNCTION__, $cache_key); + $cached_value = $this->Application->getCache($cache_key); if ($cached_value !== false) { return $cached_value; } @@ -938,7 +996,7 @@ $item_template = $this->_templateAliases[$item_template]; } - $this->Application->setCache(__CLASS__ . __FUNCTION__, $cache_key, $item_template); + $this->Application->setCache($cache_key, $item_template); return $item_template; } @@ -973,7 +1031,9 @@ */ function getItemTemplateCustomField($module_prefix) { - $cached_value = $this->Application->getCache(__CLASS__ . __FUNCTION__, $module_prefix); + $cache_key = __CLASS__ . '::' . __FUNCTION__ . '[%CfSerial%]:' . $module_prefix; + $cached_value = $this->Application->getCache($cache_key); + if ($cached_value !== false) { return $cached_value; } @@ -983,7 +1043,7 @@ WHERE FieldName = ' . $this->Conn->qstr($module_prefix . '_ItemTemplate'); $item_template_field_id = $this->Conn->GetOne($sql); - $this->Application->setCache(__CLASS__ . __FUNCTION__, $module_prefix, $item_template_field_id); + $this->Application->setCache($cache_key, $item_template_field_id); return $item_template_field_id; }