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

December 29th, 2009

假如一个页面中某个函数被多次调用,即可使用静态变量来做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;
}

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

mysql数据库如何统计各个表的大小

December 29th, 2009

一条查询可以搞定:

select table_schema,
sum(data_length+index_length)/1024/1024 as total_mb,
sum(data_length)/1024/1024 as data_mb,
sum(index_length)/1024/1024 as index_mb,
count(*) as tables
from information_schema.tables
group by table_schema
order by 2 desc;

另:合适增加使用index可以提高查询效率,但不可滥用。

windows下的utf8文件头部字节

December 29th, 2009

Microsoft自己为了方便识别utf8文件格式,在这类文件头部加入了efbbbf 几个字节。称为UTF8-BOM。

其他平台则没有这个现象。处理这类文本的时候需要将efbbbf先过滤掉。

推荐连岳的博客

December 12th, 2009

谁是连岳?

http://baike.baidu.com/view/610071.htm?fr=ala0

连岳的博客

http://www.lianyue.net/blogs/lianyue/

软件随想录在线阅读

December 11th, 2009

中文版http://local.joelonsoftware.com/wiki/Chinese_(Simplified)

shupi.jpg

作者Jole和译者阮一峰都是很有意思的人,强烈推荐