Optimizations: memcache and persistant db connections

This commit is contained in:
Latif Khalifa
2013-10-11 16:31:12 +02:00
parent a0d0d36f05
commit e8aca8283b
7 changed files with 79 additions and 6 deletions

View File

@@ -61,5 +61,6 @@ rrmdir(ReportParser::getWorkPath());
if ($nr)
{
Memc::flush();
IRCNotify::send("#SingularityViewer", "[CrashProcessor] $nr new reports. http://crash.singularityviewer.org/crashes.php ");
}
}

View File

@@ -15,6 +15,12 @@ body {
color: #a0a0a0;
}
@media screen and (max-width: 768px) {
body {
padding: 0px;
}
}
div {
display: block;
padding: 0;

View File

@@ -66,12 +66,16 @@ class CrashReport
global $DB;
$where = $filter->getWhere();
if (!$res = $DB->query("select count(id) as total from reports $where") OR !$row = $DB->fetchRow($res))
$q = "select count(id) as total from reports $where";
if (false !== $cached = Memc::getq($q)) return $cached;
if (!$res = $DB->query($q) OR !$row = $DB->fetchRow($res))
{
return 0;
}
else
{
Memc::setq($q, $row["total"]);
return $row["total"];
}
}
@@ -81,7 +85,10 @@ class CrashReport
global $DB;
$ret = array();
if (!$res = $DB->query("select $fields from reports " . $filter->getWhere() . kl_str_sql(" order by id desc limit !i offset !i", $filter->limit, $filter->offset)))
$q = "select $fields from reports " . $filter->getWhere() . kl_str_sql(" order by id desc limit !i offset !i", $filter->limit, $filter->offset);
if (false !== $cached = Memc::getq($q)) return $cached;
if (!$res = $DB->query($q))
{
return $ret;
}
@@ -93,6 +100,7 @@ class CrashReport
$ret[] = $r;
}
Memc::setq($q, $ret);
return $ret;
}

View File

@@ -40,7 +40,7 @@ class DBH
$this->db_user = $db_user;
$this->db_host = $db_host;
$this->dbh = @mysql_connect($db_host, $db_user, $db_pass);
$this->dbh = @mysql_pconnect($db_host, $db_user, $db_pass);
if (!$this->dbh) {
DBH::log("[error] connection to database failed");

50
htdocs/lib/Memc.php Normal file
View File

@@ -0,0 +1,50 @@
<?php
class Memc
{
static $mem;
static $active = false;
static $expire = 3600;
function init()
{
if (!class_exists("Memcached")) return;
self::$mem = new Memcached("scr");
$servers = self::$mem->getServerList();
if (empty($servers))
{
//This code block will only execute if we are setting up a new EG(persistent_list) entry
self::$mem->setOption(Memcached::OPT_RECV_TIMEOUT, 1000);
self::$mem->setOption(Memcached::OPT_SEND_TIMEOUT, 3000);
self::$mem->setOption(Memcached::OPT_TCP_NODELAY, true);
self::$mem->setOption(Memcached::OPT_PREFIX_KEY, "cr_");
self::$mem->addServer("localhost", 11211);
}
self::$active = true;
}
function getq($q)
{
if (!self::$active) return false;
$key = md5($q);
return self::$mem->get($key);
}
function setq($q, $data)
{
if (!self::$active) return false;
$key = md5($q);
return self::$mem->set($key, $data, self::$expire);
}
function flush()
{
if (!self::$active) return false;
self::$mem->flush();
}
}
?>

View File

@@ -86,6 +86,8 @@ class SearchFilter
$ret = array();
$where = $this->chan ? kl_str_sql("where client_channel=!s", $this->chan) : '';
$q = "select distinct client_version from reports $where order by client_version desc";
if (false !== $cached = Memc::getq($q)) return $cached;
if (!$res = $DB->query($q))
{
return $ret;
@@ -96,6 +98,7 @@ class SearchFilter
$ret[] = $row["client_version"];
}
Memc::setq($q, $ret);
return $ret;
}
@@ -105,6 +108,8 @@ class SearchFilter
$ret = array();
$q = "select distinct grid from reports order by grid asc";
if (false !== $cached = Memc::getq($q)) return $cached;
if (!$res = $DB->query($q))
{
return $ret;
@@ -115,6 +120,7 @@ class SearchFilter
$ret[] = $row["grid"];
}
Memc::setq($q, $ret);
return $ret;
}
@@ -135,7 +141,7 @@ class SearchFilter
</script>
<form method="get">
<div style="display: inline-block;" class="ui-widget ui-corner-all ui-widget-content">
<div class="ui-widget ui-corner-all ui-widget-content">
<div class="ui-widget-header" style="padding: 5px">Filter</div>
<div class="filterelem">

View File

@@ -15,6 +15,8 @@ function __autoload($class)
require_once(SITE_ROOT . '/lib/' . $class . '.php');
}
Memc::init();
/* Directory relative to server root
* No leading or trailing slash.
* Example: http://www.example.com/applications/app1/
@@ -67,7 +69,7 @@ if (!$DB->connect($DB_NAME, $DB_HOST, $DB_USER, $DB_PASS)) {
die();
}
Option::init();
//Option::init();
$S = new Session();
if (!defined('NO_SESSION') && PHP_SAPI != "cli") {