Some performance hacks for zp #1

I've done some minor hacks on zp to boost performance. I'm using eAccelerator with PHP-FastCGI, but these hacks can easily be ported to APC, XCache or even memcached. Alternatively, you may also use disk-based caching.

#1 Reduce load on the DB server:
This is based on the caching technique described on http://www.zenphoto.org/2008/01/hacks/ We'll use eAccelerator shm cache instead of storing the query results on disk.

* Edit the functions-db.php under zp-core directory.
* Search function query_single_row, change it to:
`

function query_single_row($sql, $noerrmsg=false) {

if ($cache = get_cache($sql))

return $cache;

$result = query($sql, $noerrmsg);

if ($result) {

$data = mysql_fetch_assoc($result);

store_cache($sql, $data);

return $data;

}

else {

return false;

}

}

`
* Search function query_full_array, change it to:
`

function query_full_array($sql, $noerrmsg = false) {

if ($cache = get_cache($sql))

return $cache;

$result = query($sql, $noerrmsg);

if ($result) {

$allrows = array();

while ($row = mysql_fetch_assoc($result))

$allrows[] = $row;

store_cache($sql, $allrows);

return $allrows;

}

else {

return false;

}

}

`
* Last thing, add two functions in functions-db.php. You could change “cachedirectory” to your cache directory, and change $cache_time_out to the expire time you want.
`

function store_cache($query, $result_cache) {

$cache_time_out = 600;

if (preg_match("/^(insert|delete|update|replace)\s+/i",$query))

return;

if (stristr($query, "ORDER BY RAND"))

return;

$key = 'sql_' . md5($query);

$val = serialize($result_cache);

eaccelerator_put($key, $val, $cache_time_out);

}

function get_cache($query) {

if (preg_match("/^(insert|delete|update|replace)\s+/i",$query))

return;

if (stristr($query, "ORDER BY RAND"))

return;

$key = 'sql_' . md5($query);

$val = eaccelerator_get($key);

if ( $val != NULL )

return unserialize($val);

}

`

Comments

  • Oops! Posted in the wrong forum.
    @admin: Please relocate this thread to the Gen. discussions.
  • acrylian Administrator, Developer
    Not really a problem, I don't really look at the "categories" anyway since I read mostly via RSS..:-)
Sign In or Register to comment.