diff options
author | elij <elij.mx@gmail.com> | 2011-05-29 01:17:09 +0200 |
---|---|---|
committer | Lukas Fleischer <archlinux@cryptocrack.de> | 2011-06-22 15:02:21 +0200 |
commit | af5d05f4ad517489558afcbc2296999c724d2c1e (patch) | |
tree | 9b603dcc0006569c152ea72aade1bdfc8ecf4e79 | |
parent | fa53ca68b004b9a97906e0bcfe8a7d3ba2ad0f9c (diff) | |
download | aur-af5d05f4ad517489558afcbc2296999c724d2c1e.tar.gz aur-af5d05f4ad517489558afcbc2296999c724d2c1e.tar.xz |
refactor apc code and move to aur.inc
- move apc cache code to aur.inc (centralize)
- refactor the apc usage in stats.inc to utilize new code in aur.inc
Lukas: Small commenting style and spelling changes.
Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de>
-rw-r--r-- | web/lib/aur.inc | 60 | ||||
-rw-r--r-- | web/lib/stats.inc | 61 |
2 files changed, 66 insertions, 55 deletions
diff --git a/web/lib/aur.inc b/web/lib/aur.inc index fb267af8..1f7b71ad 100644 --- a/web/lib/aur.inc +++ b/web/lib/aur.inc @@ -14,6 +14,12 @@ include_once("config.inc"); include_once("version.inc"); include_once("acctfuncs.inc"); +# Check if APC extension is loaded, and set cache prefix if it is. +if (!defined('EXTENSION_LOADED_APC')) { + define('EXTENSION_LOADED_APC', extension_loaded('apc')); + define('APC_PREFIX', 'aur:'); +} + # see if the visitor is already logged in # function check_sid() { @@ -257,6 +263,46 @@ function db_query($query="", $db_handle="") { return $result; } +# Set a value in the cache (currently APC) if cache is available for use. If +# not available, this becomes effectively a no-op (return value is +# false). Accepts an optional TTL (defaults to 600 seconds). +function set_cache_value($key, $value, $ttl=600) { + $status = false; + if (EXTENSION_LOADED_APC) { + $status = apc_store(APC_PREFIX.$key, $value, $ttl); + } + return $status; +} + +# Get a value from the cache (currently APC) if cache is available for use. If +# not available, this returns false (optionally sets passed in variable $status +# to false, much like apc_fetch() behaves). This allows for testing the fetch +# result appropriately even in the event that a 'false' value was the value in +# the cache. +function get_cache_value($key, &$status=false) { + if(EXTENSION_LOADED_APC) { + $ret = apc_fetch(APC_PREFIX.$key, $status); + if ($status) { + return $ret; + } + } + return $status; +} + +# Run a simple db query, retrieving and/or caching the value if APC is +# available for use. Accepts an optional TTL value (defaults to 600 seconds). +function db_cache_value($dbq, $dbh, $key, $ttl=600) { + $status = false; + $value = get_cache_value($key, $status); + if (!$status) { + $result = db_query($dbq, $dbh); + $row = mysql_fetch_row($result); + $value = $row[0]; + set_cache_value($key, $value, $ttl); + } + return $value; +} + # set up the visitor's language # function set_lang() { @@ -491,12 +537,12 @@ function get_salt($user_id) { $dbh = db_connect(); $salt_q = "SELECT Salt FROM Users WHERE ID = " . $user_id; - $result = db_query($salt_q, $dbh); - if ($result) { - $salt_row = mysql_fetch_row($result); - return $salt_row[0]; - } - return; + $result = db_query($salt_q, $dbh); + if ($result) { + $salt_row = mysql_fetch_row($result); + return $salt_row[0]; + } + return; } function save_salt($user_id, $passwd) @@ -535,7 +581,7 @@ function parse_comment($comment) if ($i % 2) { # convert links $html .= '<a href="' . htmlspecialchars($matches[$i]) . - '">' . htmlspecialchars($matches[$i]) . '</a>'; + '">' . htmlspecialchars($matches[$i]) . '</a>'; } else { # convert everything else diff --git a/web/lib/stats.inc b/web/lib/stats.inc index 756fa27c..67fbdca9 100644 --- a/web/lib/stats.inc +++ b/web/lib/stats.inc @@ -2,40 +2,10 @@ include_once('aur.inc'); -# APC configuration variables -$apc_prefix = 'aur:'; -$apc_ttl = 600; - -# Check if APC extension is loaded -if (!defined('EXTENSION_LOADED_APC')) - define('EXTENSION_LOADED_APC', extension_loaded('apc')); - -# run a simple db query, retrieving and/or caching the value if APC -# is available for use -# -function db_cache_value($dbq, $dbh, $key) -{ - global $apc_ttl; - $bool = false; - if(EXTENSION_LOADED_APC) { - $ret = apc_fetch($key, $bool); - } - if(!$bool) { - $result = db_query($dbq, $dbh); - $row = mysql_fetch_row($result); - $ret = $row[0]; - if (EXTENSION_LOADED_APC) { - apc_store($key, $ret, $apc_ttl); - } - } - return $ret; -} - function updates_table($dbh) { - global $apc_prefix, $apc_ttl; - $key = $apc_prefix . 'recent_updates'; - if(!(EXTENSION_LOADED_APC && ($newest_packages = apc_fetch($key)))) { + $key = 'recent_updates'; + if(!($newest_packages = get_cache_value($key))) { $q = 'SELECT * FROM Packages ORDER BY ModifiedTS DESC LIMIT 10'; $result = db_query($q, $dbh); @@ -43,26 +13,23 @@ function updates_table($dbh) while ($row = mysql_fetch_assoc($result)) { $newest_packages->append($row); } - if (EXTENSION_LOADED_APC) { - apc_store($key, $newest_packages, $apc_ttl); - } + set_cache_value($key, $newest_packages); } include('stats/updates_table.php'); } function user_table($user, $dbh) { - global $apc_prefix; $escuser = mysql_real_escape_string($user); $base_q = "SELECT count(*) FROM Packages,Users WHERE Packages.MaintainerUID = Users.ID AND Users.Username='" . $escuser . "'"; $maintainer_unsupported_count = db_cache_value($base_q, $dbh, - $apc_prefix . 'user_unsupported_count:' . $escuser); + 'user_unsupported_count:' . $escuser); $q = "SELECT count(*) FROM Packages,Users WHERE Packages.OutOfDateTS IS NOT NULL AND Packages.MaintainerUID = Users.ID AND Users.Username='" . $escuser . "'"; $flagged_outdated = db_cache_value($q, $dbh, - $apc_prefix . 'user_flagged_outdated:' . $escuser); + 'user_flagged_outdated:' . $escuser); # If the user is a TU calculate the number of the packages $atype = account_from_sid($_COOKIE["AURSID"]); @@ -72,35 +39,33 @@ function user_table($user, $dbh) function general_stats_table($dbh) { - global $apc_prefix; # AUR statistics $q = "SELECT count(*) FROM Packages"; - $unsupported_count = db_cache_value($q, $dbh, $apc_prefix . 'unsupported_count'); + $unsupported_count = db_cache_value($q, $dbh, 'unsupported_count'); $q = "SELECT count(*) FROM Packages WHERE MaintainerUID IS NULL"; - $orphan_count = db_cache_value($q, $dbh, $apc_prefix . 'orphan_count'); + $orphan_count = db_cache_value($q, $dbh, 'orphan_count'); $q = "SELECT count(*) FROM Users"; - $user_count = db_cache_value($q, $dbh, $apc_prefix . 'user_count'); + $user_count = db_cache_value($q, $dbh, 'user_count'); $q = "SELECT count(*) FROM Users,AccountTypes WHERE Users.AccountTypeID = AccountTypes.ID AND AccountTypes.AccountType = 'Trusted User'"; - $tu_count = db_cache_value($q, $dbh, $apc_prefix . 'tu_count'); + $tu_count = db_cache_value($q, $dbh, 'tu_count'); $targstamp = intval(strtotime("-7 days")); $yearstamp = intval(strtotime("-1 year")); $q = "SELECT count(*) FROM Packages WHERE Packages.ModifiedTS >= $targstamp AND Packages.ModifiedTS = Packages.SubmittedTS"; - $add_count = db_cache_value($q, $dbh, $apc_prefix . 'add_count'); + $add_count = db_cache_value($q, $dbh, 'add_count'); $q = "SELECT count(*) FROM Packages WHERE Packages.ModifiedTS >= $targstamp AND Packages.ModifiedTS != Packages.SubmittedTS"; - $update_count = db_cache_value($q, $dbh, $apc_prefix . 'update_count'); + $update_count = db_cache_value($q, $dbh, 'update_count'); $q = "SELECT count(*) FROM Packages WHERE Packages.ModifiedTS >= $yearstamp AND Packages.ModifiedTS != Packages.SubmittedTS"; - $update_year_count = db_cache_value($q, $dbh, $apc_prefix . 'update_year_count'); + $update_year_count = db_cache_value($q, $dbh, 'update_year_count'); $q = "SELECT count(*) FROM Packages WHERE Packages.ModifiedTS = Packages.SubmittedTS"; - $never_update_count = db_cache_value($q, $dbh, $apc_prefix . 'never_update_count'); + $never_update_count = db_cache_value($q, $dbh, 'never_update_count'); include('stats/general_stats_table.php'); } - |