Merge branch 'caching_engines'

This commit is contained in:
Shish 2009-01-20 03:56:21 -08:00
commit 0e34bab864
9 changed files with 95 additions and 58 deletions

View File

@ -204,7 +204,7 @@ class IPBan implements Extension {
private function get_active_bans() { private function get_active_bans() {
global $database; global $database;
$cached = $database->cache_get("bans"); $cached = $database->cache->get("bans");
if($cached) return $cached; if($cached) return $cached;
$bans = $database->get_all(" $bans = $database->get_all("
@ -215,7 +215,7 @@ class IPBan implements Extension {
ORDER BY end_timestamp, id ORDER BY end_timestamp, id
"); ");
$database->cache_set("bans", $bans); $database->cache->set("bans", $bans);
if($bans) {return $bans;} if($bans) {return $bans;}
else {return array();} else {return array();}
@ -225,7 +225,7 @@ class IPBan implements Extension {
global $database; global $database;
$sql = "INSERT INTO bans (ip, reason, end_timestamp, banner_id) VALUES (?, ?, ?, ?)"; $sql = "INSERT INTO bans (ip, reason, end_timestamp, banner_id) VALUES (?, ?, ?, ?)";
$database->Execute($sql, array($ip, $reason, strtotime($end), $user->id)); $database->Execute($sql, array($ip, $reason, strtotime($end), $user->id));
$database->cache_delete("bans"); $database->cache->delete("bans");
} }
// }}} // }}}
} }

View File

@ -124,13 +124,13 @@ class DatabaseConfig extends BaseConfig {
public function DatabaseConfig($database) { public function DatabaseConfig($database) {
$this->database = $database; $this->database = $database;
$cached = $database->cache_get("config"); $cached = $database->cache->get("config");
if($cached) { if($cached) {
$this->values = $cached; $this->values = $cached;
} }
else { else {
$this->values = $this->database->db->GetAssoc("SELECT name, value FROM config"); $this->values = $this->database->db->GetAssoc("SELECT name, value FROM config");
$database->cache_set("config", $this->values); $database->cache->set("config", $this->values);
} }
} }
@ -147,7 +147,7 @@ class DatabaseConfig extends BaseConfig {
$this->database->Execute("DELETE FROM config WHERE name = ?", array($name)); $this->database->Execute("DELETE FROM config WHERE name = ?", array($name));
$this->database->Execute("INSERT INTO config VALUES (?, ?)", array($name, $this->values[$name])); $this->database->Execute("INSERT INTO config VALUES (?, ?)", array($name, $this->values[$name]));
} }
$database->cache_delete("config"); $database->cache->delete("config");
} }
} }
?> ?>

View File

