PHP/drupal优化之静态变量和缓存使用

December 29th, 2009 by Bruce Dou Views:251

假如一个页面中某个函数被多次调用,即可使用静态变量来做cache,提高运行速度

function my_module_function($reset = FALSE) {
static $my_data;
if (!isset($my_data) || $reset) {
// Do your expensive calculations here, and populate $my_data
// with the correct stuff..
}
return $my_data;
}

Drupal中最明显的利用memcache的函数就是

drupal_lookup_path 假如不做memcache或者静态变量,后果就是每页多了一半的查询
使用memcache缓存优化此函数:

function drupal_lookup_path($action, $path = ”, $path_language = ”) {
global $language;
// $map is an array with language keys, holding arrays of Drupal paths to alias relations
/*
static $map = array(), $no_src = array(), $has_paths;
*/
static $map = array(), $no_src = array();

$path_language = $path_language ? $path_language : $language->language;
// Use $has_paths to avoid looking up paths in subsequent calls if there simply are no aliases
/*
if (!isset($has_paths)) {
$has_paths = (db_result(db_query_range(‘SELECT dst FROM {url_alias}’, 0, 1)) !== FALSE);
}
*/
if ($action == ‘wipe’) {
$map = array();
$no_src = array();
//$has_paths = NULL;
//ADDED BY BRUCE
cache_clear_all(‘*’, ‘cache_pathdst’, TRUE);
cache_clear_all(‘*’, ‘cache_pathsrc’, TRUE);
//END
}
elseif (/*$has_paths && */$path != ”) {
if ($action == ‘alias’) {
if (isset($map[$path_language][$path])) {
return $map[$path_language][$path];
}
//ADDED BY BRUCE
$cid = $path . $path_language;
$cache = cache_get($cid, ‘cache_pathdst’);
if ($cache) {
return $cache->data;
}
//END
// Get the most fitting result falling back with alias without language
$alias = db_result(db_query(“SELECT dst FROM {url_alias} WHERE src = ‘%s’ AND language IN(‘%s’, ”) ORDER BY language DESC”, $path, $path_language));
$map[$path_language][$path] = $alias;
//ADDED BY BRUCE
cache_set($cid, $alias, ‘cache_pathdst’, time() + variable_get(‘pathcache_expire’, 43200)); // 12 hours
//END
return $alias;
}
// Check $no_src for this $path in case we’ve already determined that there
// isn’t a path that has this alias
elseif ($action == ‘source’ && !isset($no_src[$path_language][$path])) {
// Look for the value $path within the cached $map
$src = ”;
if (!isset($map[$path_language]) || !($src = array_search($path, $map[$path_language]))) {
//ADDED BY BRUCE
$cid = $path . $path_language;
$cache = cache_get($cid, ‘cache_pathsrc’);
if ($cache) {
return $cache->data;
}
//END
// Get the most fitting result falling back with alias without language
if ($src = db_result(db_query(“SELECT src FROM {url_alias} WHERE dst = ‘%s’ AND language IN(‘%s’, ”) ORDER BY language DESC”, $path, $path_language))) {
$map[$path_language][$src] = $path;
//ADDED BY BRUCE
cache_set($cid, $src, ‘cache_pathsrc’, time() + variable_get(‘pathcache_expire’, 43200)); // 12 hours
//END
}
else {
// We can’t record anything into $map because we do not have a valid
// index and there is no need because we have not learned anything
// about any Drupal path. Thus cache to $no_src.
$no_src[$path_language][$path] = TRUE;
//ADDED BY BRUCE
cache_set($cid, $path, ‘cache_pathsrc’, time() + variable_get(‘pathcache_expire’, 43200)); // 12 hours
//END
}
}
return $src;
}
}
return FALSE;
}

进行优化后和优化前页面执行sql的比较:

Before patch

Executed 271 queries in 206.31 milliseconds. Queries taking longer than 100 ms and queries executed more than once, are highlighted. Page execution time was 867.37 ms.

After patch

Executed 138 queries in 71.1 milliseconds. Queries taking longer than 100 ms and queries executed more than once, are highlighted. Page execution time was 741.77 ms.

使用静态变量优化此函数

function drupal_lookup_path($action, $path = ”) {
static $map = array();
if ($action == ‘wipe’)
{
$map = array();
return FALSE;
}
if ($path != ”)
{
// Initial load from DB
if (count($map) == 0) {
$result = db_query(“SELECT src, dst FROM {url_alias}”);
while ($url = db_fetch_object($result)) {
$map[$url->src] = $url->dst;
$map[$url->dst] = $url->src;
}
}
// $action == ‘alias’ or $action == ‘source’
if (isset($map[$path])) return $map[$path];
}
return FALSE;
}

两种方式的优化都有自己的使用范围,慎用。

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • 豆瓣
  • DZone
  • LinkedIn
  • MySpace
  • Reddit
  • RSS

Also see:

Tags: , ,

Leave a Reply