From 776a42d519e4b5407ff1d6d88f8eb835d21d8f58 Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 20 Jan 2009 02:47:20 -0800 Subject: [PATCH] cache objects --- contrib/ipban/main.php | 6 +-- core/config.class.php | 6 +-- core/database.class.php | 84 ++++++++++++++-------------------------- core/imageboard.pack.php | 10 ++--- 4 files changed, 40 insertions(+), 66 deletions(-) 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 5e3afea4..54839976 100644 --- a/core/database.class.php +++ b/core/database.class.php @@ -74,37 +74,44 @@ class PostgreSQL extends DBEngine { // }}} // {{{ cache engines interface CacheEngine { - var $db; - public function cache_get($key); - public function cache_set($key, $val, $time); - public function cache_delete($key); + var $hits = 0, $misses = 0; + + public function get($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 { - public function __construct(Database $db) { - $this->db = $db; + public function __construct($args) { + $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)); - $val = $this->db->memcache->get($key); + $val = $this->memcache->get($key); if($val) { - $this->cache_hits++; + $this->hits++; return $val; } else { - $this->cache_misses++; + $this->misses++; return false; } } - public function cache_set($key, $val, $time=0) { + public function set($key, $val, $time=0) { 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)); - $this->db->memcache->delete($key); + $this->memcache->delete($key); } } // }}} @@ -115,7 +122,6 @@ class MemCache implements CacheEngine { class Database { var $db; var $extensions; - var $cache_hits = 0, $cache_misses = 0; var $engine = null; var $cache = null; @@ -128,14 +134,16 @@ class Database { require_once "config.php"; $this->engine = new MySQL(); $this->db = @NewADOConnection($database_dsn); - $this->use_memcache = isset($memcache); - if(isset($memcache)) { - $this->cache = new MemCache($this); - } if(isset($cache)) { - //$matches = array(); - //preg_match("#(memcache)://#", $cache, $matches); + $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) { @@ -156,10 +164,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"); @@ -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 {{{ 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..3d1f1945 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->hits; + $miss = $database->cache->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";