@ -49,7 +49,7 @@ class ImgQuerylet {
} }
} }
// }}} // }}}
// {{{ dbengines // {{{ db engines
class DBEngine { class DBEngine {
var $name = null; var $name = null;
var $auto_increment = null; var $auto_increment = null;
@ -71,7 +71,59 @@ class PostgreSQL extends DBEngine {
function init($db) { function init($db) {
} }
} }
//}}} // }}}
// {{{ cache engines
interface CacheEngine {
public function get($key);
public function set($key, $val, $time=0);
public function delete($key);
public function get_hits();
public function get_misses();
}
class NoCache implements CacheEngine {
public function get($key) {return false;}
public function set($key, $val, $time=0) {}
public function delete($key) {}
public function get_hits() {return 0;}
public function get_misses() {return 0;}
}
class MemCache implements CacheEngine {
var $hits=0, $misses=0;
public function __construct($args) {
$this->memcache = new Memcache;
$this->memcache->pconnect('localhost', 11211) or ($this->use_memcache = false);
}
public function get($key) {
assert(!is_null($key));
$val = $this->memcache->get($key);
if($val) {
$this->hits++;
return $val;
}
else {
$this->misses++;
return false;
}
}
public function set($key, $val, $time=0) {
assert(!is_null($key));
$this->memcache->set($key, $val, false, $time);
}
public function delete($key) {
assert(!is_null($key));
$this->memcache->delete($key);
}
public function get_hits() {return $this->hits;}
public function get_misses() {return $this->misses;}
}
// }}}
/* /*
* A class for controlled database access * A class for controlled database access
@ -79,8 +131,8 @@ class PostgreSQL extends DBEngine {
class Database { class Database {
var $db; var $db;
var $extensions; var $extensions;
var $cache_hits = 0, $cache_misses = 0;
var $engine = null; var $engine = null;
var $cache = null;
/* /*
* Create a new database object using connection info * Create a new database object using connection info
@ -91,7 +143,18 @@ class Database {
require_once "config.php"; require_once "config.php";
$this->engine = new MySQL(); $this->engine = new MySQL();
$this->db = @NewADOConnection($database_dsn); $this->db = @NewADOConnection($database_dsn);
$this->use_memcache = isset($memcache);
if(isset($cache)) {
$matches = array();
preg_match("#(memcache)://(.*)#", $cache, $matches);
if($matches[1] == "memcache") {
$this->cache = new MemCache($matches[2]);
}
}
else {
$this->cache = new NoCache();
}
if($this->db) { if($this->db) {
$this->db->SetFetchMode(ADODB_FETCH_ASSOC); $this->db->SetFetchMode(ADODB_FETCH_ASSOC);
$this->engine->init($this->db); $this->engine->init($this->db);
@ -110,10 +173,6 @@ class Database {
"; ";
exit; exit;
} }
if($this->use_memcache) {
$this->memcache = new Memcache;
$this->memcache->pconnect('localhost', 11211) or ($this->use_memcache = false);
}
} }
else { else {
header("Location: install.php"); header("Location: install.php");
@ -121,36 +180,6 @@ class Database {
} }
} }
// memcache {{{
public function cache_get($key) {
assert(!is_null($key));
if($this->use_memcache) {
$val = $this->memcache->get($key);
if($val) {
$this->cache_hits++;
return $val;
}
else {
$this->cache_misses++;
}
}
return false;
}
public function cache_set($key, $val, $time=0) {
assert(!is_null($key));
if($this->use_memcache) {
$this->memcache->set($key, $val, false, $time);
}
}
public function cache_delete($key) {
assert(!is_null($key));
if($this->use_memcache) {
$this->memcache->delete($key);
}
}
// }}}
// safety wrapping {{{ // safety wrapping {{{
public function execute($query, $args=array()) { public function execute($query, $args=array()) {
$result = $this->db->Execute($query, $args); $result = $this->db->Execute($query, $args);

View File

@ -144,7 +144,7 @@ class Image {
} }
public function get_tag_array() { public function get_tag_array() {
$cached = $this->database->cache_get("image-{$this->id}-tags"); $cached = $this->database->cache->get("image-{$this->id}-tags");
if($cached) return $cached; if($cached) return $cached;
if(!isset($this->tag_array)) { if(!isset($this->tag_array)) {
@ -156,7 +156,7 @@ class Image {
} }
} }
$this->database->cache_set("image-{$this->id}-tags", $this->tag_array); $this->database->cache->set("image-{$this->id}-tags", $this->tag_array);
return $this->tag_array; return $this->tag_array;
} }
@ -267,7 +267,7 @@ class Image {
array($tag)); array($tag));
} }
$this->database->cache_delete("image-{$this->id}-tags"); $this->database->cache->delete("image-{$this->id}-tags");
} }
@ -528,8 +528,8 @@ function get_debug_info() {
$i_files = count(get_included_files()); $i_files = count(get_included_files());
global $_execs; global $_execs;
global $database; global $database;
$hits = $database->cache_hits; $hits = $database->cache->get_hits();
$miss = $database->cache_misses; $miss = $database->cache->get_misses();
$debug = "<br>Took $i_utime + $i_stime seconds and {$i_mem}MB of RAM"; $debug = "<br>Took $i_utime + $i_stime seconds and {$i_mem}MB of RAM";
$debug .= "; Used $i_files files and $_execs queries"; $debug .= "; Used $i_files files and $_execs queries";
$debug .= "; Sent $_event_count events"; $debug .= "; Sent $_event_count events";

View File

@ -79,7 +79,7 @@ class GenericPage {
header("Cache-control: no-cache"); header("Cache-control: no-cache");
usort($this->blocks, "blockcmp"); usort($this->blocks, "blockcmp");
$layout = new Layout(); $layout = new Layout();
$layout->display_page($this); $layout->display_page($context);
break; break;
case "data": case "data":
if(!is_null($this->filename)) { if(!is_null($this->filename)) {

View File

@ -42,8 +42,10 @@ Tips
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
class Layout { class Layout {
public function display_page($page) { public function display_page($context) {
global $config; $page = $context->page;
$config = $context->config;
$theme_name = $config->get_string('theme'); $theme_name = $config->get_string('theme');
$base_href = $config->get_string('base_href'); $base_href = $config->get_string('base_href');
$data_href = get_base_href(); $data_href = get_base_href();

View File

@ -1,8 +1,10 @@
<?php <?php
class Layout { class Layout {
function display_page($page) { function display_page($context) {
global $config; $page = $context->page;
$config = $context->config;
$theme_name = $config->get_string('theme', 'default'); $theme_name = $config->get_string('theme', 'default');
$data_href = get_base_href(); $data_href = get_base_href();
$contact_link = $config->get_string('contact_link'); $contact_link = $config->get_string('contact_link');

View File

@ -1,8 +1,10 @@
<?php <?php
class Layout { class Layout {
function display_page($page) { function display_page($context) {
global $config; $page = $context->page;
$config = $context->config;
$theme_name = $config->get_string('theme', 'default'); $theme_name = $config->get_string('theme', 'default');
$data_href = get_base_href(); $data_href = get_base_href();
$contact_link = $config->get_string('contact_link'); $contact_link = $config->get_string('contact_link');

View File

@ -1,8 +1,10 @@
<?php <?php
class Layout { class Layout {
function display_page($page) { function display_page($context) {
global $config; $page = $context->page;
$config = $context->config;
$theme_name = $config->get_string('theme', 'default'); $theme_name = $config->get_string('theme', 'default');
$data_href = get_base_href(); $data_href = get_base_href();
$contact_link = $config->get_string('contact_link'); $contact_link = $config->get_string('contact_link');