From c819d552c0884d11cbe6a1e392d49e5e52d63ef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rytis=20Slatkevi=C4=8Dius?= Date: Sun, 13 Mar 2011 20:05:25 +0000 Subject: [PATCH] user web: change remaining occurrences of fullpage caching to object cache. start_cache() and end_cache() are now deprecated. svn path=/trunk/boinc/; revision=23230 --- checkin_notes | 16 +++++- html/inc/cache.inc | 12 ++-- html/inc/team.inc | 15 +++-- html/user/download_network.php | 16 +++--- html/user/server_status.php | 101 ++++++++++++++++++++------------- html/user/team_members.php | 12 ++-- html/user/userw.php | 71 ++++++++++++----------- 7 files changed, 142 insertions(+), 101 deletions(-) diff --git a/checkin_notes b/checkin_notes index 8dc378dab7..186317aa9f 100644 --- a/checkin_notes +++ b/checkin_notes @@ -1581,8 +1581,22 @@ Rytis 12 Mar 2011 top_users.php view_profile.php -David 12 Mar 2001 +David 12 Mar 2011 - move client simulator PHP code to doc/sim doc/sim/ various + +Rytis 13 Mar 2011 + - user web: change remaining occurrences of fullpage caching to object + cache. start_cache() and end_cache() are now deprecated. + + html/ + inc/ + cache.inc + team.inc + user/ + download_network.php + server_status.php + team_members.php + userw.php diff --git a/html/inc/cache.inc b/html/inc/cache.inc index ec54ee78d6..4317705685 100644 --- a/html/inc/cache.inc +++ b/html/inc/cache.inc @@ -167,8 +167,10 @@ function get_cached_data($max_age, $params=""){ if ($max_age) { if (defined('MEMCACHE_SERVERS')) { $cache = BoincMemcache::get()->get($path); - if ($cache) { - return $cache->content; + if ($cache['content']) { + return $cache['content']; + } else { + return $cache; } } else { cache_check_diskspace(); @@ -181,6 +183,7 @@ function get_cached_data($max_age, $params=""){ return false; //No data was cached, just return } +// DEPRECATED function start_cache($max_age, $params=""){ global $no_cache, $caching, $memcache; @@ -242,7 +245,7 @@ function start_cache($max_age, $params=""){ } // write output buffer both to client and to cache -// +// DEPRECATED function end_cache($max_age,$params=""){ global $no_cache; if ($no_cache) return; @@ -276,8 +279,7 @@ function set_cached_data($max_age, $data, $params=""){ $path = get_path($params); if (defined('MEMCACHE_SERVERS')) { $cache = array('content' => $data, 'timestamp' => time()); - ob_end_flush(); - $cache = BoincMemcache::get()->set($path, $cache, $max_age); + BoincMemcache::get()->set($path, $cache, $max_age); } else { $fhandle = fopen($path, "w"); fwrite($fhandle, $data); diff --git a/html/inc/team.inc b/html/inc/team.inc index a4c88da2e9..073f9717a8 100644 --- a/html/inc/team.inc +++ b/html/inc/team.inc @@ -225,11 +225,16 @@ function display_team_members($team, $offset, $sort_by) { '.tra('Country').' '; - - if ($nosort) { - $users = BoincUser::enum("teamid=$team->id limit $offset,$n"); - } else { - $users = BoincUser::enum("teamid=$team->id order by $sort_clause limit $offset,$n"); + + $cache_args = "teamid=".$team->id."&mosort=".$nosort."&order=".$sort_clause."&limit=".$offset."_".$n; + $users = unserialize(get_cached_data(TEAM_PAGE_TTL, $cache_args)); + if (!$users) { + if ($nosort) { + $users = BoincUser::enum("teamid=$team->id limit $offset,$n"); + } else { + $users = BoincUser::enum("teamid=$team->id order by $sort_clause limit $offset,$n"); + } + set_cached_data(TEAM_PAGE_TTL, serialize($users), $cache_args); } $j = $offset + 1; diff --git a/html/user/download_network.php b/html/user/download_network.php index 48a5d86548..f43a3498ec 100644 --- a/html/user/download_network.php +++ b/html/user/download_network.php @@ -22,7 +22,6 @@ require_once("../inc/cache.inc"); check_get_args(array()); -start_cache(3600); page_head(tra("Download BOINC add-on software")); echo "

" . @@ -40,17 +39,18 @@ If this is not enough you should contact the author."). "; -$httpFile = @fopen("http://boinc.berkeley.edu/addons.php?strip_header=true", "rb"); -if (!$httpFile){ - echo ""; -} else { - fpassthru($httpFile); - fclose($httpFile); +$httpFile = unserialize(get_cached_data(3600)); +if (!$httpFile) { + $httpFile = @file_get_contents("http://boinc.berkeley.edu/addons.php?strip_header=true"); + if ($httpFile) { + set_cached_data(3600, serialize($httpFile)); + } } +echo $httpFile; echo "

"; page_tail(); -end_cache(3600); + ?> diff --git a/html/user/server_status.php b/html/user/server_status.php index 0db5c057bb..7bb830b684 100644 --- a/html/user/server_status.php +++ b/html/user/server_status.php @@ -58,11 +58,6 @@ check_get_args(array("xml")); $xml = get_int("xml", true); -$cache_args = $languages_in_use[0]; -if ($xml) $cache_args = "xml=1"; -$cache_period = 3600; -start_cache($cache_period, $cache_args); - // daemon status outputs: 1 (running) 0 (not running) or -1 (disabled) // function daemon_status($host, $pidname, $progname, $disabled) { @@ -128,45 +123,68 @@ function show_counts($key, $xmlkey, $value) { return 0; } -function get_mysql_count ($query) { - $result = mysql_query("select count(*) as count from " . $query); - $count = mysql_fetch_object($result); - mysql_free_result($result); - return $count->count; +function get_mysql_count($query) { + $count = unserialize(get_cached_data(3600, "get_mysql_count".$query)); + if ($count == false) { + $result = mysql_query("select count(*) as count from " . $query); + $count = mysql_fetch_object($result); + mysql_free_result($result); + $count = $count->count; + set_cached_data(3600, serialize($count), "get_mysql_count".$query); + } + return $count; } -function get_mysql_value ($query) { - $result = mysql_query($query); - $row = mysql_fetch_object($result); - mysql_free_result($result); - return $row->value; +function get_mysql_value($query) { + $value = unserialize(get_cached_data(3600, "get_mysql_value".$query)); + if ($value == false) { + $result = mysql_query($query); + $row = mysql_fetch_object($result); + mysql_free_result($result); + $value = $row->value; + set_cached_data(3600, serialize($value), "get_mysql_value".$query); + } + return $value; } function get_mysql_assoc($query) { - $sql = "SELECT * FROM app WHERE deprecated != 1"; - $result = mysql_query($sql); - while($row = mysql_fetch_assoc($result)) { - $assoc[] = $row; + $assoc = unserialize(get_cached_data(3600, "get_mysql_assoc".$query)); + if ($assoc == false) { + $sql = "SELECT * FROM app WHERE deprecated != 1"; + $result = mysql_query($sql); + while($row = mysql_fetch_assoc($result)) { + $assoc[] = $row; + } + mysql_free_result($result); + set_cached_data(3600, serialize($assoc), "get_mysql_assoc".$query); } - mysql_free_result($result); return $assoc; } -function get_mysql_user ($clause) { - $result = mysql_query("select count(userid) as userid from (SELECT distinct userid FROM result where validate_state=1 and received_time > (unix_timestamp()-(3600*24*1)) " . $clause . ") t"); - $count = mysql_fetch_object($result); - mysql_free_result($result); - return $count->userid; +function get_mysql_user($clause) { + $count = unserialize(get_cached_data(3600, "get_mysql_user".$clause)); + if ($count == false) { + $result = mysql_query("select count(userid) as userid from (SELECT distinct userid FROM result where validate_state=1 and received_time > (unix_timestamp()-(3600*24*1)) " . $clause . ") t"); + $count = mysql_fetch_object($result); + mysql_free_result($result); + $count = $count->userid; + set_cached_data(3600, serialize($count), "get_mysql_user".$clause); + } + return $count; } -function get_cpu_time ($appid) { - $result = mysql_query(" - Select ceil(avg(cpu_time)/3600*100)/100 as cpu_time, - ceil(min(cpu_time)/3600*100)/100 as min, - ceil(max(cpu_time)/3600*100)/100 as max - from (SELECT cpu_time FROM `result` WHERE appid = $appid and validate_state =1 and received_time > (unix_timestamp()-(3600*24)) ORDER BY `received_time` DESC limit 100) t"); - $count = mysql_fetch_object($result); - mysql_free_result($result); +function get_cpu_time($appid) { + $count = unserialize(get_cached_data(3600, "get_cpu_time".$appid)); + if ($count == false) { + $result = mysql_query(" + Select ceil(avg(cpu_time)/3600*100)/100 as cpu_time, + ceil(min(cpu_time)/3600*100)/100 as min, + ceil(max(cpu_time)/3600*100)/100 as max + from (SELECT cpu_time FROM `result` WHERE appid = $appid and validate_state =1 and received_time > (unix_timestamp()-(3600*24)) ORDER BY `received_time` DESC limit 100) t"); + $count = mysql_fetch_object($result); + mysql_free_result($result); + set_cached_data(3600, serialize($count), "get_cpu_time".$appid); + } return $count; } @@ -340,12 +358,16 @@ if ($retval) { get_mysql_count("result where file_delete_state=1") ); - $result = mysql_query("select MIN(transition_time) as min from workunit"); - $min = mysql_fetch_object($result); - mysql_free_result($result); - $gap = (time() - $min->min)/3600; - if (($gap < 0) || ($min->min == 0)) { - $gap = 0; + $gap = unserialize(get_cached_data(3600, "transitioner_backlog")); + if ($gap === false) { + $result = mysql_query("select MIN(transition_time) as min from workunit"); + $min = mysql_fetch_object($result); + mysql_free_result($result); + $gap = (time() - $min->min)/3600; + if (($gap < 0) || ($min->min == 0)) { + $gap = 0; + } + set_cached_data(3600, serialize($gap), "transitioner_backlog"); } show_counts( tra("Transitioner backlog (hours)"), @@ -441,5 +463,4 @@ if ($xml) { page_tail(); } -end_cache($cache_period, $cache_args); ?> diff --git a/html/user/team_members.php b/html/user/team_members.php index 079ef8e8b2..07ffd58bb7 100644 --- a/html/user/team_members.php +++ b/html/user/team_members.php @@ -37,15 +37,15 @@ if ($offset > 1000) { $teamid = get_int("teamid"); -$cache_args = "teamid=$teamid&offset=$offset&sort_by=$sort_by"; -start_cache(TEAM_PAGE_TTL, $cache_args); - -$team = BoincTeam::lookup_id($teamid); +$cache_args = "teamid=$teamid"; +$team = unserialize(get_cached_data(TEAM_PAGE_TTL, $cache_args)); +if (!$team) { + $team = BoincTeam::lookup_id($teamid); + set_cached_data(TEAM_PAGE_TTL, serialize($team), $cache_args); +} page_head(tra("Members of %1", "$team->name")); display_team_members($team, $offset, $sort_by); page_tail(); -end_cache(TEAM_PAGE_TTL, $cache_args); - ?> diff --git a/html/user/userw.php b/html/user/userw.php index a738f615ae..c920e89bac 100644 --- a/html/user/userw.php +++ b/html/user/userw.php @@ -17,9 +17,7 @@ // along with BOINC. If not, see . require_once("../inc/util.inc"); -require_once("../inc/db.inc"); require_once("../inc/wap.inc"); -require_once("../inc/cache.inc"); check_get_args(array("id")); @@ -29,45 +27,46 @@ function show_credit_wap($user) { return $retstr; } -function show_user_wap($user) { - wap_begin(); - if (!$user) { - echo "
".tra("User not found!")."
"; - wap_end(); - return; - } - +function show_user_wap($userid) { + wap_begin(); + + $cache = unserialize(get_cached_data(USER_PAGE_TTL, "userid=".$userid)); + if (!$cache) { + $cache = new stdClass; + $cache->user = BoincUser::lookup_id($userid); + if ($cache->user->teamid) { + $cache->team = BoincTeam::lookup_id($cache->user->teamid); + } + $cache->wap_timestamp = wap_timestamp(); + set_cached_data(USER_PAGE_TTL, serialize($cache), "userid=".$userid); + } + + if (!$cache->user) { + echo "
".tra("User not found!")."
"; + wap_end(); + return; + } + // keep a 'running tab' in wapstr in case exceeds 1K WAP limit - - $wapstr = PROJECT . "
".tra("Account Data
for %1
Time:", $user->name)." " . wap_timestamp(); - $wapstr .= show_credit_wap($user); - if ($user->teamid) { - $team = BoincTeam::lookup_id($user->teamid); - $wapstr .= "
".tra("Team:")." $team->name
"; - $wapstr .= tra("Team TotCred:")." " . format_credit($team->total_credit) . "
"; - $wapstr .= tra("Team AvgCred:")." " . format_credit($team->expavg_credit) . "
"; - + $wapstr = PROJECT."
".tra("Account Data
for %1
Time:", $cache->user->name)." ".$cache->wap_timestamp; + $wapstr .= show_credit_wap($cache->user); + if ($cache->user->teamid) { + $wapstr .= "
".tra("Team:")." ".$cache->team->name."
"; + $wapstr .= tra("Team TotCred:")." " . format_credit($cache->team->total_credit) . "
"; + $wapstr .= tra("Team AvgCred:")." " . format_credit($cache->team->expavg_credit) . "
"; } else { $wapstr .= "
".tra("Team: None")."
"; } - - // don't want to send more than 1KB probably? - if (strlen($wapstr)>1024) { - echo substr($wapstr,0,1024); - } else { - echo $wapstr; - } - - wap_end(); + + // don't want to send more than 1KB probably? + if (strlen($wapstr) > 1024) { + $wapstr = substr($wapstr, 0, 1024); + } + + echo $wapstr; + wap_end(); } -$userid = get_int('id'); +show_user_wap(get_int('id')); -$cache_args = "userid=".$userid; -start_cache(USER_PAGE_TTL, $cache_args); - -$user = BoincUser::lookup_id($userid); -show_user_wap($user); - -end_cache(USER_PAGE_TTL, $cache_args); ?>