diff --git a/contrib/ipban/main.php b/contrib/ipban/main.php index 48c7e106..94344b88 100644 --- a/contrib/ipban/main.php +++ b/contrib/ipban/main.php @@ -204,7 +204,7 @@ class IPBan implements Extension { private function get_active_bans() { global $database; - $cached = $database->cache_get("bans"); + $cached = $database->cache->get("bans"); if($cached) return $cached; $bans = $database->get_all(" @@ -215,7 +215,7 @@ class IPBan implements Extension { ORDER BY end_timestamp, id "); - $database->cache_set("bans", $bans); + $database->cache->set("bans", $bans); if($bans) {return $bans;} else {return array();} @@ -225,7 +225,7 @@ class IPBan implements Extension { global $database; $sql = "INSERT INTO bans (ip, reason, end_timestamp, banner_id) VALUES (?, ?, ?, ?)"; $database->Execute($sql, array($ip, $reason, strtotime($end), $user->id)); - $database->cache_delete("bans"); + $database->cache->delete("bans"); } // }}} } diff --git a/core/config.class.php b/core/config.class.php index a33f31bc..50be4250 100644 --- a/core/config.class.php +++ b/core/config.class.php @@ -124,13 +124,13 @@ class DatabaseConfig extends BaseConfig { public function DatabaseConfig($database) { $this->database = $database; - $cached = $database->cache_get("config"); + $cached = $database->cache->get("config"); if($cached) { $this->values = $cached; } else { $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("INSERT INTO config VALUES (?, ?)", array($name, $this->values[$name])); } - $database->cache_delete("config"); + $database->cache->delete("config"); } } ?> diff --git a/core/database.class.php b/core/database.class.php index 05af31e2..15bf2133 100644 --- a/core/database.class.php +++ b/core/database.class.php @@ -49,7 +49,7 @@ class ImgQuerylet { } } // }}} -// {{{ dbengines +// {{{ db engines class DBEngine { var $name = null; var $auto_increment = null; @@ -71,7 +71,59 @@ class PostgreSQL extends DBEngine { 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 @@ -79,8 +131,8 @@ class PostgreSQL extends DBEngine { class Database { var $db; var $extensions; - var $cache_hits = 0, $cache_misses = 0; var $engine = null; + var $cache = null; /* * Create a new database object using connection info @@ -91,7 +143,18 @@ class Database { require_once "config.php"; $this->engine = new MySQL(); $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) { $this->db->SetFetchMode(ADODB_FETCH_ASSOC); $this->engine->init($this->db); @@ -110,10 +173,6 @@ class Database { "; exit; } - if($this->use_memcache) { - $this->memcache = new Memcache; - $this->memcache->pconnect('localhost', 11211) or ($this->use_memcache = false); - } } else { 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 {{{ public function execute($query, $args=array()) { $result = $this->db->Execute($query, $args); diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index 0f38d6e1..13e3cd71 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -144,7 +144,7 @@ class Image { } 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(!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; } @@ -267,7 +267,7 @@ class Image { 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()); global $_execs; global $database; - $hits = $database->cache_hits; - $miss = $database->cache_misses; + $hits = $database->cache->get_hits(); + $miss = $database->cache->get_misses(); $debug = "
Took $i_utime + $i_stime seconds and {$i_mem}MB of RAM"; $debug .= "; Used $i_files files and $_execs queries"; $debug .= "; Sent $_event_count events"; diff --git a/core/page.class.php b/core/page.class.php index 3bd5c2ca..b22fff3c 100644 --- a/core/page.class.php +++ b/core/page.class.php @@ -79,7 +79,7 @@ class GenericPage { header("Cache-control: no-cache"); usort($this->blocks, "blockcmp"); $layout = new Layout(); - $layout->display_page($this); + $layout->display_page($context); break; case "data": if(!is_null($this->filename)) { diff --git a/themes/danbooru/layout.class.php b/themes/danbooru/layout.class.php index b3320201..4231da2a 100644 --- a/themes/danbooru/layout.class.php +++ b/themes/danbooru/layout.class.php @@ -42,8 +42,10 @@ Tips * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ class Layout { - public function display_page($page) { - global $config; + public function display_page($context) { + $page = $context->page; + $config = $context->config; + $theme_name = $config->get_string('theme'); $base_href = $config->get_string('base_href'); $data_href = get_base_href(); diff --git a/themes/default/layout.class.php b/themes/default/layout.class.php index 07b61a72..167011c8 100644 --- a/themes/default/layout.class.php +++ b/themes/default/layout.class.php @@ -1,8 +1,10 @@ page; + $config = $context->config; + $theme_name = $config->get_string('theme', 'default'); $data_href = get_base_href(); $contact_link = $config->get_string('contact_link'); diff --git a/themes/futaba/layout.class.php b/themes/futaba/layout.class.php index a1d27860..0a6ea41c 100644 --- a/themes/futaba/layout.class.php +++ b/themes/futaba/layout.class.php @@ -1,8 +1,10 @@ page; + $config = $context->config; + $theme_name = $config->get_string('theme', 'default'); $data_href = get_base_href(); $contact_link = $config->get_string('contact_link'); diff --git a/themes/minimal/layout.class.php b/themes/minimal/layout.class.php index 4a62a41d..306da52f 100644 --- a/themes/minimal/layout.class.php +++ b/themes/minimal/layout.class.php @@ -1,8 +1,10 @@ page; + $config = $context->config; + $theme_name = $config->get_string('theme', 'default'); $data_href = get_base_href(); $contact_link = $config->get_string('contact_link');