APC work follow-up

I managed to send an outdated patch to the mailing list that got applied, so
some of these changes never made it in. A little more cleanup and a little
more caching, and also increase the default time to 600 seconds (10
minutes).

Signed-off-by: Dan McGee <dan@archlinux.org>
Signed-off-by: Loui Chang <louipc.ist@gmail.com>
This commit is contained in:
Dan McGee 2009-01-23 19:32:55 -06:00 committed by Loui Chang
parent d6a09ba1a5
commit 9cf7f1a4a2

View file

@ -2,7 +2,10 @@
include_once('aur.inc'); include_once('aur.inc');
# APC configuration variables
$apc_prefix = 'aur:'; $apc_prefix = 'aur:';
$apc_ttl = 600;
# Check if APC extension is loaded # Check if APC extension is loaded
if (!defined('EXTENSION_LOADED_APC')) if (!defined('EXTENSION_LOADED_APC'))
define('EXTENSION_LOADED_APC', extension_loaded('apc')); define('EXTENSION_LOADED_APC', extension_loaded('apc'));
@ -12,14 +15,17 @@ if (!defined('EXTENSION_LOADED_APC'))
# #
function db_cache_value($dbq, $dbh, $key) function db_cache_value($dbq, $dbh, $key)
{ {
if(!(EXTENSION_LOADED_APC && ($ret = apc_fetch($key)))) { global $apc_ttl;
$bool = false;
if(EXTENSION_LOADED_APC) {
$ret = apc_fetch($key, $bool);
}
if(!$bool) {
$result = db_query($dbq, $dbh); $result = db_query($dbq, $dbh);
$row = mysql_fetch_row($result); $row = mysql_fetch_row($result);
$ret = $row[0]; $ret = $row[0];
# set the TTL here in seconds: 300 seconds = 5 minutes
if (EXTENSION_LOADED_APC) { if (EXTENSION_LOADED_APC) {
apc_store($key, $ret, 300); apc_store($key, $ret, $apc_ttl);
} }
} }
return $ret; return $ret;
@ -27,7 +33,7 @@ function db_cache_value($dbq, $dbh, $key)
function updates_table($dbh) function updates_table($dbh)
{ {
global $apc_prefix; global $apc_prefix, $apc_ttl;
$key = $apc_prefix . 'recent_updates'; $key = $apc_prefix . 'recent_updates';
if(!(EXTENSION_LOADED_APC && ($newest_packages = apc_fetch($key)))) { if(!(EXTENSION_LOADED_APC && ($newest_packages = apc_fetch($key)))) {
$q = 'SELECT * FROM Packages WHERE DummyPkg != 1 ORDER BY GREATEST(SubmittedTS,ModifiedTS) DESC LIMIT 0 , 10'; $q = 'SELECT * FROM Packages WHERE DummyPkg != 1 ORDER BY GREATEST(SubmittedTS,ModifiedTS) DESC LIMIT 0 , 10';
@ -37,9 +43,8 @@ function updates_table($dbh)
while ($row = mysql_fetch_assoc($result)) { while ($row = mysql_fetch_assoc($result)) {
$newest_packages->append($row); $newest_packages->append($row);
} }
if (EXTENSION_LOADED_APC) { if (EXTENSION_LOADED_APC) {
apc_store($key, $newest_packages, 300); apc_store($key, $newest_packages, $apc_ttl);
} }
} }
include('stats/updates_table.php'); include('stats/updates_table.php');
@ -47,28 +52,24 @@ function updates_table($dbh)
function user_table($user, $dbh) function user_table($user, $dbh)
{ {
global $apc_prefix;
$escuser = mysql_real_escape_string($user);
$base_q = "SELECT count(*) FROM Packages,PackageLocations,Users WHERE Packages.MaintainerUID = Users.ID AND Packages.LocationID = PackageLocations.ID AND PackageLocations.Location = '%s' AND Users.Username='" . $escuser . "'";
$base_q = 'SELECT count(*) FROM Packages,PackageLocations,Users WHERE Packages.MaintainerUID = Users.ID AND Packages.LocationID = PackageLocations.ID AND PackageLocations.Location = "%s" AND Users.Username="' . $maintainer_unsupported_count = db_cache_value(sprintf($base_q, 'unsupported'), $dbh,
mysql_real_escape_string($user).'"'; $apc_prefix . 'user_unsupported_count:' . $escuser);
$result = db_query(sprintf($base_q, 'unsupported'), $dbh); $q = "SELECT count(*) FROM Packages,Users WHERE Packages.OutOfDate = 1 AND Packages.MaintainerUID = Users.ID AND Users.Username='" . $escuser . "'";
$row = mysql_fetch_row($result);
$maintainer_unsupported_count = $row[0];
$q = "SELECT count(*) FROM Packages,Users WHERE Packages.OutOfDate = 1 AND Packages.MaintainerUID = Users.ID AND Users.Username='" . $flagged_outdated = db_cache_value($q, $dbh,
mysql_real_escape_string($user)."'"; $apc_prefix . 'user_flagged_outdated:' . $escuser);
$result = db_query($q, $dbh);
$row = mysql_fetch_row($result);
$flagged_outdated = $row[0];
# If the user is a TU calculate the number of the packages # If the user is a TU calculate the number of the packages
$atype = account_from_sid($_COOKIE["AURSID"]); $atype = account_from_sid($_COOKIE["AURSID"]);
if (($atype == 'Trusted User') || ($atype == 'Developer')) { if (($atype == 'Trusted User') || ($atype == 'Developer')) {
$result = db_query(sprintf($base_q, 'community'), $dbh); $maintainer_community_count = db_cache_value(sprintf($base_q, 'community'), $dbh,
$row = mysql_fetch_row($result); $apc_prefix . 'user_community_count:' . $escuser);
$maintainer_community_count = $row[0];
} }
include('stats/user_table.php'); include('stats/user_table.php');