cache objects

This commit is contained in:
Shish 2009-01-20 02:47:20 -08:00
parent 5e75ad3cd4
commit 776a42d519
4 changed files with 40 additions and 66 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

@ -74,37 +74,44 @@ class PostgreSQL extends DBEngine {
// }}} // }}}
// {{{ cache engines // {{{ cache engines
interface CacheEngine { interface CacheEngine {
var $db; var $hits = 0, $misses = 0;
public function cache_get($key);
public function cache_set($key, $val, $time); public function get($key);
public function cache_delete($key); public function set($key, $val, $time);
public function delete($key);
}
class NoCache implements CacheEngine {
public function get($key) {return false;}
public function set($key) {}
public function delete($key) {}
} }
class MemCache implements CacheEngine { class MemCache implements CacheEngine {
public function __construct(Database $db) { public function __construct($args) {
$this->db = $db; $this->memcache = new Memcache;
$this->memcache->pconnect('localhost', 11211) or ($this->use_memcache = false);
} }
public function cache_get($key) { public function get($key) {
assert(!is_null($key)); assert(!is_null($key));
$val = $this->db->memcache->get($key); $val = $this->memcache->get($key);
if($val) { if($val) {
$this->cache_hits++; $this->hits++;
return $val; return $val;
} }
else { else {
$this->cache_misses++; $this->misses++;
return false; return false;
} }
} }
public function cache_set($key, $val, $time=0) { public function set($key, $val, $time=0) {
assert(!is_null($key)); assert(!is_null($key));
$this->db->memcache->set($key, $val, false, $time); $this->memcache->set($key, $val, false, $time);
} }
public function cache_delete($key) { public function delete($key) {
assert(!is_null($key)); assert(!is_null($key));
$this->db->memcache->delete($key); $this->memcache->delete($key);
} }
} }
// }}} // }}}
@ -115,7 +122,6 @@ class MemCache implements CacheEngine {
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; var $cache = null;
@ -128,14 +134,16 @@ 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($memcache)) {
$this->cache = new MemCache($this);
}
if(isset($cache)) { if(isset($cache)) {
//$matches = array(); $matches = array();
//preg_match("#(memcache)://#", $cache, $matches); 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) {
@ -156,10 +164,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");
@ -167,36 +171,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->hits;
$miss = $database->cache_misses; $miss = $database->cache->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";